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

Lab IAT 05

This document summarizes the secant method for finding the roots of nonlinear equations. It discusses writing data to text files in MATLAB, lists the steps of the secant method algorithm, provides pseudocode for implementing the method in a while loop, and includes an example MATLAB script and function to apply the secant method to solve a sample equation. The example calculates the root iteratively until convergence within a specified tolerance, records the results to a text file, and plots the convergence profile.

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 views9 pages

Lab IAT 05

This document summarizes the secant method for finding the roots of nonlinear equations. It discusses writing data to text files in MATLAB, lists the steps of the secant method algorithm, provides pseudocode for implementing the method in a while loop, and includes an example MATLAB script and function to apply the secant method to solve a sample equation. The example calculates the root iteratively until convergence within a specified tolerance, records the results to a text file, and plots the convergence profile.

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/ 9

Introduction to Numerical Methods and MATLAB

Programming for Engineers


Lecture 5: The secand method

Claudia Zoccarato

E-mail: [email protected]

June, 12 2017

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Write data to text file

In the previous lectures, we used the function fprintf to display the


results on the screen. The same function can be used to write our result in
a TEXT FILE.
Open the TEXT FILE:
fid = fopen(’filename’)
fid is a integer scalar variable that identifies the TEXT FILE.
Options:
fid = fopen(’filename’,permission)
permesso: ’w’: writing (to overwrite existing data)
permesso: ’a’: append (to write data starting from the end of the
file)
Write to TEXT FILE:
fprintf(fid,’formats’,variables)
Close the TEXT FILE:
fclose(fid)
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/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 - 12/06/2017
The secant method

The secant method is a root-finding algorithm that uses a succession of


roots of secant lines to better approximate a root of a function f . The
secant scheme is the following:

f (xk )
xk+1 = xk −
mk
f (xk )−f (xk−1 )
where mk = (xk −xk−1 )

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
The secant method - Pseudocode

1: Choose x−1 and x0


2: while convergence is not reached
f (x )−f (x )
3: mk = (xkk −xk−1k−1)
4: xk+1 = xk − f (xk )/mk
5: dk+1 =|xk+1 − xk |
6: end while

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

Implement the secant method in MATLAB to solve the equation f (x) = 0 with
f (x) = x3 − 4x2 + 5x − 2 = 0 using x−1 = 2.5 and x0 = 2.4. The function
secant must be implemented and call from the script.
Plot the function f (x) in the interval [0, 2.5].
The while loop is repeated until convergence. The termination criteria has to be
defined such that dk+1 > toll and iter < itmax
Estimate the convergence factors
Use the vector structure to store dk .
Plot the convergence profile of the method. Display the results in a table using the
command fprintf :
# Iteration k | xk | f (xk ) | dk

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Solution - SCRIPT
1 close all
2 clear
3 % The s e c a n t 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 %
5 % Plot of the f un ct ion
6 f i g u r e (1)
7 f = @( x ) x .ˆ3−4∗ x .ˆ2+5∗ x −2;
8 x = linspace (0 ,2.5 ,100) ;
9 y = l i n s p a c e (0 ,0 ,100) ;
10 plot (x , f (x) ,x , y) ;
11 % Input data
12 t o l l = 10ˆ −8;
13 itmax = 100;
14 x0 = 2 . 5 ;
15 x1 = 2 . 4 ;
16 % Open t h e t e x t f i l e t o w r i t e t h e r e s u l t s
17 f i d = f o p e n ( ’ r e s u l t s . d a t ’ , ’w ’ ) ;
18 % C a l l the fu nc ti on ’ secant ’
19 [ xnew , i t e r , dk ] = s e c a n t ( f , x0 , x1 , i t m a x , t o l l , f i d ) ;
20 % Plot the convergence p r o f i l e
21 f i g u r e (2)
22 vecit = 2: i t e r
23 s e m i l o g y ( v e c t i t , a b s ( dk ( 2 : i t e r ) ) , ’−∗b ’ ) ;
24 t i t l e ( ’ P r o f i l o d i Convergenza ’ )
25 xlabel ( ’ iterazioni ’)
26 ylabel ( ’ scarti ’)
27 % Close the output f i l e
28 fclose ( fid ) ;

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Solution - FUNCTION part 1

1 f u n c t i o n [ xnew , i t e r , dk ] = s e c a n t ( f , x0 , x1 , i tm a x , t o l l , f i d )
2 % s e c a n t The s e c a n t method
3 % [ xnew , i t e r , dk ] = s e c a n t ( x0 , x1 , f , i t m a x , t o l l )
4 %
5 % I n p u t Data :
6 % f : function
7 % x0 : #1 i n i t i a l v a l u e xknew
8 % x1 : #2 i n i t i a l v a l u e xknew
9 % i t m a x : maximum number o f i t e r a t i o n
10 % t o l l : required tollerance
11 % fid : variable for f i l e identification
12 %
13 % Output Data :
14 % xnew : s o l u t i o n o f t h e l a s t i t e r a t i o n
15 % i t e r : number o f i t e r a t i o n s
16 % dk : | xnew−x o l d |
17 %
18 %
19 % Variable I n i t i a l i z a t i o n
20 xoldm1 = x0 ; % S e t i n i t i a l v a l u e o f x : x {k−1}
21 x o l d = x1 ; % S e t i n i t i a l v a l u e o f x : x {k}
22 dk ( 1 ) = 2 . 0 ∗ t o l l ; % S e t i n i t i a l v a l u e o f dk
23 f o l d m 1 = f ( xoldm1 ) ; % Compute t h e f u n c t i o n i n xoldm1
24 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

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 12/06/2017
Solution - FUNCTION part 2

1 % Loop W h i l e
2 w h i l e ( a b s ( dk ( i t e r ) ) >= t o l l ) && ( i t e r < itmax )
3 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
4 fold = f ( xold ) ; % Compute t h e f u n c t i o n v a l u e i n x o l d
5 mk = ( f o l d −f o l d m 1 ) / ( x o l d−xoldm1 ) ; % Compute mk
6 xnew = x o l d−f o l d /mk ; % Compute t h e new a p p r o x i m a t i o n xnew
7 dk ( i t e r ) = a b s ( xnew−x o l d ) ; % Update t h e v e c t o r dk
8 M1 = dk ( i t e r ) / dk ( i t e r −1) ˆ 1 . 6 1 8 ; % Compute t h e c o n v e r g e n c e f a c t o r
9 foldm1 = f o l d ; % Update t h e f u n c t i o n i n xoldm1
10 xoldm1 = x o l d ; % Update xoldm1
11 x o l d = xnew ; % Update x o l d
12 f p r i n t f ( f i d , ’%d %e %e %e \n ’ , i t e r , xnew , dk ( i t e r ) , M1) ;
13 end
14 end

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

You might also like