write a program to find a root of non linear equation using Secant method in c++.

write a program to find a root of non linear equation using Secant method in c++.

Source Code:
/********************************************
    program: solution of non-linear equation
                    bisection method
    language: C
    Author : Tanveer Khan
    Rajasthan University, Jaipur
************************************************

///solution of non linear equations using secant method

#include<iostream.h>
#include<conio.h>
#include<math.h>
float secant(float x)
{
return(pow(x,3)-(5*x)+3);
}
void main()
{
clrscr();
float x,y,x0,x1;
int i=0,n;
cout<<"equation : ((x^3)-5x+3)"<<endl;
cout<<"enter the 2 roots ";
cin>>x>>y;
cout<<"enter no. of iterations ";
cin>>n;
if(secant(x)*secant(y)>=0)
{
cout<<"interval you entered is wrong."<<endl;
}
do
{
if(i==n)
{break;
}
x0=(x*secant(y)-y*secant(x))/(secant(y)-secant(x));
cout<<"\n"<<i+1<<"\tx="<<x<<"\ty="<<y<<"\t";
cout<<"x"<<i+1<<"="<<x0<<"\t f(x"<<i+1<<")="<<secant(x0)<<"\n";
cout<<"the "<<i+1<<" approximation root is "<<x0<<"\n";
if(x0==x1)
{
cout<<"the root is "<<x0<<"\n";
}
x1=x0;
i++;
x=y;
y=x1;
}
while(secant(x)*secant(y)!=0);
getch();
}

OUTPUT :


Comments