This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Attia, John Okyere. “Control Statements .” Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999 © 1999 by CRC PRESS LLC CHAPTER THREE CONTROL STATEMENTS 3.1 FOR LOOPS “ FOR ” loops allow a statement or group of statements to be repeated a fixed number of times. The general form of a for loop is for index = expression statement group X end The expression is a matrix and the statement group X is repeated as many times as the number of elements in the columns of the expression matrix. The index takes on the elemental values in the matrix expression. Usually, the ex pression is something like m:n or m:i:n where m is the beginning value, n the ending value, and i is the increment. Suppose we would like to find the squares of all the integers starting from 1 to 100. We could use the following statements to solve the problem: sum = 0; for i = 1:100 sum = sum + i^2; end sum For loops can be nested, and it is recommended that the loop be indented for readability. Suppose we want to fill 10by20 matrix, b, with an element value equal to unity, the following statements can be used to perform the operation. % n = 10; % number of rows m = 20; % number of columns for i = 1:n for j = 1:m b(i,j) = 1; % semicolon suppresses printing in the loop end end © 1999 CRC Press LLC © 1999 CRC Press LLC b % display the result % It is important to note that each for statement group must end with the word end . The following program illustrates the use of a for loop. Example 3.1 The horizontal displacement x t ( ) and vertical displacement y t ( ) are given with respect to time, t, as x t t y t t ( ) ( ) sin( ) = = 2 For t = 0 to 10 ms, determine the values of x t ( ) and y t ( ) . Use the values to plot x t ( ) versus y t ( ) . Solution : MATLAB Script % for i= 0:10 x(i+1) = 2*i; y(i+1) = 2*sin(i); end plot(x,y) Figure 3.1 shows the plots of x t ( ) and y t ( ) . © 1999 CRC Press LLC © 1999 CRC Press LLC Figure 3.1 Plot of x versus y. 3.2 IF STATEMENTS IF statements use relational or logical operations to determine what steps to perform in the solution of a problem. The relational operators in MATLAB for comparing two matrices of equal size are shown in Table 3.1 . Table 3.1 Relational Operators RELATIONAL OPERATOR MEANING < less than <= less than or equal > greater than >= greater than or equal == equal ~= not equal © 1999 CRC Press LLC © 1999 CRC Press LLC When any of the above relational operators are used, a comparison is done be tween the pairs of corresponding elements. The result is a matrix of ones and zeros, with one representing TRUE and zero FALSE. For example, if a = [1 2 3 3 3 6]; b = [1 2 3 4 5 6]; a == b The answer obtained is ans = 1 1 1 0 0 1 The 1s indicate the elements in vectors a and b that are the same and 0s are the ones that are different....
View
Full
Document
This note was uploaded on 12/19/2009 for the course ELECTRONIC elm 461 taught by Professor Oğuzkucur during the Spring '09 term at Gebze Institute of Technology.
 Spring '09
 Oğuzkucur

Click to edit the document details