16 Pages

Matlab_probs3

Course: M 426, Fall 2009
School: Delaware
Rating:
 
 
 
 
 

Word Count: 4193

Document Preview

Matlab Beginning Exercises R. J. Braun Department of Mathematical Sciences University of Delaware Version of September 5, 2008 1 Introduction This collection of exercises is intended to help you start learning Matlab. Matlab is a huge package with many capabilities, but it is easy to use on many levels. The text indicated by the numbers will be Matlab commands that you can input to the Matlab prompt. text will...

Register Now

Unformatted Document Excerpt

Coursehero >> Delaware >> Delaware >> M 426

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Matlab Beginning Exercises R. J. Braun Department of Mathematical Sciences University of Delaware Version of September 5, 2008 1 Introduction This collection of exercises is intended to help you start learning Matlab. Matlab is a huge package with many capabilities, but it is easy to use on many levels. The text indicated by the numbers will be Matlab commands that you can input to the Matlab prompt. text will indicate quantities that you need to replace with in a Matlab command. Text to the right of a % symbol are comments and need not be typed at the prompt unless you wish to have the comment in a diary or other le. In nearly all cases, no output will be shown; thats for you to nd! 2 Getting Started In the computer classroom, you can start Matlab by clicking on the icon. In Windows, you can double click on an icon. In Linux, you can open Matlab by opening a terminal window and then typing the appropriate command. Typing the command matlab followed by <enter> will start Matlab using its window manager. In this mode, you can nd helpful pull down menus across the top of the window (or desktop). The default conguration has one main command window, and two other windows that allow access to the workspace, command history, current directory and launch pad. Typing the command matlab -nojvm runs Matlab without the window manager. This can avoid some annoying bugs in the window manager that may appear (particularly in linux) on starting up. This will give only the command window; plots will appear in separate graphics windows in both modes. To exit, use the pull down menu under File or type the command exit in the command window. 3 Scalar arithmetic 1. 3*2^4 2. (3*2)^4 3. 3-2^4 4. 3^4-3 5. 8/2^4 6. 2^4\8 7. 8^4/2 % same as previous! two different divisions, \ and / % parentheses have highest priority Matlab can be used as a calculator. Some of the problems indicate the precedence of arithmetic operations. Precedence of arithmetic operations is: (1) parentheses, (2) exponentiation, (3) multiplication and division, and (4) addition and subtraction. Calculations proceed from left to right on the line and equal precedence is settled in this way. 1 4 Vectors It will typically be that our basic unit for computing will be a vector. We need to be able to create and manipulate them. Save yourself some typing: use the arrow keys to recall commands and to edit them! 4.1 Creating vectors % create a row vector (spaces) % colon generates list; default stride 1 % start : stride : stop specifies list % same as last; semicolon suppresses output 1. x = [3 4 7 11] 2. x = 3:8 3. x = 8:-1:0 4. xx = [ 8 7 6 5 4 3 2 1 0]; 5. xx 6. x = linspace(0,1,11) 7. x = 0:0.1:1 8. y = linspace(0,1); 9. length(x) 10. length(y) 11. size(x) 12. size(y) 13. y(3) 14. y(1:12) 15. y([3 6 9 12]) 16. x 17. z = [ 1+2*i 4-3*i ] 18. z 19. z. 20. 3*[1 2 5] % display contents % generate vector automatically % same thing % note semicolon! % access single element % access first twelve elements % access values specified in a vector! % transpose % note difference in transposes! % factor replicated, multiplies each element Matlab labels the arrays (vectors and matrices) beginning with 1; this will be an important programming detail on more than one occasion. 2 4.2 Help with vectors and other things Matlab has extensive online help; try these commands to get help at the command line. If the window manager is running in Matlab, you can also get browser-based interactive help. Try both. 1. help help 2. help length 3. help size 4. help linspace 5. help logspace 6. help clc 7. help clear 8. help who 9. help whos Note that Matlab help is also available in html format by using the menu bar at the top of the desktop. The help is more comprehensive in the browser-based help, but it is sometimes harder to nd if you already know the command name. % try Matlabs extensive help 5 5.1 Mathematical functions and plotting Elementary functions in Matlab 1. help elfun 2. Use the following few commands (a script) to make a plot. The evaluation of v = cos(u) in Matlab creates a vector whose elements are v(k) = cos(u(k)) where k = 1, 2, ...n. n = 11; u = linspace(0,2*pi,n); v = cos(u); % all function evaluations done at once! plot(u,v) 3. Once you have typed these to the Matlab prompt, you can recall and edit them one at a time, or you can save time by not having to retype them in the following way. You can save these commands in a le and execute the le. Click on the blank sheet icon at the upper left in the Matlab desktop; using the Matlab editor, you can type the commands or copy them to the editor and then save it to lename .m, where lename is the name you supply. (Avoid reserved words in Matlab, e.g., plot; see below for more info. Suggestion: call it rstplot.m.) You can execute this newly created le by typing the lename without extension (that is, without the .m) to the Matlab prompt. This collections of commands is essentially a small Matlab program, and this kind of le is called a script le. 4. The last plot had a crude curve representing the cosine function; repeat the last example with n=101 to improve the curve. 5. Add these commands to clean up the plot by adding axis labels and a title. The text inside the single quotes is a string which we intend to be the labels, etc. 3 xlabel(u); ylabel(v); title(v = cos(u)); Further improvements in plotting appearance will be discussed later; the command help plot will be very helpful. 5.2 Script and function les After this script le has run, if you type u at the prompt, you will see the contents of this array. Script les leave all variables that were created in the workspace. This can be advantageous during debugging and writing of Matlab code. There is a dierent type of le, called a function le, that creates its own work space, does calculations, returns values, then wipes out the workspace. This is analogous to what occurs in many programming languages. Functions generally execute faster than scripts and we will typically use them in this course. If you insert the line function rstplot at the beginning of the script le you made, execute clear all and close all, then execute the le rstplot.m, you will see the plot again, but typing u at the prompt will not give you its contents, since there will be no u in the workspace. We shall see how to return results from functions many times during the semester. It may be advantageous to develop a function as a script rst, and then convert it to the desired function when it is working as expected. 5.3 Reserved words in Matlab The following words are already used in Matlab and they must be avoided: for, end, if, while, function, return, elseif, case, otherwise, switch, continue, else, try, catch, global, persistent, break. (You can use similar words by capitalizing one or more letters if your system is case sensitive.) the following variable names are special in Matlab and you should avoid changing them unless you are very careful about what you are doing: ans, beep, pi, eps, inf, NaN, nan, i, j, nargin, nargout, realmin, realmax, bitmax, varargin, varargout. Try the following to see the distinction between the two cases just described. 1. end = 1 2. End = 1 3. pi = 3 4. pi 5. clear pi 6. pi % back to the present! % you should get an error % could get error, depending on system % forgetting thousands of years of progress! 6 Diary les We can record what was typed and the text-based output during a Matlab session in a le called a diary le. This recording of a session will be useful for homework and projects. It is not a program and the Matlab commands cant be run in the desktop like a script or function le can. 1. e = [ 1 2 3; 4 5 6; 7 8 0] 2. f = [9 8 7; 6 5 4; 3 2 1] 3. g = [1 2 3 4; 5 6 7 8; 9 10 11 12] 4. h = [1 1 1 1; 2 2 2 2; 3 3 3 3] 4 5. diary ThisClassIsGreat 6. g*h 7. e*f 8. diary off % puts diary in file called ThisClassIsGreat % more about arithmetic ops later! % diary file closed 9. dispWill you see this? % not this line! 10. diary ThisClassIsGreat 11. f*e 12. diary % diary toggles with no additional specification % reopening it will append additional work (Note that in the computer classroom, this le is by default written in ~/desktop.) 7 Output We will have the need to produce legible output; lets see how to do that. First, we will do some very basic output commands; then, we need to see how to repeat commands in Matlab. Finally, we will do a little nicer output. 7.1 Basic output % inside quotes: verbatim 1. disp(The disp command writes text.) 2. t = logspace(-3,6,10); 3. x = 0.1234567890123456*t; 4. format short 5. t 6. x 7. format long 8. x 9. format long e 10. x 11. format short e 12. x 13. format short g 14. x 15. format long g 16. x 17. disp(sprintf(The 10th element of x is x(10) = %6.3f,x(10))) 5 18. disp(sprintf(The 10th element of x is x(10) = %10.3f,x(10))) 19. disp(sprintf(The 10th element of x is x(10) = %12.3f,x(10))) 20. disp(sprintf(The 10th element of x is x(10) = %8.3e,x(10))) 21. disp(sprintf(The 10th element of x is x(10) = %8.3e,x(10))) 22. disp(sprintf(The 10th element of x is x(10) = %8.12e,x(10))) 23. disp(sprintf(The 10th element of x is x(10) = %24.15e,x(10))) 24. disp(sprintf(The 10th element of x is x(10) = %1.4g,x(10))) 25. disp(sprintf(The 10th element of x is x(10) = %16.12g,x(10))) 26. disp(sprintf(The 10th element of x is x(10) = %24.15g,x(10))) 7.2 Repeating Commands I The for loop repeats the commands that are inside of it. The basic structure is for <list-of-values> <statements-to-be-done> end Use this structure to repeat output, or do calculations 1. % for loop now for k = 1:5 disp(sprintf(%8.5e,x(k))) end 2. % for loop doing a calculation total = 0 for k = 1:10 total = total + x(k); disp(sprintf(Total for k=%2.0f is %15.8g,k,total)) end 3. sum(x) % built in matlab command sums components of vector Here the command sum can replace the loop; it adds up the elements of a vector. 7.3 Creating a table 1. Try this script to make a neat table. disp( ) % a blank line disp( k t x ) disp(----------------------------) for k=1:10 disp(sprintf(%2.0f %7.3g %7.7e,k,t(k),x(k))) end 6 8 More about working with vectors and matrices We need more tools to eciently manipulate vectors and matrices in Matlab. 8.1 Vector and matrix arithmetic Well now explore making matrices and doing arithmetic with them. There are some new arithmetic operations that you did not see before in linear algebra, but they will be useful nonetheless. 1. g = [1; 2; 3; 4] 2. g = [1 2 3 4; 5 6 7 8] 3. g - 2 4. 2*g-1 5. g = [1 2 3 4; 5 6 7 8; 9 10 11 12] 6. h = [1 1 1 1; 2 2 2 2; 3 3 3 3] 7. g-h 8. g*h 9. h*g 10. g.*h 11. g./h 12. h.\g 13. g*h 14. g*h 15. e = [ 1 2 3; 4 5 6; 7 8 0] 16. f = [9 8 7; 6 5 4; 3 2 1] 17. e*f 18. f*e 19. q = rand(3) 20. A^2 21. A^2 - A*A 22. A.^2 23. A.^2 - A*A % Not same! % matrix with elements uniformly on [0,1] % raise matrix to a power % verify with definition % square each element % not the same! % NOT the usual matrix multiplication! Elementwise! % elementwise division; denominator below slash % Last two same! % What happened? 7 8.2 Standard arrays Some matrices occur so often that Matlab has standard ways to produce them; this makes programming and use easier. 1. ones(3) 2. zeros(2,5) 3. size(zeros(2,5)) 4. g = [1 2 3 4; 5 6 7 8; 9 10 11 12] 5. size(g) 6. ones(size(g)) 7. eye(4) 8. eye(2,4) 9. eye(4,2) 10. rand(3) 11. b = eye(3) 12. rand(size(b)) 13. a = 1:4 14. diag(a) 15. diag(a,1) 16. diag(a,-2) % Random elements uniformly distributed between 0 and 1 % Identity matrix % All ones % All zeros 8.3 More about creating arrays % slowest % slow % faster % fastest 1. d = exp(1) 2. d*ones(3,4) 3. d+zeros(3,4) 4. d(ones(3,4)) 5. repmat(d,3,4) 8.4 Selecting and changing parts of matrices % single element % all rows, second column % second row, all columns 1. g = [1 2 3 4; 5 6 7 8; 9 10 11 12] 2. g(3,2) 3. g(:,2) 4. g(2,:) 5. g(1:2,:) 6. g(2,:) = [1 1 1 1] 7. g(:,2) = [2; 2; 2] % replace row % replace column 8 9 A start at matrix algebra In linear algebra, there was a need to solve systems, nd determinants and inverses, and to construct special matrices; we now scratch the surface of these capabilities in Matlab. 1. A = rand(5) 2. b = rand(5,1) 3. det(A) 4. inv(A) 5. x = inv(A)*b % matrix of coefficients from Ax=b % right hand side % determinant of A % inverse of A % solves system, NOT recommended! 6. size(x), size(b), size(A) 7. x2 = A\b 8. spy(A) 9. D = diag(1:5) 10. spy(D) 11. L = tril(A) 12. U = triu(A) 13. size(U), spy(U) % better way to solve systems % sparsity plot: show nonzero elements 10 10.1 More vectors and plotting Vectorizing function evaluations We try a dierent rational function than in your textbook. The ideas behind rational functions approximations are discussed in Numerical Analysis by Burden and Faires (Section 8.4), which is on reserve for this course. (Maple can compute the rational function approximation with the ratpoly command.) Consider the rational function 3 1 3 1 5 x + 20 x2 60 x3 , (1) y= 2 1 2 1 + 5 x + 20 x which approximates ex on the interval x [0, 1]. We want to implement function this for plotting and to compare with the exponential function. 1. We could plot the function as follows; I recommend opening the editor in matlab and typing this into the editor and saving the le. The resulting script le can be run in Matlab by typing the lename without the extension .m to the command window prompt. The rst approach is: n = 150; x = linspace(0,1,n); y = zeros(1,n); for k = 1:n y(k) = (1-(3/5)*x(k)+(3/20)*x(k)^2 -(x(k)/60)*x(k)^2)/... (1+(2/5)*x(k)+(1/20)*x(k)^2) end plot(x,y) xlabel(x), ylabel(y); title(Rational function approximation to e^{-x}) 9 This approach makes no use of vectorization; the for loop computes each element one at a time. Note that x2 is computed several times. 2. Now make use of vectorization and try to minimize the amount of calculation of powers of x; you may type this in by modifying your previous script le, or download this script from the course web page. In this case, we % Script file: RatFunPlot % Compare rational function approximation to exp(-x) on [0,1] % f(x) = (1-3x/3-3x^2/20-x^3/60)/(1+2x/5+x^2/20) % n = 150; x = linspace(0,1,n); xsqd = x.^2; num = 1-(3/5)*x+(3/20)*xsqd -(x/60).*xsqd den = 1+(2/5)*x+(1/20)*xsqd y = num./den; plot(x,y,x,exp(-x)) xlabel(x); title(Rational function approximation to e^{-x}) legend(Rational Function,e^{-x},0) In this case, the calculation of x2 over all entries is computed in a single line; this is a vectorized calculation. The numerator and denominator are computed similarly, using the result of the squaring operation xsqd. The quotient is also a vectorized calculation. The legend is located by the number of the corner of the plot when counting starting from the upper right and proceeding counterclockwise (for values 1 through 4); a value of 0 attempts to nd the least conict with data and -1 locates the legend to the right of the plot. More details about the legend can be found in Matlab help. 10.2 More about plotting x = linspace(0,1); N = length(x); y = 1 - x + x.^2/2 - x.^3/6; exact = exp(-x); figure % create new figure window for plotting y plot(x,y,r-.,LineWidth,2) figure % create new figure window plot(x,exact,LineWidth,2) hold on % causes subsequent curves to be added to current figure plot(x,y,r-.,LineWidth,2) hold off % returns to normal plotting mode title(sprintf(Approximate and exact with %3.0f points,N)) xlabel(x); legend(Exact,Approximate,Location,NorthEast) We can use hold on and hold off to control when to add curves to gures. Note the little trick to write a variables value in the title of the plot using sprintf. You will want to use this during this semester. 1. We can control the number of gures and add to gures that are open. Try this script: 2. close all % will close all graphics windows 10 11 11.1 Conditional Execution and Repeating Commands II Values and Variables in Matlab Values that variables may take in Matlab include oating point (double precision, e.g.), character and logical. Floating point numbers are the standard numerical values in Matlab; character values are messages and labels containing letters and numbers. The logical values are True and False, which are indicated in Matlab with a 1 and a 0, respectively. Variables with these values are not arithmetic and are used dierently that numerical array to access parts of arrays; more about this in class. 11.2 Relational operators Relational operators carry out comparisons or tests and the result is to return logical values True (1) or False (0). The relational operators that are available (and their Matlab form) are: less than (<), less than or equal to (<=), greater than (<), greater than or equal to (>=), equal to (==), and not equal to (=). Note that == is a test between two quantities, while = assigns a value to a variable. We can use these to compare scalars and arrays; try out the following. 1. a = 4; b = -1; c = 1; 2. a > b 3. a < b 4. a >= b 5. mytest = (abs(b) == c) % parentheses optional but clearer % note that the output is a logical value Things get a little more complicated with arrays. Using the g and h from above, try out the following. 1. g = [1 2 3 4; 5 6 7 8; 9 10 11 12] 2. h = [3 3 4 4; 5 5 6 6; 7 7 8 8] 3. h >= g 4. g == h % note that the output is a logical value 5. bigger = (g >= h) 6. g(bigger) 7. g([0 0 0 1; 1 1 1 1; 1 1 1 1]) % array argument not logical! We will use some of this kind of thing programming later, but I dont want to elaborate now. 11.3 Logical operators We can string relational operators together and manipulate logical variables using logical operators. They are (Matlab in parentheses): logical AND (&), logical OR (|) and logical NOT (). The NOT operator just changes the value of the logical variable to the other value. A truth table shows what the AND and OR operators do; it will be given in class. The upshot is that the both parts from an AND operator must be True to return a True; only one of the inputs for the OR operator need be True for it to return True. Continuing with the last set of Matlab commands, try these out. 1. h >= g 2. p = ~(h >= g) 11 3. ~p 4. (g == h) & (g > h) 5. (g == h) | (g > h) 6. g >= h 11.4 The while loop Sometimes, we want to repeat commands until a condition is satised. If we dont know how many repetitions are required in advance, a while loop is preferable over a for loop. The general structure is while <expression> <commands> end The commands inside the while loop will be repeated as long as the expression returns a value of True, provided that expression returns a scalar. If the expression evaluates to an array, then all of the values that result from it must be True for commands to be evaluated. An example follows; it could be typed into a script le or at the command prompt. In the example, we halve a number until it is so small that the computer cant detect it when added to 1; the last number before this occurs is called machine epsilon and well talk about it more in class. num = 0; myeps = 1; while (1+myeps) > 1 myeps = myeps/2; num = num + 1; end num myeps = 2*myeps eps % matlabs preset variable for machine epsilon The result is that myeps = eps = 252 . 11.5 The if-else-end construct Sometimes we want to compute one set of commands or another depending on the result of a relational test. There are several forms that this sort of construct can take. 11.5.1 Simplest The simplest is: if <expression> <commands evaluated if True> end An example follows; given a positive number of pens, a cost is computed and displayed. if (pens >= 0) cost = pens*1.99; disp(sprintf(Cost of %3.0f pens is $ %5.2f,pens,cost)) end 12 11.5.2 Intermediate The next most complicated is: if <expression> <commands evaluated if True> else <commands evaluated if False> end The example now prints a warning for negative numbers of pens. if (pens >= 0) cost = pens*1.99; disp(sprintf(Cost of %3.0f pens is $ %5.2f,pens,cost)) else disp(An invalid number of pens was specified.) end 11.5.3 Most general The most general form is: if <expression1> <commands evaluated elseif <expression2> <commands evaluated elseif <expression3> <commands evaluated elseif <expression4> <commands evaluated else <commands evaluated end if expression1 is True> if expression2 is True> if expression3 is True> if expression4 is True> if all other expressions are False> We arent limited to 4 expressions here; we could add more. We could use this to price pens according to quantity, as follows. sale = false; if (pens >= 0) & (pens < 20) cost = pens; sale = true; elseif (pens >= 20) & (pens < 40) cost = pens*0.95; sale = true; elseif (pens >= 40) & (pens <= 100) cost = pens*0.90; sale = true; else disp(not a valid number of pens) end if sale disp(sprintf(Cost of %3.0f pens ...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Delaware - M - 426
University of Delaware Department of Mathematical Sciences Math 426/CISC 410 Intro to Numerical Analysis and Algorithmic Computing 08F R. J. Braun Your name: Project 3: SVD Image Compression In class, we used the SVD to compress an image. We treated
Delaware - M - 428
University of Delaware Department of Mathematical Sciences Math 428 Algorithmic and Numerical Solution of Differential Equations 08S R. J. Braun Project 2: FitzHugh-Nagumo Equations Due date: 5/1/08, 3:30 pm. YOUR NAME: In this project you will use M
Delaware - M - 426
Foundations of Numerical AnalysisAn Introduction using MATLABTobin A. Driscoll and Richard J. Braun September 3, 2008Copyright c 2008 by T.A. Driscoll and R.J. Braun. All rights reserved. Please do not redistribute for free or for prot.iiCon
Delaware - M - 428
Math 428 Numerical and Algorithmic Solution of Dierential Equations 08S R. J. Braun Department of Mathematical Sciences University of Delaware Honors Project 1. Due 4/17/08, 3:30pm. In this project you will reproduce some of the eorts to compute 10 d
Delaware - M - 428
UNIVERSITY OF DELAWARE Mathematical Sciences Department Math 428/Cisc 411 Algorithmic &amp; Numerical Solution of Differential Equations Spring 2008 Instructor: Dr. Richard J. Braun, Ewing 509, (302) 831-1869, braun@math.udel.edu. Text: &quot;Numerical Analys
Delaware - M - 426
Math 426/CISC 410 08F, All Sections R.J. Braun Homework 9 Solutions, Hints and AnswersProblem 4.3.1. The ratios below lead one to the hypothesis that = |1 /2 | where 1 is the smallest eigenvalue and 2 is the next largest. I used the absolute value
Delaware - M - 426
Math 426/CISC 410 08F, All Sections R.J. Braun Homework 6 Solutions, Hints and AnswersProblem 2.7.1. We assume that the relative change in b is eps; that is, |b|/|b| =eps. This script will do the job. % Script: Prob2_7_1.m % Problem 2.7.1: verify re
Delaware - M - 426
Math 426/CISC 410 08F, All Sections R.J. Braun Homework 10 Solutions, Hints and AnswersProblem 4.6.2. (a) Assume that A is n-by-n and nonsingular. The SVD is A = U SV ; here S is a diagonal matrix with the singular values on the diagonal, and U and
Delaware - M - 353
University of Delaware Department of Mathematical Sciences Math 353 Engineering Mathematics III 05F R. J. Braun Homework 12: Due Wednesday, 11/16/05, 2:30pm1. Read 9.1,9.2,9.3,9.5. 2. 9.2, Problems 9.2.2(ab). Do h = 0.2 by hand; you may (and are en
Delaware - M - 428
Math 428 Numerical and Algorithmic Solution of Dierential Equations 08S Project 4. Due Friday, 5/16/05, 4:30pm. R. J. Braun Your name: In this project you will examine the numerical solution of the boundary value problem u + eu = 0, u(0) = u(1) = 0,
Delaware - M - 353
University of Delaware Department of Mathematical Sciences Math 353 Engineering Mathematics III 05F R. J. Braun Your name: Due date: 11/30/05, 5pm. Project 2: Solving a Nonlinear ODE IVP In this project you will study the solution of a nonlinear seco
Delaware - M - 353
University of Delaware Department of Mathematical Sciences Math 353 Engineering Mathematics III 05F R. J. Braun Homework 8: Due Tuesday, 10/11/05, 4:00pm1. Read 3.3-5. Reading 3.1,2 is optional; do so if you're not sure about all this matrix-vector
Delaware - M - 353
University of Delaware Department of Mathematical Sciences Math 353 Engineering Mathematics III 05F R. J. Braun Homework 7: Due Monday, 10/3/05, 5:00pm 1. Read 2.2, 2.3, 2.4. 2. 2.2, Problems 2.2.3(a,d), 2.2.11. 3. Apply the method of bisection 3 tim
Delaware - M - 426
qriter_testThe approximation to the smallest eigenvalue:ans = -0.059675578871905 1.564191203698703 1.987691412628296 2.349297531449158 3.409078620563635 -1.263166795003825 0.386479838574264 0.760280531365414 0.8920698056836
Delaware - M - 426
type Hw1_07f.m% Script for Hw 1, 07f. % Problem 3; display the output for second part of 11.2 of the Beginning% Matlab Problemsg = [1 2 3 4; 5 6 7 8; 9 10 11 12]h = [3 3 4 4; 5 5 6 6; 7 7 8 8]h &gt;= gg = hbigger = (g &gt;= h)g(bigger)g([0 0 0
Delaware - M - 426
type TestESeq% Script TestESeq.m% try approximation for exp(1) approx = zeros(1,15);format short e;for k = 1:15 approx(k) = (1+10^(-k)^(10^k);endabserror = abs(exp(1)-approx)%% make a nice table, like that from the basic matlab problem
Delaware - MATH - 611
A=[2 1 1 0; 4 3 3 1; 8 7 9 5; 6 7 9 8 ][L,U] = lufact(A);LUL*U - Ab = [1;2;3;4]y = L\bx = U\yA\bA = [ 1e-12 1; 1 1A = [ 1e-12 1; 1 1 ]norm(A)cond(A)
Delaware - MATH - 611
t = linspace(0,2*pi,200);A=[1 3;4 2]/4;for i=1:200, v=A*[cos(t(i);sin(t(i)]; plot(v(1),v(2),'o'), hold on, endginput(1)norm(ans)norm(A)
Delaware - MATH - 611
A = magic(8)A=A+A';eig(A)lam = poweriter(A);plot(lam,'-o')plot(520-lam,'-o')shgsemilogy(abs(520-lam),'-o')shg[x,y]=ginput(2)diff(log(y) / diff(x)exp(ans)eigeig(A)(ans(end-1)/ans(end)^2eig(A)lam=inviter(A,160);plot(lam,'-o')shglam=i
Delaware - MATH - 611
edit lufactA=[2 1 1 0; 4 3 3 1; 8 7 9 5; 6 7 9 8 ]A([1 3],:) = A([3 1],:)L1=eye(4); L1(2:4,1) = -A(2:4,1)/A(1,1)format ratL1A = L1*AP1=eye(4); P1([1 3],:)=P1([3 1],:);P2=eye(4); P2([2 4],:)=P1([2 4],:);P2*AP2=eye(4); P2([2 4],:)=P1([4 2],:)
Delaware - MATH - 611
m=5;A = toeplitz([1;-ones(m-1,1)],[1 zeros(1,m-1)]); A(:,m)=1[L,U]=lu(A)m=50;A=[2 1 1 0; 4 3 3 1; 8 7 9 5; 6 7 9 8 ];A = toeplitz([1;-ones(m-1,1)],[1 zeros(1,m-1)]); A(:,m)=1;x = rand(50,1);b = A*x;[L,U]=lu(A);x1 = U\(L\b);norm(x-x1)/norm(x
Delaware - MATH - 611
A = magic(5)A = A(:,1:3)[Q,R] = gs(A)Q'*Qans-eye(3)Q*R - A[Q,R]=qr(A,0)[Q,R]=qr(A)N = 40;N = 400;x = linspace(-1,1,N+1)';A = [ x.^0 x.^1 x.^2 x.^3 x.^4 ];[Q,R]=qr(A,0);plot(x,Q)
Delaware - MATH - 611
% MATLAB introduction2+23^4exp(1)log(1)sin(pi/2)exp(1i*pi)sqrt(-4)xpiformat longpiepseps=1e-100pix = 2sin(x)x = x+1v = [ 1, 2, 3, 4 ]v = [ 1; 2; 3; 4 ]v'A = [ 1, 2, 3; 10, 20, 30 ]vB = [ v, v, v ]-3*v-v - 2*v4*BA = [ 1, 2,
Delaware - MATH - 611
image(adam)axis equalshgsize(adam)Z = mean(double(adam),3);image(Z),axis equalshgcolormap(gray(256)shgZ = Z(101:700,201:700);image(Z),axis equalshgsize(Z)Z(1:10,1:10)A = blur(8,1.5)A = blur(12,2.5)A(5,:)'A = blur(12,.5)A(5,:)'A = b
Delaware - CIS - 361
The need for File SystemsNeed to store data and programs in files Must be able to store lots of data Must be nonvolatile and survive crashes and power outages Must allow multiple processes concurrent access Store on disks OS manages files in a file
Delaware - CIS - 361
Solaris Management Facility (SMF) - WorkshopGanesh Hiregoudar Renaud Manus OP/N1 RPE ApproachabilitySun MicrosystemsThanks! We would like to thanks the following engineers who participated in writing and delivering this SMF workshop. &gt; Jarod Nas
Delaware - CIS - 361
Memory ManagementOne of the most important OS jobs.Review memory hierarchy sizes, speed, costGrowing memories -&gt; growing programs need for swapping and paging The simplest way to use memory is to have one program in memory sharing it with the OS
Delaware - CIS - 361
ZFSNew filesystem and volume manager from Sun Open source after 5 years development First appeared in OpenSolaris, later in Solaris 10 6/06 (u2). Has been ported to FreeBSD. Also being including on Macs (Leopard*) Linux FUSE port Jeff Bonwick is a U
Delaware - CIS - 361
Process SynchronizationConsider two threads using and modifying a shared global variable. What problems could occur? Example: a bank account balance (a shared global variable) Balance $200 A: Deposit $10 B: Deposit $10,000Process SynchronizationA
Delaware - CIS - 361
ThreadsTraditional processes have one thread of control sequential program. Multiple threads of control are possible to get parallelism. Processes - own resources (resource grouping) - are scheduled for running (have states (Running, Ready, Blocked
Delaware - CIS - 361
CPU SchedulingScheduling processes (or kernel-level threads) onto the cpu is one of the most important OS functions. The cpu is an expensive resource so utilize it well. May have more than 1. With multiprogramming will have many processes (threads)
Delaware - CIS - 361
Solaris Zones: Operating System Support for Consolidating Commercial WorkloadsDaniel Price and Andrew Tucker Sun Microsystems, Inc. ABSTRACTServer consolidation, which allows multiple workloads to run on the same system, has become increasingly im
Delaware - CIS - 361
DeadlocksLots of resources can only be used by one process at a time. Exclusive access is needed. Suppose process A needs R1 + R2 and B needs R1 + R2. A gets R1, B gets R2 A waits for R2, B waits for R1 A and B can't continue without second resource
Delaware - CIS - 361
Zones - ContainersServer Consolidation Run multiple workloads on system Improve utilization of resources Reduce costs Run workloads in isolation Cannot observe others Security Isolation Running apps as different user not enough - privilege escalatio
Delaware - CIS - 361
Dining PhilosophersFive philosophers sit around a table with five forks and spaghetti to eat. Philosophers think for a while and they want to eat, only spaghetti, for a while. To eat a philosopher requires two forks, one from the left and one from r
Delaware - CIS - 361
The ShellWhat does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program executable and . run it. Other features: wildcards, pipes, redirection.ProcessesThe shell
Delaware - CIS - 361
Architecture BackgroundVon Neumann architecture - cpu = ALU + control unit - memory - devices Above connected with buses ALU carry out instruction, may have more than one execution unit Program counter memory address of next instruction to fetch.
Delaware - CIS - 361
Chapter 2 Process, Thread and Scheduling Soloris IPC1Outline Generic System V IPC Support Shared Memory System V Semaphores System V Message Queues POSIX IPC Signal Door2Generic System V IPC SupportModule TheCreationSystem V IPC
Delaware - CIS - 361
Chapter 2 Process, Thread and Scheduling - Scheduler Class and PriorityXIANG Yong xyong@tsinghua.edu.cnOutline Scheduling Class and Priority Dispatch Queues &amp; Dispatch Tables Thread Priorities &amp; Scheduling Turnstiles &amp; Priority Inheritance2
Delaware - CIS - 361
Operating Systems Update: Solaris, OpenSolaris, LinuxHarry J Foxwell, PhD OS AmbassadorSun Microsystems Federal, IncSolaris 10 Introduced in Jan 05, included:&gt; Containers: OS virtualization for isolation &gt; &gt; &gt; &gt; &gt;consolidation/utilization Obs
Delaware - CIS - 361
Name:UDel CISC361-010 Midterm, March 24, 2005Be sure that you read each question and understand what it is asking. No calculators permitted. 1. (10 pts) True/False (circle T for True, F for False) (a) T F - The C shell (csh) is derived from the Bo
Delaware - CIS - 361
Chapter 2 Process, thread, and scheduling - Solaris Multithreaded ProcessZhao Xia zhaoxia@os.pku.edu.cnOutline Introduction to Solaris Processes Multithreaded Process Model Proc tools2The Process ModelSolarisKernel is Multi-threadedK
Delaware - CIS - 361
Name:Show your work! Partial credit will be given, but only if you show your work!. Be sure that you read each question and understand what it is asking.UDel CISC361-010 Final, December 13, 20011. 10 pts True False a T F - Segmentation is just a
Delaware - CIS - 361
ZFSNew filesystem and volume manager from Sun Open source after 5 years development First appeared in OpenSolaris, later in Solaris 10 6/06 (u2). Has been ported to FreeBSD. Also being including on Macs in the works (Leopard*) Linux FUSE port Jeff B
Delaware - CIS - 361
UDel CISC361Study Operating System principles - processes, threads - scheduling - mutual exclusion - synchronization - deadlocks - memory management - file systems - security - networking (if time allows)Operating SystemsUse of Solaris - advanced
Delaware - CIS - 361
Zones - ContainersServer Consolidation Run multiple workloads on system Improve utilization of resources Reduce costs Run workloads in isolation Cannot observe others Security Isolation Running apps as different user not enough - privilege escalatio
Delaware - CIS - 361
Architecture BackgroundVon Neumann architecture - cpu = ALU + control unit - memory - devices Above connected with buses ALU carry out instruction, may have more than one execution unit Program counter memory address of next instruction to fetch.
Delaware - CIS - 361
UDel CISC361-010 Midterm, October 24, 2001Show your work. Partial credit will be given, but only if you show your work. Be sure that you read each question and understand what it is asking.Name:1.10 ptsa b c d e f g h i j 2.True False T F
Delaware - CIS - 105
CISC 105 Fall 2005Name Circle section: 18 19 20 21Midterm 110/07/05TAGeneral Instructions DO NOT PUT YOUR SSN ON ANYTHING! DO NOT WRITE YOUR NAME ON ANY PAGE EXCEPT THIS ONE! Turn off any noise making device, especially CELL PHONES. You m
Delaware - CIS - 105
CISC105 Spring 2007 Lab11 This lab is designed to help with your project. Youll write small programs that demonstrate things you are supposed to do in the project. In general, writing small test programs that demonstrate the concepts you want to emp
Delaware - CIS - 105
CISC105 Spring 2007 Lab06 CODE! If you are having difficulty writing programs, consider programming more often. Working on C every day makes a big difference, and leaving it alone, even just for a weekend, can result in backsliding. You are learning
Delaware - CIS - 181
CISC181 Spring 2008 Lab02 If you did not use the SunRays in CISC105 or 106, then see Lab00 from this directory. Lab00 also reviews compiling and running C+ programs on Unix. This lab should be a review of programming concepts you have seen before.
Delaware - CIS - 181
Project 2, CISC181 Spring 08Domain choice due by email (see below) April 14th Testing and makefile due Thursday April 24th midnight, paper Friday in class Due Sunday April 27th midnight, paper Monday in class AssignmentPlease read the online FAQ fo
Delaware - CIS - 181
CISC181 Fall 2008 Lab01 See Lab00 if you would like to review compiling and running C+ programs on Unix. Be sure to change your disk quota as I described in an email to you last week. Remember, never delete an email from your professor or TA. Thi
Delaware - CIS - 181
Every problem starts with the same picture. Given the code, modify the picture to show what happens. Be sure to struct Node { 1) cross out pointers that go away; int data; 2) add any new pointers, structs, or data; Node * next; 3) follow the code, no
Delaware - CIS - 181
CISC181 Fall 2006 Lab02 Write a program for each of the following problems. Be sure to save every separate program. All programs must be properly commented and indented (see Assignment Standards on the class website). Ask your TA for guidance. Read
Delaware - CIS - 181
CISC181 Fall 2006 Lab09 Write a program for each of the following problems. Be sure to save every separate program. All programs must be properly commented and indented (see Assignment Standards on the class website). Ask your TA for guidance. Feel
Delaware - CIS - 181
181 Quiz 4/11/08 10 pts. Name Section TA1. Show an expression that puts a pseudo-random number into x: int x;2. The C+ language builds on C by adding three features, according to our discussion in class: inheritance, polymorphism, and 1 2 3 4 5 6
Delaware - CIS - 181
181 Quiz 5/9/08 10 pts. Name Section TA Consider the class below which has functions for addition and multiplication. class Rat{ int num; int denom; public: Rat(int n = 0, int d = 1):num(n),denom(d){} Rat operator+(const Rat&amp; rhs); friend Rat operato
Delaware - CIS - 181
Every problem starts with the same picture. Given the code, modify the picture to show what happens. Be sure to struct Node { 1) cross out pointers that go away; int data; 2) add any new pointers, structs, or data; Node * next; 3) follow the code, no
Delaware - CIS - 181
CISC 181 Fall 2006First Midterm Learning Experience10/9/06NameLogin nameSection:TAGeneral Instructions DO NOT PUT YOUR SSN ON ANYTHING! DO NOT WRITE YOUR NAME ON ANY PAGE EXCEPT THIS ONE! Turn off any noise making device, especially