Lab 5
Lab 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.
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.
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.
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
func = input('Enter the function in terms of x (e.g., @(x) x^2 - 4): ');
% Initialize variables
fprintf('Iteration\tRoot (x_n)\t\tf(x_n)\n');
f_x0 = func(x0);
f_x1 = func(x1);
while true
f_x2 = func(x2);
3|Page
EXPERIMENT NUMBER: 5
end
x0 = x1;
x1 = x2;
f_x0 = f_x1;
f_x1 = f_x2;
end
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