Secant Method

Home >> Sem 2 >> CONM program list  >> Secant Method
Just copy and paste to notepad  



//cosx-3x+1
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
void main()
{
         double x1,x2,x3,fx1,fx2,fx3,e;
         abc:
         cout<<"Enter Two Initial Approximation\n";
         cin>>x1>>x2;
         fx1=cos(x1)-(3*x1)+1;
         fx2=cos(x2)-(3*x2)+1;
         if(fx1*fx2<0)
         {
                  x3=((x1*fx2)-(x2*fx1))/(fx2-fx1);
                  fx3=cos(x3)-(3*x3)+1;
         }
         else
         {
                 cout<<"Re-Enter The Root\n";
                 goto abc;
         }
         cout<<"Enter Epsilon\n";
         cin>>e;
         cout<<"x1"<<setw(15)<<"x2"<<setw(15)<<"fx1"<<setw(15)<<"fx2"<<setw(15)<<"x3"<<setw(15)<<"fx3"<<endl;
         cout<<x1<<setw(15)<<x2<<setw(15)<<fx1<<setw(15)<<fx2<<setw(15)<<x3<<setw(15)<<fx3<<endl;
         while((x3-x1)>e || (x3-x2)>e)
         {

                          x1=x2;
                          x2=x3;
                          fx1=cos(x1)-(3*x1)+1;
                          fx2=cos(x2)-(3*x2)+1;
                          x3=((x1*fx2)-(x2*fx1))/(fx2-fx1);
                          fx3=cos(x3)-(3*x3)+1;
                          cout<<x1<<setw(15)<<x2<<setw(15)<<fx1<<setw(15)<<fx2<<setw(15)<<x3<<setw(15)<<fx3<<endl;
        }
         cout<<"Required Root Is:="<<x3;
}