Wednesday, May 7, 2008

Chord - Tangent method to solve equations

//****************************************************************************
# include ;
# include ;
# include ;

# define EPS 1.e-6

double tc,tp,sc,sp,xp,xc,a,b,aux;
long int NrIt;


//****************************************************************************
double f(double x)
{
return x*x*x*x-x*x-2;
}

double df(double x)
{
return 4*x*x*x-2*x;;
}

//****************************************************************************


void main(void)
{
clrscr();
cout<<"Please enter the limits of the interval :\n";
cin>>a>>b;

NrIt=0;

if(f(a)*df(a)>0)
{
tp=a;
sp=b;
}
else if(f(a)*df(a)<0)
{
tp=b;
sp=a;
}

aux=0;
xp=xc=(sp+tp)/2.0;

while((f(xc)>=EPS)/*&&(fabs(xc-aux)>=EPS)*/)
{
NrIt++;

tc=tp-f(tp)/df(tp);
sc=(sp*f(tp)-tp*f(sp))/(f(tp)-f(sp));

xc=(sc+tc)/2.0;
aux=xp;
xp=xc;
tp=tc;
sp=sc;

cout<<"\nX"<
}

cout<<"\nThe solution is: "<cout<<"\n\nNo. of iterations= "<
getche();
}

No comments: