C Program for Secant Method

6 Min Read
C Program for Secant Method

The secant method in C Program is the most popular method to solve quadratic equations and this is mainly because of its simplicity and accuracy. So, here we are going to discuss the this method in c programming.

What is Secant Method?

The secant method is a method of finding the roots of the quadratic equation. There are two main methods to solve this equation, one is Newton’s method and the other is the secant method. The this method is much faster than Newton’s method. It is also known as “Newton’s method without division”.

The basic idea of this method is that you divide the quadratic equation into two linear equations and then you use the solution of this new equation to get the values of x and y.

C Program for Secant Method

How to Use the Secant Method in C Programming?

In order to implement the secant method you need to know how to convert a quadratic equation to a linear equation. This can be achieved by dividing the quadratic equation by 2.

For example, if we have an equation like 3x^2 + 4x – 8 = 0.

Then, it will become like this:

Now, it is possible to rewrite this equation as two linear equations, as shown below:


x + 2 = sqrt(8/3)
3x + 6 = sqrt(8/3)

Now, you can use the formula that has been given to get the value of x and y.

For Example:


x = sqrt(8/3)/3 = sqrt(8/9)
y = -sqrt(8/9)/3 = -sqrt(8/9)

Now, you can check the values of x and y and find the solution of the original equation.

Secant method is the most effective approach to find the root of a function. It is based on Newton-Raphson method, and being free from derivative it can be used as an alternative to Newton’s method. The C program for Secant method requires two initial guesses, and the method overall is open bracket type. Also, the this method is an improvement over the Regula-Falsi method as approximation is done by a secant line during each iterative operation.

The programming effort for Secant Method in C is a bit tedious, but it’s the most effective of all other method to find the root of a function. Here, at each iteration, two of the most recent approximations of the root are utilized to find out the next approximation. Also, in this method, it is not mandatory that the interval should contain the root.

The secant method is faster than the bisection method as well as the regula-falsi method. The rate of convergence is fast; once the method converges, its rate of convergence is 1.62, which is quite high. Although convergence is not guaranteed in this method, this method is the most economical one giving definitely rapid rate of convergence at low cost.

Features of Secant Method:

  • Type – open bracket
  • No. of initial guesses – 2
  • Convergence – super linear
  • Rate of convergence – faster
  • Accuracy – good
  • Programming effort – tedious
  • Approach – interpolation

Below is a short and simple C programming source code for method to find the root of x^3 – 4 or x*x*x – 4.

f(x) = x*x*x – 4

Source Code for Secant Method in C:

#include<stdio.h>
float f(float x)
{
    return(x*x*x-4); // f(x)= x^3-4
}
float main()
{
    float a,b,c,d,e;
    int count=1,n;
    printf("\n\nEnter the values of a and b:\n"); //(a,b) must contain the solution.
    scanf("%f%f",&a,&b);
    printf("Enter the values of allowed error and maximun number of iterations:\n");
    scanf("%f %d",&e,&n);
    do
    {
        if(f(a)==f(b))
        {
            printf("\nSolution cannot be found as the values of a and b are same.\n");
        return;
        }
        c=(a*f(b)-b*f(a))/(f(b)-f(a));
        a=b;
        b=c;
        printf("Iteration No-%d    x=%f\n",count,c);
        count++;
        if(count==n)
        {
        break;
        }
    } while(fabs(f(c))>e);
    printf("\n The required solution is %f\n",c);

}

Input/Output:

When the values for a and b, the initial guesses, are entered same, the solution can’t  be obtained. In such case, enter different values for a and b such that f(a) and f(b) are not equal.

Also see,
Secant Method MATLAB Program
Secant Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

The C language source code for secant method is well-tested and completely error-free. Any questions relevant to method or it’s source code aforementioned can be brought up to us from the comments section. I hope you liked the explanation of the secant method. We have also discussed some of the tips to implement it in c programming.

Share This Article
4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version