The Secant Method MATLAB Program

The Secant Method is a numerical algorithm used to find the roots of a function. It is similar to the Newton-Raphson method, but instead of using the derivative of the function, it uses an approximation of the derivative based on two points. The algorithm starts with two initial points and then iteratively refines them to converge to the root of the function.

Here’s how the Secant Method works:

  1. Choose two initial points, x0, and x1, such that x0 is not too close to x1.
  2. Calculate the function values at x0 and x1, i.e., f(x0) and f(x1).
  3. Calculate the slope of the secant line passing through (x0, f(x0)) and (x1, f(x1)).
  4. Calculate the x-intercept of the secant line.
  5. Set x0 to x1 and x1 to the x-intercept of the secant line.
  6. Repeat steps 2-5 until the difference between x1 and x0 is less than a predetermined tolerance.

Here’s the Secant Method algorithm in mathematical notation:

Given a function f(x), initial points x0 and x1, and a tolerance tol, repeat the following steps until |x1 - x0| < tol:

  • Calculate the slope of the secant line passing through (x0, f(x0)) and (x1, f(x1)): m = (f(x1) - f(x0)) / (x1 - x0)
  • Calculate the x-intercept of the secant line: x2 = x1 - f(x1) / m
  • Set x0 = x1 and x1 = x2

The value of x1 will be the root of the function.

Note that the Secant Method may not converge for some functions or initial points. It is also possible to encounter issues such as division by zero or numerical instability, so care must be taken when implementing the algorithm.

Matlab code for secant method

Here’s how you can implement the Secant Method in MATLAB:

Step 1: Define the function You need to define the function for which you want to find the root. For example, let’s say you want to find the root of the function f(x) = x^2 - 4. You can define the function in MATLAB as follows:

function y = f(x)
    y = x^2 - 4;
end

Step 2: Define the initial points You need to define two initial points, x0 and x1, from which the algorithm will start. Make sure that x0 and x1 are not too close to each other. For example, let’s say you choose x0 = 1 and x1 = 3.

x0 = 1;
x1 = 3;

Step 3: Define the tolerance You need to define a tolerance value that will determine the stopping criteria for the algorithm. For example, let’s say you choose a tolerance of 0.0001.

tol = 0.0001;

Step 4: Implement the Secant Method You can implement the Secant Method in MATLAB using a while loop. The loop will iterate until the difference between the two last approximations is smaller than the tolerance value.

while abs(x1 - x0) > tol
    x2 = x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0));
    x0 = x1;
    x1 = x2;
end

The value x2 will be the root of the function.

Step 5: Display the result Finally, you can display the result using the disp() function.

disp(['The root is: ', num2str(x2)])

Here’s the complete code:

function y = f(x)
    y = x^2 - 4;
end

x0 = 1;
x1 = 3;
tol = 0.0001;

while abs(x1 - x0) > tol
    x2 = x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0));
    x0 = x1;
    x1 = x2;
end

disp(['The root is: ', num2str(x2)])