MATLAB Lecture - 8 2018
MATLAB Lecture - 8 2018
MATLAB Lecture - 8 2018
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)
2*2=4
end
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
n!=1.2.3.4…(n-1).n
0!=1
1!=1
5!=1.2.3.4.5 = 120
1
2017-18 Spring DERS 8
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
>>
clc
clear all
fk=1;
n=input('Faktöriyeli hesaplanacak sayı= ');
2
2017-18 Spring DERS 8
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
>>
Ex1:
3
2017-18 Spring DERS 8
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
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.
4
2017-18 Spring DERS 8
Self-test Exercise
Write a MATLAB for-loop to calculate the first 20 Fibonacci numbers: F 1 =
1; F 2 = 1;, F n = F n-1 + F n-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 replace by a vector operation.
d) Print out the sin(x) value from Matlab’s function and sind(x) for
comparison.
5
2017-18 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): ' );
Using sum:
sum(x) gives the sum of array x.
x= y=
1 3 5 7 9 1 2 3 4 5
ans = ans =
6
2017-18 Spring DERS 8
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
2017-18 Spring DERS 8
reserved in the workspace. In each iteration, the space is updated. This can
cause loss of time.
Run the program by uncommenting line 6 (% y = zeros(1,n);) as (y =
zeros(1,n);). There will not be any warning when you define the size.
Algorithm:
clc
clear all
clc
8
2017-18 Spring DERS 8
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.
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
9
2017-18 Spring DERS 8
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