BAHCESEHIR UNIVERSITY
MAT3012 NUMERICAL ANALYSIS
(LAB APPLICATOINS)
Week-3, Fall
Resch. Asst. Aysun Soysal
LAB-3 OBJECTIVES
Structured Programming (continue with week-2)
• Examples of if conditional statement
• For loop
• While loop
2
STRUCTURED PROGRAMMING
4.1 Conditional If Statement
Ex1. Write a matlab function ‘area’ that accepts the values 𝑎, 𝑏, 𝑐 as inputs and returns the value of
the area as output. If the inputs are not positive or triangle inequality does not hold, then the
function should display a warning message.
Recall. The area of the triangle with sides 𝑎, 𝑏, and 𝑐 is given by
𝐴= 𝑢(𝑢 − 𝑎)(𝑢 − 𝑏)(𝑢 − 𝑐)
𝑎+𝑏+𝑐
where 𝑢 = .
2
3
Solution.
function [ A ] = area( a,b,c )
if (a<=0)||(b<=0)||(c<=0)
fprintf('Side of triangle should have positive length. Try again!\n')
elseif (a+b<=c)||(a+c<=b)||(b+c<=a)
fprintf('Triangle inequality is not satisfied. Try again!\n')
else
u=(a+b+c)/2;
A=sqrt(u*(u-a)*(u-b)*(u-c));
end
end
4
>> area(-3,4,5)
Side of triangle should have positive length. Try again!
>> area(2,3,6)
Triangle inequality is not satisfied. Try again!
>> area(3,4,5)
ans =
5
Ex2. Write down a Matlab function ‘quadratic’ that accepts the values 𝑎, 𝑏, 𝑐 as inputs and
calculates the real roots of the quadratic equation.
Recall. For the quadratic equation
𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0
i. The equation does not have a real root if ∆= 𝑏 2 − 4𝑎𝑐 < 0
−𝑏
ii. The equation has only one real root 𝑥 = if ∆= 0
2𝑎
−𝑏+ ∆ −𝑏− ∆
iii. The equation has two real roots 𝑥1 = and 𝑥2 = if ∆> 0.
2𝑎 2𝑎
6
Solution.
function [ ] = quadratic( a,b,c )
if a==0
fprintf('Equation is not quadratic. Try again!\n')
return
end
delta=b^2-4*a*c;
if delta<0
fprintf('Quadratic equation does not have real roots.\n')
elseif delta==0
x=-b/(2*a);
fprintf('Quadratic equation has only one real root x=%6.5f\n',x)
else
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
fprintf('Quadratic equation has two real roots x1=%6.5f, x2=%6.5f\n',x1,x2)
end
end 7
>> quadratic(0,4,5)
Equation is not quadratic. Try again!
>> quadratic(1,4,4)
Quadratic equation has only one real root x=-2.00000
>> quadratic(1,-1,-6)
Quadratic equation has two real roots x1=3.00000, x2=-2.00000
8
4.2. Foor Loop
▪ For is used to repeat statement a specific number of times.
▪ Syntax:
for index=initial value:final value
Statements
end
9
Ex1. Calculate the sum
12 + 22 + 32 + ⋯ + 1002 .
10
Solution.
I. Way:
function [ S ] = summation( n )
S=0;
for k=1:n
S=S+k^2;
end
end
>> summation(100)
ans =
11
338350
II. Way.
>> k=1:100;
>> S=sum(k.^2) % sum is a built-in fuction in MATLAB
S=
338350
12
Ex2. The Fibonacci sequence is given by
𝑓1 = 0, 𝑓2 = 1, 𝑎𝑛𝑑 𝑓𝑛 = 𝑓𝑛−1 + 𝑓𝑛−2
𝑓𝑛 5 −1
for 𝑛 = 3,4,5, … . Test whether the ratio approaches the golden ratio .
𝑓𝑛+1 2
13
Solution.
F(1)=0;
F(2)=1;
for n=3:20
F(n)=F(n-1)+F(n-2);
end
for n=1:19
RF(n)=F(n)/F(n+1);
GL(n)=(sqrt(5)-1)/2;
end
plot(1:19,RF,'--bo')
xlabel('n')
hold on
plot(1:19,GL,'-r')
14
15
4.3 While Loop
▪ While is used to repeat statements an indefinite number of times until some logical condition
is satisfied.
▪ Syntax:
while expression
Statements
end
16
Ex. What is the greatest value of 𝑛 that satisfies
12 + 22 + 32 + ⋯ + 𝑛2 < 100.
17
Solution.
S=0;
n=0;
while S+ (n+1)^2 < 100
n=n+1;
S=S+n^2;
end
n
>> greatest
n=
6 18
THANK YOU FOR YOUR ATTENTION !
19