You are on page 1of 3

Secant method

#include<iostream.h> // header file


#include<iomanip.h>
#include<math.h>
#include<conio.h> //for clear screen
float f(float x); //maths funcs
void secant(float *x,float a,float b,int*itr); // funtn for secant
method
void main()
{
int itr=0, maxitr;
float x, x0,x1,aerr,e;
clrscr();
cout<<"enter the value of a,b,allowed error and maximum
iteration"<<"\n";
cin>>x0>>x1>>aerr>>maxitr;
do
{
secant(&x,x0,x1,&itr);
e=fabs((x0-x1)/x1)*100; /for absolute value
cout<<"error in this iteration="<<e<<"%"<<"\n";
x0=x1;
x1=x;
}
while(itr<maxitr&&e>aerr);
getch();
}
float f(float x)
{
return (tan(x)+x-3); //functns
}
void secant (float*x,float x0,float x1,int*itr)
{
*x=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
++(*itr);

// funtns calling

cout<<"x0="<<",f(x0)="<<f(x0)<<",x1="<<x1<<",f(x1)="<<f
(x1)<<"iteration"
<<*itr<<" x="<<setw(5)<<setprecision(5)<<*x<<"\n";
}

For tan(x)+x-3

For x3+x+7

You might also like