An Introduction to Programming and Numerical Methods in MATLAB
DisciplinaMatlab498 materiais • 2.121 seguidores
whos re* lists only the variables whose names start with re. Example 1.19 The following code\ufffd \ufffd \ufffd \ufffd clear all a = linspace(0,1,20); b = 0:0.3:5; c = 1.; whos gives the output Name Size Bytes Class a 1x20 160 double array b 1x17 136 double array c 1x1 8 double array Grand total is 38 elements using 304 bytes Here we have used the clear all command to remove all previously de\ufb01ned variables. To look at the size of one variable we can use the command length, for instance with the previous example length(a) will give the answer 20. We note that the command size(a) will give two dimensions of the array, that is 1.7 Accessing Elements of Arrays 23 in this case [1 20]; this will be particularly useful when we consider matrices in due course. 1.7 Accessing Elements of Arrays This is one of the most important ideas in MATLAB and other programming languages which is often misunderstood. Let us start by considering a simple array x = 0:0.1:1.;. The elements of this array can be recalled by using the format x(1) through to x(11). The number in the bracket is the index and refers to which value of x we require. A convenient mathematical notation for this would be xj where j = 1, · · · , 11. This programming notation should not be confused with x(j); that is x is a function of j. Let us consider the following illustrative example: Example 1.20 Construct the function f(x) = x2+2 on the set of points x = 0 to 2 in steps of 0.1 and give the value of f(x) at x = 0, x = 1 and x = 2. The code to construct the function is:\ufffd \ufffd \ufffd \ufffd x = 0:0.1:2; f = x.\u2c62+2; % Function at x=0 f(1) % Function at x=1 f(11) % Function at x=2 f(21) Note that the three points are not f(0), f(1) and f(2)! In this example we have noted that xj = (j \u2212 1)/10 and hence x1 = 0, x11 = 1 and x21 = 2. These three indices are the ones we have used to \ufb01nd the value of the function. In MATLAB f(j) the value of j refers to the index within the array rather than the function f(.) evaluated at the value j! Important Point 24 1. Simple Calculations with MATLAB The expression end is very useful at this point, since it can be used to refer to the \ufb01nal element within an array. In the previous example f(end) gives the value of f(21) since the length of f is 21. Example 1.21 We now show how to extract various parts of the array x.\ufffd \ufffd \ufffd \ufffd x = linspace(0,1,10); y = x(1:end); % Whole of x y = x(1:end/2); % First half y = x(2:2:end); % Even indices only y = x(2:end-1); % All but the last one 1.8 Tasks In this introductory chapter we shall give quite a few details (at least initially) concerning these suggested tasks. However, as the reader\u2019s grasp of the MAT- LAB syntax develops the tasks will be presented more like standard questions (the solutions are given at the back of the book in Appendix C). Task 1.1 Calculate the values of the following expressions (to \ufb01nd the MAT- LAB commands for each function you can use the Glossary, see for instance the entry for tan on page 386 or the help command, help tan). p(x) = x2 + 3x + 1 at x = 1.3, y(x) = sin(x) at x = 30\u25e6, f(x) = tan\u22121(x) at x = 1, g(x) = sin ( cos\u22121(x) ) at x = \u221a 3 2 . Task 1.2 Calculate the value of the function y(x) = |x| sinx2 for values of x = \u3c0/3 and \u3c0/6 (use the MATLAB command abs(x) to calculate |x|). Task 1.3 Calculate the quantities sin(\u3c0/2), cos(\u3c0/3), tan 60\u25e6 and ln(x +\u221a x2 + 1) where x = 1/2 and x = 1. Calculate the expression x/((x2 + 1) sinx) where x = \u3c0/4 and x = \u3c0/2. (If you are getting strange answers in the form 1.8 Tasks 25 of rationals you may well have left the format as rat, so go back to the default by typing format). Task 1.4 Explore the use of the functions round, ceil, floor and fix for the values x = 0.3, x = 1/3, x = 0.5, x = 1/2, x = 1.65 and x = \u22121.34. Task 1.5 Compare the MATLAB functions rem(x,y) and mod(x,y) for a va- riety of values of x and y (try x = 3, 4, 5 and y = 3, 4,\u22124, 6). (Details of the commands can be found using the help feature). Task 1.6 Evaluate the functions 1. y = x3 + 3x2 + 1 2. y = sinx2 3. y = (sinx)2 4. y = sin 2x + x cos 4x 5. y = x/(x2 + 1) 6. y = cos x1+sin x 7. y = 1/x + x3/(x4 + 5x sinx) for x from 1 to 2 in steps of 0.1 Task 1.7 Evaluate the function y = x x + 1x2 , for x = 3 to x = 5 in steps of 0.01. Task 1.8 Evaluate the function y = 1 x3 + 1 x2 + 3 x , for x = \u22122 to x = \u22121 in steps of 0.1. Task 1.9 (D) The following code is supposed to evaluate the function f(x) = x2 cos\u3c0x (x3 + 1)(x + 2) , 26 1. Simple Calculations with MATLAB for x \u2208 [0, 1] (using 200 steps). Correct the code and check this by evaluating the function at x = 1 using f(200) which should be \u22121/6.\ufffd \ufffd \ufffd \ufffd x = linspace(0,1); clear all g = x\u2c63+1; H = x+2; z = x.\u2c62; y = cos xpi; f = y*z/g*h Task 1.10 (D) Debug the code which is supposed to plot the polynomial x4\u22121 between x = \u22122 and x = 2 using 20 points. \ufb03 \ufb01 \ufb02 x = -2:0.1:2; c = [1 0 0 -1]; y = polyval(c,x); plot(y,x) Task 1.11 (D) Debug the code which is supposed to set up the function f(x) = x3 cos(x + 1) on the grid x = 0 to 3 in steps of 0.1 and give the value of the function at x = 2 and x = 3.\ufffd \ufffd \ufffd \ufffd x = linspace(0,3); f = x\u2c63.*cos x+1; % x = 2 f(2) % x = 3 f(End) 2 Writing Scripts and Functions 2.1 Creating Scripts and Functions With the preliminaries out of the way we now turn our attention to actually using MATLAB by writing a short piece of code. Most of the commands in this section have purposely been written so they can be typed at the prompt, >>. However, as we develop longer codes or ones which we will want to run many times it becomes necessary to construct scripts. A script is simply a \ufb01le containing the sequence of MATLAB commands which we wish to execute to solve the task at hand; in other words a script is a computer program written in the language of MATLAB. To invoke the MATLAB editor1 we type edit at the prompt. This editor has the advantage of understanding MATLAB syntax and producing automatic formatting (for instance indenting pieces of code as necessary). It is also useful for colour coding the MATLAB commands and variables. Both of these at- tributes are extremely useful when it comes to debugging code. The MATLAB editor also has the feature that once a piece of code has been run the values of variables can be displayed by placing the mouse close to the variable\u2019s loca- tion within the editor. This is extremely useful for seeing what is going on and provides the potential to identify where we might have made a mistake (for instance, if we had set a variable to be the wrong size). 1 You can of course make use of any other editor you have available on your computer. We have chosen to use the built-in MATLAB editor. Its implementation may di\ufb00er slightly from platform to platform. If you are unsure of its use try typing help edit at the MATLAB prompt. 28 2. Writing Scripts and Functions Example 2.1 We begin by entering and running the code: \ufb03 \ufb01 \ufb02 a = input(\u2019First number \u2019); b = input(\u2019Second number \u2019); disp([\u2019 Their sum is \u2019 num2str(a+b)]) disp([\u2019 Their product is \u2019 num2str(a*b)]) This simple code can be entered at the prompt, but that would defeat our purpose of writing script \ufb01les. We shall therefore create our \ufb01rst script and save it in a \ufb01le named twonums.m. To do this, \ufb01rst we type edit at the MATLAB prompt to bring the editor window to the foreground (if it exists) or invoke a new one if it doesn\u2019t. Along the base of the typing area are a set of tabs. These allow you to switch between multiple codes you may be simultaneously working on. Since this is our \ufb01rst use of the editor, MATLAB will have given this code the default name Untitled.m.