0% found this document useful (0 votes)
15 views14 pages

Lab IAT 04

This document discusses numerical methods for solving nonlinear equations, including fixed point iteration and Newton-Raphson methods. It provides MATLAB code examples and exercises for implementing these methods to solve specific equations. The exercises involve implementing the methods in MATLAB scripts and functions, plotting results, and analyzing convergence properties.

Uploaded by

igorkapganglink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Lab IAT 04

This document discusses numerical methods for solving nonlinear equations, including fixed point iteration and Newton-Raphson methods. It provides MATLAB code examples and exercises for implementing these methods to solve specific equations. The exercises involve implementing the methods in MATLAB scripts and functions, plotting results, and analyzing convergence properties.

Uploaded by

igorkapganglink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to Numerical Methods and MATLAB

Programming for Engineers


Lecture 4: Fixed point and Newton-Raphson

Claudia Zoccarato

E-mail: [email protected]

June, 07 2017

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
Functions in MATLAB

A function takes some input arguments, perform the calculations with


these inputs and gives back the output solution.
A function is a list of commands with optional inputs and outputs.
The variables of a function are local: they are eliminated from the
workspace after the function has been run.
The function syntax is the following:
>> function[output arguments]=functionname(input arguments)
The input and output arguments are separated by commas.
The name of the function MUST be the same of the file name .m
used to save the function.

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

To run the function from the commmand window:


>> [A,p,d]=rectangle(3,4) + ENTER
The functions can be used insed the scripts (it is important to save the function in
the same folder where the script is saved).
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
Solution of non linear equations

Given a function f (x), find the solution of f (x) = 0 with x belonging to


the interval [a, b].
1 Bisection method (lecture 03)
2 Fixed point iteration method (lecture 04)
3 Newton-Raphson method (lecture 04)
4 Secant method (lecture 05)

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

The non linear equation can be written as an equivalent fixed point


iteration problem. The aim is at finding ξ ∈ R such that ξ is the fixed
point of the function g(x): g(ξ) = ξ.
The fixed point iteration method allows to built the succesion
x0 , x1 , x2 , . . . converging to the solution ξ. Given x0 , the method is the
following
xk+1 = g(xk )
.

The convergence condition is the following:


|g 0 (ξ)| < 1
x0 is close enough to ξ

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

8 Comment the different results obtained with g1 , g2 and g3 .


Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
this is part of the solution ...
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 % The w h i l e Loop
21 while ( termination condition )
22 i t e r = i t e r + 1; % Update t h e i t e r a t i o n c o u n t e r v a l u e
23 xknew = . . . % Compute t h e new v a l u e o f xk
24 dk ( i t e r ) = . . . % Compute t h e v a l u e o f dk
25 M1 = . . . % Compute t h e c o n v e r g e n c e f a c t o r M1
26 M2 = . . . % Compute t h e c o n v e r g e n c e f a c t o r M2
27 x o l d = xnew ; % Update t h e v a l u e x o l d
28 ... % Print the r e s u l t s
29 end
30 % Plot of the convergence p r o f i l e
31 f i g u r e (2)
32 ...
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
EXERCISE
1 In the previous exercize, use a function to solve the fixed-point iteration method.
This is part of the solution ...

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

1 Implement a script in MATLAB to solve the equation f (x) = 0 with


f (x) = ln(x + 2) − 2x in the interval ] − 2, 2] by using the
Newton-Raphson method. Chose as initial approximation x0 = −0.5.
Plot the convergence profile, verify the order of convergence of the
method and estimate the convergence factor. Then, implement a
function which solve the Newton-Raphson method (HINT: look back
to fixed point iteration method).
2 Implement a script in MATLAB to find the root ξ = 1 of the
polynomial p(x) = x(x − 1)3 in the interval [−2, 2] using the
Newton-Raphson with the initial point x0 = 2.0. Plot the convergence
profile and discuss the result. Modify the method in order to improve
the convergence and solve the problem again. What do you notice?

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017
EXERCISES

3 Given the non linear equation f (x) = 0 con


f (x) = arctan(7(x − π/2)) + sin((x − π/2)3 ) in the interval ] − 1, 6]. Plot the
function in the given interval.Implement a script in MATLAB to find the root ξ
using the Newton-Raphson with the initial point x0,1 = 0.5. Then, repeat the
calculation starting from x0,2 = 4.0.
4 Implement a script in MATLAB to find the roots ξ1 e ξ2 of the polynomial
p(x) = x3 − 4x2 + 5x − 2 using the Newton-Raphson. Start from x0 = 2.5 and
discuss the result. Change the starting point in x0 = 0.5 and verify the order of
convergence of the method.
5 Given the function f (x) = x3 + 4xcos(x) − 2 in the interval [0, 2]. Find an
approximate solution of the equation f (x) = 0 with the bisection method,
Newton-Raphson method and the fixed-point iteration method using the function
g(x) = (2 − x3 )/(4cos(x)). Plot in a same figure the profiles of convergence of
the methods.

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 07/06/2017

You might also like