MATLAB-Lecture 8 - Loops
MATLAB-Lecture 8 - Loops
Ex:
clear all !!!!
clc 1*1=1
1*2=2
for x=1:3 ====
display('! ! ! !')
!!!!
for y=1:2
2*1=2
fprintf('%g * %g = %g\n',x,y,x*y)
end 2*2=4
display('= = = =') ====
end !!!!
display(' SON ') 3*1=3
3*2=6
====
SON
>>
for x=1:10
display(' ')
for y=1:10
fprintf('%g * %g = %g\n',x,y,x*y)
end
end
display(' SON ')
- Calculating Factorials
-What Is Factorial?
1
Spring DERS 8
n!=n×(n−1)!
n!=1.2.3.4…(n-1).n
0!=1
1!=1
5!=1.2.3.4.5 = 120
0!=1
___________________
1!=1.1 =0!.1
2!=1.1.2 =1!.2
3!=1.1.2.3 =2!.3
4!=1.1.2.3.4 =3!.4
5!=1.1.2.3.4.5 =4!.5
. .
. .
. .
n!= =(n-1)!.n
clc
clear all
fk=1;
n=input('Faktöriyeli hesaplanacak sayı= ');
for i=1:n
fk=fk*i;
% disp([num2str(i),' adımda fk değeri ',num2str(i),...
% '!= ',num2str(fk)])
end
fprintf('\n\n')
disp([num2str(n), '!= ', num2str(fk)])
5!= 120
>>
2
Spring DERS 8
clc
clear all
fk=1;
n=input('Faktöriyeli hesaplanacak sayı= ');
for i=1:n
fk=fk*i;
disp([num2str(i),' adımda fk değeri ',num2str(i),...
'!= ',num2str(fk)])
end
fprintf('\n\n')
disp([num2str(n), '!= ', num2str(fk)])
5!= 120
>>
3
Spring DERS 8
Ex1:
10!
f = factorial(10)
f = 3628800
Ex2:
22!
format long
f = factorial(22)
f=
1.124000727777608e+21
n = [0 1 2; 3 4 5];
f = factorial(n)
f=
1 1 2
6 24 120
Vector operations
4
Spring DERS 8
Warnings
• If the argument of the of the sum or prod function is a matrix, then
the sum or product is taken over each column.
Apply the MATLAB functions sum and prod to the matrix
A = [1 2 3; 4 5 6]
sum calculates the sum of each column producing the row vector [5 7 9]
prod calculates the product of each column producing the row vector
[4 10 18]
>> A = [1 2 3; 4 5 6]
>> sum(A)
>> prod(A)
Use help sum or help prod to get more information.
• Remember to use semicolons ; to suppress unwanted output during
the for-loop.
Self-test Exercise
Write a MATLAB for-loop to calculate the first 20 Fibonacci numbers: F 1 =
1; F2 = 1;, Fn = Fn-1 + Fn-2 for n = 3,...,20, storing the results in a vector F.
Answer:
F(1)= 1; F(2) = 1;
for n = 3:20
F(n) = F(n-1) + F(n-2);
end
Use the mouse to select the text between the word "Answer" and here to
see the answer.
Summary
MATLAB uses for loops to execute a group of statements several times.
Often a for loop can be replaced by a vector operation.
Follow the steps below to write a program to calculate the given problem.
d) Print out the sin(x) value from Matlab’s function and sind(x) for
comparison.
5
Spring DERS 8
f) Analyze the Workspace. You will see that all the variables are arrays of
1x1 .
clear
clc
x = input('Açı değeri (derece) (x): ' );
n = input('İterasyon sayısı (n): ' );
6
Spring DERS 8
Using sum:
sum(x) gives the sum of array x.
x= y=
1 3 5 7 9 1 2 3 4 5
ans = ans =
25 15
>> >>
Let’s write the above program by using sum(x) and writing variable y as
an array. It’s the most practical way of writing MATLAB programs. Because
in MATLAB, the variables do not need to change size.
clear
clc
x = input('Açı değeri (derece) (x): ' );
n = input('İterasyon sayısı (n): ' );
a=x*pi/180;
% y = zeros(1,n);
for i = 0:n
y(i+1) = (-1)^i*x^(2*i+1)/factorial(2*i+1);
end
top=sum(y);
7
Spring DERS 8
Algorithm:
clc
clear all
8
Spring DERS 8
clc
clear all
continue command;
continue passes control to the next iteration of a for or while loop. It skips
any remaining statements in the body of the loop for the current iteration.
The program continues execution from the next iteration.
continue applies only to the body of the loop where it is called. In nested
loops, continue skips remaining statements only in the body of the loop in
which it occurs.
9
Spring DERS 8
Ex-1:
clc
clear all
n=input('Beşden büyük bir sayı giriniz :');
for x=1:n
y=x^2;
if x>5, break, end
if x==2, continue, end
disp(['x= ',num2str(x),' y= ',num2str(y)])
end
disp('/////')
Ex-2:
clc
clear all
n=input('10 dan büyük bir sayı giriniz :');
for x=1:0.5:n
y=x^2;
if x>=12, break, end
if x>=3 && x<=9, continue, end
disp(['x= ',num2str(x),' y= ',num2str(y)])
end
disp('/////')
10