0% found this document useful (0 votes)
7 views5 pages

Lab 5

Uploaded by

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

Lab 5

Uploaded by

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

EXPERIMENT NUMBER: 5

LAB TITLE
Implementation of Secant method in MATLAB
Introduction
Secant method is an iterative tool of mathematics and numerical methods to find the approximate root of polynomial
equations. During the course of iteration, this method assumes the function to be approximately linear in the region of
interest.

Mathematical Derivation of Secant Method:

Consider a curve f(x) = 0 as shown in the figure below:

Secant method estimates the point of intersection of the curve and the X- axis (i.e. root of the equation that represents
the curve) as exactly as possible. For that, it uses succession of roots of secant line in the curve.

Assume x0 and x1 to be the initial guess values, and construct a secant line to the curve through (x 0, f(x0)) and (x1, f(x1)).
The equation of this secant line is given by:

1|Page
EXPERIMENT NUMBER: 5
If x be the root of the given equation, it must satisfy: f(x) = 0 or y= 0. Substituting y = 0 in the above equation, and solving
for x, we get:

Now, considering this new x as x2, and repeating the same process for x2, x3, x4, . . . . we end up with the following
expressions:

This is the required formula which will also be used in the program for secant method in MATLAB.

Advantages of Secant Method over other Root Finding Methods:

 Its rate of convergence is more rapid than that of bisection method. So, secant method is considered to be a
much faster root finding method.

 In this method, there is no need to find the derivative of the function as in Newton-Raphson method.

Limitations of Secant Method:

 The method fails to converge when f(xn) = f(xn-1)

 If X-axis is tangential to the curve, it may not converge to the solution.

LAB Task
Implement Secant method by using for loop method. The code should be generalized and well commented, take Non-
linear function, initial guesses, tolerance, No. of iterations from the user and find out the approximated root of any given
function? Also draw flow chart for given code.

2|Page
EXPERIMENT NUMBER: 5
MATLAB Code:

function secant_method

% Prompt user for function, initial points, and tolerance

func = input('Enter the function in terms of x (e.g., @(x) x^2 - 4): ');

x0 = input('Enter the first initial point (x0): ');

x1 = input('Enter the second initial point (x1): ');

tolerance = input('Enter the tolerance value: ');

% Initialize variables

fprintf('Iteration\tRoot (x_n)\t\tf(x_n)\n');

iter = 0; % Initialize iteration counter

f_x0 = func(x0);

f_x1 = func(x1);

while true

iter = iter + 1; % Increment iteration counter

% Calculate next root using the secant method formula

x2 = (x0 * f_x1 - x1 * f_x0) / (f_x1 - f_x0);

f_x2 = func(x2);

% Display the current iteration results

fprintf('%d\t\t%.6f\t\t%.6f\n', iter, x2, f_x2);

3|Page
EXPERIMENT NUMBER: 5

% Check for convergence

if abs(x2 - x1) < tolerance || abs(f_x2) < tolerance

break; % Exit the loop if the tolerance is met

end

% Update for the next iteration

x0 = x1;

x1 = x2;

f_x0 = f_x1;

f_x1 = f_x2;

end

fprintf('Converged to root: %.6f with f(x_n) = %.6f\n',x2,f_x2);

end

Console Window

4|Page
EXPERIMENT NUMBER: 5
Flow Chart:
1. Start
2. Input Function and Initial Values: Prompt user to enter the function, initial points x0, x1, and tolerance.
3. Initialize Iteration Counter: Set iteration iter = 0.
4. Evaluate Function at Initial Points: Calculate f(x0) and f(x1).
5. Iteration Start: Increment iteration counter iter = iter + 1.
6. Calculate Next Approximation (x2): Use secant formula: x2 = (x0 * f(x1) - x1 * f(x0)) / (f(x1) - f(x0)).
7. Evaluate Function at x2: Calculate f(x2).
8. Display Iteration Results: Print current iteration, x2, and f(x2).
9. Convergence Check: If |x2 - x1| < tolerance or |f(x2)| < tolerance, go to step 13.
10. Update Values for Next Iteration: Set x0 = x1, x1 = x2, f(x0) = f(x1), f(x1) = f(x2).
11. Repeat Iteration: Return to step 5.
12. Convergence Achieved: Print final root and f(x2).
13. End.

5|Page

You might also like