Lab IAT 04
Lab IAT 04
Claudia Zoccarato
E-mail: [email protected]
June, 07 2017
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
Functions in MATLAB
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
FUNCTION - Example
1 f u n c t i o n [ A , p , d]= r e c t a n g l e ( a , b )
2 % C a l c u l a t i o n o f a r e a , p e r i m e t e r and d i a g o n a l o f
3 % a r e c t a n g l e knowing t h e s i d e a and b
4 % Input : rectangle sides (a , b)
5 % Output : a r e a (A) , p e r i m e t e r ( p ) e d i a g o n a l (D) o f a
rectangle
6 %
7 A = a ∗b ;
8 p= 2 ∗ ( a+b ) ;
9 d = s q r t ( aˆ2+b ˆ 2 ) ;
10 end
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
Solution of non linear equations
Example
Problem
Implement a MATLAB program to
solve the following non linear
equation. The aim is at finding
ξ ∈ R such that ξ is a root of the
function f (x):
f (ξ) = 0 f (x) = x2 − 3x + 2
The solutions are: ξ1 = 1 e ξ2 = 2.
Given x > 1.5 the problem has one
solution, ξ = 2.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
The fixed point iteration method
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
The fixed point iteration method - Algorithm
1: Choose x0
2: while convergence is not reached
3: xk+1 = g(xk )
4: dk+1 =|xk+1 − xk |
5: end while
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
EXAMPLE
To solve the non linear equation f (x) = 0: x2 − 3x + 2 = 0, use the following fixed
point functions
x2 + 2
g1 (x) =
3
√
g2 (x) = 3x − 2
.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
EXERCISE - fixed point iteration
1 Impement the fixed point iteration method in MATLAB (using a script) to solve
the equation f (x) = 0 with f (x) = x2 − 3x + 2 = 0. Apply the method the fixed
2 √ 2
−2
point functions g1 (x) = x 3+2 , g2 (x) = 3x − 2 and g3 (x) = x2x−3 .
2 Plot the functions g1 (x), g2 (x) e g3 (x) in the interval [1.4, 3.0] and verify that the
problem has one solution.
3 The while loop is repeated until convergence. The termination criteria has to be
defined such that dk+1 > toll and iter < itmax
4 Estimate the convergence factors with two methods: M 1 = dk+1 /dk and
0
M 2 = |g (xk )|
5 Use the vector structure to store dk .
6 Plot the convergence profile of the method: plot dk vs iterations in a semi-log
scale plot. Use the commandsemilogy: semilogy is the same as plot, except a
logarithmic (base 10) scale is used for the Y-axis.
7 Display the results in a table using the command fprintf :
# Iteration k | xk | g(xk ) | dk
1 close all
2 clear
3 % F i x e d−p o i n t i t e r a t i o n method t o s o l v e a non l i n e a r e q u a t i o n
4 % Plot of the f un ct ion in the i n t e r v a l
5 f i g u r e (1)
6 f = @( x ) x .ˆ2−3∗ x +2;
7 x = linspace (1.5 ,2.5 ,100) ;
8 y = l i n s p a c e (0 ,0 ,100) ;
9 plot (x , f (x) ,x , y) ;
10 % Input data
11 t o l l = 10ˆ −10;
12 itmax = 100;
13 x0 = 1 . 6 ;
14 % Variable i n i t i a l i z a t i o n
15 x o l d = x0 ; % S e t i n i t i a l v a l u e x0
16 dk ( 1 ) = 2 . 0 ∗ t o l l ; % S e t i n i t i a l e v a l u e o f dk
17 i t e r = 1; % I n i t i a l i z e the i t e r a t i o n counter
18 ... % Define the f un cti on g ( x )
19 ... % Define the f i r s t d e r i v a t i v e g ’ ( x )
20 % C a l l the fu nc ti on f i x e d point i t e r a t i o n
21 [ xknew , i t e r , dk ]= f i x e d p o i n t ( g , g1 , x0 , i t m a x , t o l l )
22 % Plot of the convergence p r o f i l e
23 f i g u r e (2)
24 ...
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
The Newton-Raphson method - Algorithm
1: Choose x0
2: while convergence is not reached
3: xk+1 = xk − f (xk )/f 0 (xk )
4: dk+1 =|xk+1 − xk |
5: end while
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
EXERCISES
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
EXERCISES
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017