tailieunhanh - Lecture 8 Loops for repetitive computations

The for loop is a MATLAB construct that allows a sequence of MATLAB statements to be executed more than once. The for loop repeats a block of commands for a specified number of times; the specified number is established before the loop is executed. | Lecture 8 Loops for repetitive computations The for loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier. Concept of the for loop The for loop is a MATLAB construct that allows a sequence of MATLAB statements to be executed more than once. The for loop repeats a block of commands for a specified number of times; the specified number is established before the loop is executed. Construction of for loops The for loop construct: for index = [range of values in array format] command #1 command #2 command #3 . end A simple example _ Note: After executing this script, change the steps n = 1:5 to n = 1:.5:5 and execute the script to show that n does not have to be an integer. The output should look like: The value of n is now 1 The value of n is now 2 The value of n is now 3 The value of n is now 4 The value of n is now 5 Hands on vector_1 contains the values of n, .: vector_1 = [1 2 3 4 5] vector_2 contains the squares of n, .: vector_2 = [1 4 9 16 25] You can use a loop to define, element by element, a vector; this illustrates how a for loop works. Try the following exercise: for n=1:5 for n=1:5 fprintf('The fprintf('The value of n is now % value of n is now % d d \ \ n',n n',n ); ); vector_1(n)=n; vector_1(n)=n; vector_2(n)=n^2; vector_2(n)=n^2; end end Let us create a conversion chart Degrees-to-Radians Create a table that converts angles from degrees to radians, from 0 to 360 degrees in increments of 10 degrees. Inputs and Outputs Input: Define an array of angles in degrees. Output: Display a table of angles in degrees and the corresponding values in radians. The conversion formula is: The conversion table code Hands on Create a table that illustrates the relationship between temperature in degrees Celsius and the corresponding temperature in degrees Fahrenheit, from -40 to 100 degrees C in increments of 5 degrees. The conversion formula is as follows: Check the . | Lecture 8 Loops for repetitive computations The for loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier. Concept of the for loop The for loop is a MATLAB construct that allows a sequence of MATLAB statements to be executed more than once. The for loop repeats a block of commands for a specified number of times; the specified number is established before the loop is executed. Construction of for loops The for loop construct: for index = [range of values in array format] command #1 command #2 command #3 . end A simple example _ Note: After executing this script, change the steps n = 1:5 to n = 1:.5:5 and execute the script to show that n does not have to be an integer. The output should look like: The value of n is now 1 The value of n is now 2 The value of n is now 3 The value of n is now 4 The value of n is now 5 Hands on vector_1 contains the values of n, .: vector_1 = [1 2 3 4 5] vector_2 contains .