An interactive introduction to MATLAB
4.2the if-else statement 51 The if-else statement (http://www.eng.ed.ac.uk/teaching/courses/matlab/unit04/if-else- statement.sht ...
52 decision making Self Test Exercise: The if-else statement Evaluate the following expressions without usingMATLAB. Which of t ...
4.2the if-else statement 53 †What will the following code print? 1 a = 10; 2 if a ~= 0 3 disp('a is not equal to zero') 4 end † ...
54 decision making Exercise 7: Decision making Write your own script files to solve the following problems: Write a script file ...
4.2the if-else statement 55 Exercise 7: Decision making (continued) (continued) The friction factor,f, is calculated as: f= ...
...
5 loops Loops are another way of altering the flow of control in your program, and provide methods for repeatedly executing comm ...
58 loops Indenting for readability It is good practice to indent the body of loops for readability in your script files.MATLABwi ...
5.1for loops 59 The for loop (http://www.eng.ed.ac.uk/teaching/courses/matlab/unit05/for-loop.shtml) Self Test Exercise: for loo ...
60 loops 15 n = input('Enter the number of terms to sum: '); 16 my_sum = 0; 17 r = 0.5; 18 for m = 0:n 19 my_sum = my_sum + r^m; ...
5.2while loops 61 Comments: Line 1 contains thewhilecommand, followed by a condition e. g.x>10. This means as long as the co ...
62 loops Lines 3–4 contain the body of the loop, in this case the value of the loop counter variablexis printed. Then the value ...
5.2while loops 63 What values will this code print? 1 a = 1; 2 n = 1; 3 while a < 100 4 a = a*n 5 n = n + 1; 6 end Listing ...
64 loops On Line 20 the summation is performed, and on Line 21 the loop counter is incremented. Which loop to use: for or whil ...
5.2while loops 65 Exercise 8: Loops Write your own scripts to perform the following tasks: a) Aforloop that multiplies all even ...
66 loops Additional Exercises You should now attempt questions from Chapter C.5. Advanced Topic If you are interested, read abou ...
a advanced topic: the switch statement Theswitchstatement inMATLABis similar to theif,else, andelseif statements, and provides a ...
68 advanced topic: the switch statement Listing A.2: computer_test.m - Script to test type of computer MATLAB is running on 1 % ...
b advanced topic: vectorisation To make yourMATLABcode run faster it is important to vectorise, where possible, the algorithms t ...
70 advanced topic: vectorisation 9 end 10 toc 11 Elapsed time is 6.876476 seconds. Comments: On Line 1 therandfunction is used ...
«
1
2
3
4
5
6
»
Free download pdf