Академический Документы
Профессиональный Документы
Культура Документы
Common mistakes, Do not use turkish letters in variable names Do not use turkish letters and blanks in m-file names
Do not use two relational operators in one expression, divide the expression and then combine it using logical operators such as and, or or xor etc
0<x<10 x>0 & x<10 incorrect correct
Initialization
Initialization
Initialization Input
Initialization Input
Results
Results
loop_variable<=finish
Default value for step: 1 The step can be negative If start = finish, the loop is executed once. Usually NOT a good idea to change the loop_variable inside the loop.
Statements
Example: testloop.m
% program to test a for loop
for i=1:10 disp(i) end >> testloop 1 2 3 . . 10 >> disp(i) 10
>> disp(i) 10
Example :
Assume we have series of natural numbers up to 100 and want to find the summation of these numbers. Use a for loop, using the loop variable as the index Loop variable: i At every step of the loop, we want to add the value corresponding to the index. Result variable: sum
% program to test a for loop sum=0; for i=1:100 sum=sum+i; end disp(sum) disp([result=,num2str(sum)])
Summation is done by addition of 1st elements, obtain partial result, add 2nd element, obtain partial result, etc. sum=sum + next value
. .
j
100 times
Use a for loop, using the loop variable as the index of the vector.
Loop variable:
At every step of the loop, we want to add the value corresponding to the index.
for j=1:100
S=sqrt(S) end
Result variable:
Operation done by taking the square root of the preceding result, 100 times S=
for
loop_variable= start:step:finish
IF loop_variable>finish
Assume 1000 values We want to stop the loop when we obtain enough preicison in the calculation What will happen if the precision is obtained after 10 calculations? The FOR loop will not stop until the 1000 .
IF loop_variable<=finish
Statements
While
logical_expression
IF logical_expression
FALSE
IF logical_expression
TRUE
Basic rules for while loop Statements Usually necessary to create your loop_ variable or counter.
Example: exloop1.m
% program test % program to test while loop
i=0; for i=1:10 disp(i) end Had to create a counter while i<10 i=i+1; disp(i)
>> exloop1
1 2 3 4 5 6 7 8 9 10 >> disp(i) 10
end
Example: exloop2.m
% program to test while loop
>> exloop2 9.5000 9.0000 8.5000 8.0000 7.5000 7.0000 6.5000 6.0000 5.5000 5.0000 4.5000 4.0000 3.5000 3.0000 2.5000 2.0000 1.5000 1 2.2513 2.1972 2.1401 2.0794 2.0149 1.9459 1.8718 1.7918 1.7047 1.6094 1.5041 1.3863 1.2528 1.0986 0.9163 0.6931 0.4055 0
x=10;
while x>1 x=x-0.5; y=log(x); disp([x,y]) end
Example: exloop3.m
% BAD while loop x=1; while x>=0
>> exloop3
This is an Infinite loop !!!! Need to stop the script manually !!!
x=x+0.5; y=sin(x);
end
CTRL C
>>
disp(END of program)
variable:
A>0
i=i+1 Inside the loop: Increment a counter to count how many value we entered
>> testloop
Value for A:2 Value for A:2 Value for A:2 Value for A:2 Value for A:0 2
i 1
1 i2
2/6
Want to see how many terms you need to obtain an error of 3x10-6. Need to define and use an error variable. err Need to be part of the logical expression Need to be updated during loop At every step of the loop, we want to Verify if test is true (err>3e-6)
Increment counter
i=i+1
N=333333
N=333333
disp(['N=',num2str(i)])
1! 2! 3! 4! .... 20!
Need a for loop for sums Need a for loop for factorials Calculate the factorial of element j Do the sum on all elements
i j F=F*j
S=0; for i=1:20 F=1; for j=1:i F=F*j; end S=S+F end disp(S)
S=S+F
BREAK Break terminates the execution of a for or while loop. Statements in the loop that appear after the break statement, are not executed.
x=1;
while x>=0 x=x+0.5; y=sin(x); if x>10000 break end end
In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.
CONTINUE Continue passes control to the next iteration of the for or while loop in which it appears, skipping any remaining statements in the body of the loop.
% Problem of division by 0
x=1;
for i= -10:10 if x==0 continue end y=1/x; end
Example :
Write a Matlab script to calculate following expression for an entered x value
1 1
1 1 1 1 1 1 x
Example :
Write a Matlab script to calculate mean and standard deviation of an input data set containing an arbitrary number of input values. Check to see if there is enough input data (N>1) to eliminate division by zero.
1 x N
N
x
i 1
2 i
N x ( x i ) 2
i 1 i 1
N ( N 1)
Break
Example :
Run the following loops and report the result
for ii=1:3
for jj=1:3
if jj==3 break; end product=ii*jj; disp([num2str(ii),*,num2str(jj),... =,num2str(product)]) end disp(end of inner loop);
1 * 1 = 1 1 * 2 = 2
end
disp(end of outer loop);