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();
}

Farey Chord - Tangent method to solve equations

If the limits of the interval are very big than the Chord-Tangent method using the Farey addition produce in half of steps better approximations:


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

# define EPS 1.e-6

double tc,tp,sc,sp,xp,xc,a,b,aux,mij2;
double v_N[3],v_D[3];//[0] pt a, [1] pt mij, [2] pt b
long int NrIt=0;


//****************************************************************************
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;
v_N[0]=b;//sp
v_D[0]=1;
v_N[2]=a;//tp
v_D[2]=1;
}
else if(f(a)*df(a)<0)
{
tp=b;
sp=a;
v_N[0]=a;//sp
v_D[0]=1;
v_N[2]=b;//tp
v_D[2]=1;
}

//double mij;

v_N[1]=v_N[0]+v_N[2];
v_D[1]=v_D[0]+v_D[2];
xp=xc=v_N[1]/v_D[1];//pt xp, xc

//mij=
aux=0;
mij2=(a+b)/2;

while(
(fabs(f(xc))>=EPS)
&&(fabs(f(xc)/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=(v_N[0]+v_N[2])/(v_D[0]+v_D[2]);

if(f(sc)*f(xc)<0)
{
if(mij2>xc)
{
if(f(sc)*f(xc)<0)
{
v_N[2]=v_N[1];
v_D[2]=v_D[1];
tc=v_N[2]/v_D[2];
}
}
else //mij2<=xc
{
if(f(sc)*f(mij2)<0)
{
v_N[2]=mij2;
v_D[2]=1;
tc=v_N[2]/v_D[2];
}
else if(f(mij2)*f(xc)<0)
{
v_N[0]=mij2;
v_D[0]=1;
sc=v_N[2]/v_D[2];
v_N[2]=v_N[1];
v_D[2]=v_D[1];
tc=v_N[2]/v_D[2];
}
}
}

else if(f(xc)*f(tc)<0)
{
if(mij2 {
if(f(mij2)*f(xc)<0)
{
v_N[0]=mij2;
v_D[0]=1;
sc=v_N[0]/v_D[0];
}
}
else //mij2>=xc
{
if(f(xc)*f(mij2)<0)
{
v_N[0]=v_N[1];
v_D[0]=v_D[1];
sc=v_N[0]/v_D[0];
v_N[2]=mij2;
v_D[2]=1;
tc=v_N[0]/v_D[0];
}
else if(f(mij2)*f(tc)<0)
{
v_N[0]=mij2;
v_D[0]=1;
sc=v_N[0]/v_D[0];
}
}
}



v_N[1]=v_N[0]+v_N[2];
v_D[1]=v_D[0]+v_D[2];
xc=(v_N[0]+v_N[2])/(v_D[0]+v_D[2]);

mij2=(sc+tc)/2;

aux=xp;
xp=xc;
tp=tc;
sp=sc;

cout<<"\nX"<}

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