Secant Method In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Secant Method In C++

Secant Method In C++

BLUF: Mastering Secant Method In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Secant Method In C++

C++ is renowned for its efficiency. Learn how Secant Method In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the Secant Method in C++ along with its algorithm and illustrations.

What is the Secant Method?

A technique used to find the root of a nonlinear equation is the secant method. This method involves an iterative approach that starts with two initial guesses, x1 and x2, which may not necessarily bracket the root. Subsequently, the method performs linear interpolation between these two estimates to iteratively compute an approximation of the root.

The secant method continues iterating endlessly, or until it hits a set maximum of iterations, or until the variance between successive approximations falls below the designated tolerance threshold.

Algorithm:

Step 1: Choose i=1

Begin by utilizing the initial estimations, x i-1 and x i, as the starting point.

Step 3: Use the formula

Calculate the Absolute Error, |Ea|, using the formula |(X i+1 -Xi)/X i+1 | * 100.

Check if |Ea| <= Es (Prescribed tolerance)

If true then stop

Else go to step 2 with estimate X i+1 , X i

Program 1:

Let's consider a scenario to demonstrate the Secant Method in C++.

Example

#include <iostream>
#include <cmath>
using namespace std;
// Function for which we are finding the root
double f(double x) {
    return x * x - 4; // Example function: f(x) = x^2 - 4
}
// Secant method function
double secantMethod(double x0, double x1, double tol, int maxIter) {
    double x2, fx0, fx1, fx2, error = tol + 1;
    int iter = 0;

    while (error > tol && iter < maxIter) {
        fx0 = f(x0);
        fx1 = f(x1);
        x2 = x1 - ((fx1 * (x1 - x0)) / (fx1 - fx0));
        fx2 = f(x2);
        error = abs(x2 - x1);
        x0 = x1;
        x1 = x2;
        iter++;
        cout << "Iteration " << iter << ": x = " << x2 << ", f(x) = " << fx2 << ", error = " << error << endl;
    }

    return x2;
}
int main() {
    double x0 = 1, x1 = 3, tol = 0.0001;
    int maxIter = 100;
    cout << "Initial guess x0 = " << x0 << ", x1 = " << x1 << endl;
    double root = secantMethod(x0, x1, tol, maxIter);
    cout << "Root found at x = " << root << endl;

    return 0;
}

Output:

Program 2:

Let's consider another instance to demonstrate the Secant Method in C++.

Example

#include<iostream>
#include<cmath>

 //for specifying format
#include<bits/stdc++.h>	

using namespace std;

  //prespecified error tolerance
#define Es 0.0001

float F(float x){
	float res;
	res=(x*x)-(2*x)-5;
	return res;
} 

int main(){
	float x0,x1,x2,x2old,f0,f1,f2;
	int itr=1;
	float Ea;
	cout<<"Enter the initial guesses (x0,x1): ";
	cin>>x0>>x1;
	cout<<"Itr No.\tx0\tx1\tf0\tf1\tx2\t f2\tEa\n";
	cout<<"----------------------------------------------------------------------------\n";
	while (1){
		f0=F(x0);
		f1=F(x1);		
		x2=x1-(f1*(x1-x0)/(f1-f0));
		f2=F(x2);
		
		//finding absolute error
		Ea=fabs((x2-x1)/x2);
		cout<<fixed<<setprecision(3);
	cout<<itr<<"\t"<<x0<<"\t"<<x1<<"\t"<<f0<<"\t"<<f1<<"\t"<<x2<<"\t"<<f2<<"\t"<<Ea<<"\n";	
		x0=x1;
		x1=x2;
		itr++;
		if(Ea<=Es)
			break;	
	}
	cout<<"Approximate root (x2) is: "<<x2;
	return 0;
}

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience