Blog

Secant Method in C++ Programming

Secant Method

Secant Method/ Numerical Method /C++ Programming /Secant Method in C++ Programming / csit/ Computer science /Engineering

Secant Method method now requires two initial guesses, but unlike the bisection method, the two initial guesses do not need to bracket the root of the equation. The secant method is an open method and may or may not converge. However, when secant method converges, it will typically converge faster than the bisection method.

Secant Method in C++ Programming

#include<iostream>
#include<math.h>
using namespace std;

#define f(x) 3*x*x-6*x+2 //equation

int main()
{
    float x0,x1,x2,f0,f1,f2=1,e=1;
    cout<< "enter the initial guess:";
    cin>>x0>>x1;
    while(e>0.005 && f2!=0)
    {
        f0=f(x0);
        f1=f(x1);
        x2=(x1-(f1*(x1-x0)/(f1-f0))); //working formula 
        f2=f(x2);
        e=fabs((x1-x0)/x1);
        x0=x1;
        x1=x2;
    }
    cout<< "nthe root of the equation="<<x2;
    return 0;
}

Output :

Enter the initial Guess : 4
7
The root of the equation= 1.57735

You may like some other numerical method Questions :

BISECTION METHOD SOLUTION IN C++ PROGRAMMING
FALSE POSITION METHOD IN C++ PROGRAMMING
SECANT METHOD IN C++ PROGRAMMING
FIXED POINT METHOD / C++ PROGRAMMING
NEWTON-RAPHSON METHOD IN C++ PROGRAMMING