Yet you would be surprised how many beginners preferring to rush straight to

Yet you would be surprised how many beginners

This preview shows page 21 - 28 out of 37 pages.

Yet you would be surprised how many beginners, preferring to rush straight to the computer, start with step 2 instead of step 1
Image of page 21
It is well worth developing the mental discipline of structure- planning your program first. You could even use cut and paste to plan as follows: 1. Type the structure plan into the Editor (each line preceded by % as shown above). 2. Paste a second copy of the plan directly below the first. 3. Translate each line in the second copy into a MATLAB statement, or statements (add % comments as in the example below). 4. Finally, paste all the translated MATLAB statements into the Command Window, and run them. 5. If necessary, go back to the Editor to make corrections, and repaste the corrected statements to the Command Window (or save the program in the Editor as an M-file and execute it).
Image of page 22
You might like to try this as an exercise, before looking at the final program, which is as follows: % Vertical motion under gravity g = 9.8; % acceleration due to gravity u = 60; % initial velocity (meters/sec) t = 0 : 0.1 : 12.3; % time in seconds s = u * t - g / 2 * t .ˆ 2; % vertical displacement in meters plot(t, s), title( ’Vertical motion under gravity’ ), ... xlabel ( ’time’ ), ylabel( ’vertical displacement’ ), grid disp ( [t’ s’] ) % display a table
Image of page 23
Note the following points: Anything in a line following the percentage symbol % is ignored by MATLAB and may be used as a comment (description). The statement t = 0 : 0.1 : 12.3 sets up a vector. The formula for s is evaluated for every element of the vector t , making another vector. The expression t.ˆ2 squares each element in t. This is called an array operation , and is different to squaring the vector itself, which is a matrix operation, as we shall see later. More than one statement can be entered on the same line if the statements are separated by commas. A statement or group of statements can be continued to the next line with an ellipsis of three or more dots ... The statement disp (*t’ s’+) first transposes the row vectors t and s into columns, and constructs a matrix from these two columns, which is then displayed.
Image of page 24
You might want to save the program under a helpful name, like throw.m if you think you might come back to it. In that case, it would be worth keeping the structure plan as part of the file; just insert % symbols in front of each line of the structure plan. This way, the structure plan reminds you what the program does when you look at it again after some months. Note that you can use the context menu in the Editor to Comment/Uncomment a block of selected text.
Image of page 25
Operators, expressions and statements Any program worth its salt actually does something. What it basically does is to evaluate expressions , such as u*t - g/2*t. ˆ 2 and to execute (carry out) statements, such as balance = balance + interest MATLAB has been described as an expression based language . It interprets and evaluates typed expressions. Expressions are constructed from a variety of things, such as numbers, variables, and operators.
Image of page 26