Home » Numerical Techniques Using C++ » Secant Method C++ Formula, Example & Algorithm

# Secant Method C++ Formula, Example & Algorithm

## Secant Method C++ Formula

Intro:

The Secant Method is used to find the roots of an equation.

The Secant method is similar to the Regula-Falsi method, except for the fact that we drop the condition that f(x) should have opposite signs at the two points used to generate the next approximation.

Instead, we always retain the last two points to generate the next. Thus, if xi-1 and xi are two approximations to the root, then the next approximation xi+1 to root is given by

In contrast to the Regula-Falsi method, the Secant method does not bracket the root and it is not even necessary to bracket the root to start the iteration.

Hence, it is obvious that the iteration may not always coverage. On the other hand, it generally converges faster.

## Algorithm for Secant Method

Step 1: Choose i=1

Step 3: Use the formula

Step 4: Find Absolute Error,|Ea|= |(Xi+1 -Xi)/Xi+1|*100

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

If true then stop

Else go to step 2 with estimate Xi+1, Xi

## Secant Method C++ Program

### Secant Method C++ Program for function f(x) = x2-2x-5

```.hljs{display:block;overflow-x:auto;padding:.5em;color:#abb2bf;background:#282c34}.hljs-keyword,.hljs-operator{color:#f92672}.hljs-pattern-match{color:#f92672}.hljs-pattern-match .hljs-constructor{color:#61aeee}.hljs-function{color:#61aeee}.hljs-function .hljs-params{color:#a6e22e}.hljs-function .hljs-params .hljs-typing{color:#fd971f}.hljs-module-access .hljs-module{color:#7e57c2}.hljs-constructor{color:#e2b93d}.hljs-constructor .hljs-string{color:#9ccc65}.hljs-comment,.hljs-quote{color:#b18eb1;font-style:italic}.hljs-doctag,.hljs-formula{color:#c678dd}.hljs-section,.hljs-name,.hljs-selector-tag,.hljs-deletion,.hljs-subst{color:#e06c75}.hljs-literal{color:#56b6c2}.hljs-string,.hljs-regexp,.hljs-addition,.hljs-attribute,.hljs-meta-string{color:#98c379}.hljs-built_in,.hljs-class .hljs-title{color:#e6c07b}.hljs-attr,.hljs-variable,.hljs-template-variable,.hljs-type,.hljs-selector-class,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-number{color:#d19a66}.hljs-symbol,.hljs-bullet,.hljs-link,.hljs-meta,.hljs-selector-id,.hljs-title{color:#61aeee}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-link{text-decoration:underline}.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines) {white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```  //Program: Secant Method using C++
//wikkihut.com

#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 intial 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:

## Secant Method Example

Related Numerical Methods: