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

Thursday, May 1, 2008

Program Halley - Farey to solve equations

//****************************************************************************
// Program Halley, Bisectie, BisFarey, Hall_Bis, Hall_BissFarey
//****************************************************************************
# include ;
# include ;
# include ;
# include ;
# include ;

# define epx 1e-3 //epx=eps pt F
# define EPS 0.000001
# define max 1500 //nr max de iteratii

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

double F(double x) { return x*x*x*x-x*x-2; }
double dF(double x) { return 4*x*x*x-2*x; }
double ddF(double x) { return 12*x*x-2; }

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

double a,aux,a_aux,b_aux,b,mij,epx_aux,val,v_N[3],v_D[3];
//[0] pt a, [1] pt mij, [2] pt b
int c,opt; //contor

double x0,xp,xc;
long int NrIt;


//****************************************************************************
void main()
{
clrscr();
opt=0;

while(opt!=6)
{
clrscr();
cout<<"Please choose an option :\n";
cout<<"\t1. Method bisection\n";
cout<<"\t2. Metoda bisection using the FAREY addition\n";

cout<<"\t3. Method Halley\n";
cout<<"\t4. Method Halley_Bisection\n";
cout<<"\t5. Method Halley_BisectionFarey\n";

textcolor(YELLOW);
cprintf(" 6. Enter the interval");
textcolor(15);
cout<<"\n\t7. END THE PROGRAM\n";
cin>>opt;
if(opt!=6) cout<<"\a";
}

while(opt<=6)
{
c=1;
if(opt==6)
{
cout<<"Enter the limit of the interval, a,b :\n";
cin>>a>>b;
a_aux=a;
b_aux=b;
}

if(opt<=2)
{
aux=0;
a=a_aux;
b=b_aux;
mij=(a+b)/2;

//initializari
if(opt==1) mij=(a+b)/2;
else if(opt==2)
{
v_N[0]=a;
v_D[0]=1;
v_N[2]=b;
v_D[2]=1;
v_N[1]=v_N[0]+v_N[2];
v_D[1]=v_D[0]+v_D[2];
mij=v_N[1]/+v_D[1];
}

c=0;
epx_aux=epx+1;
val=fabs(F(mij));

while((c=epx)||(fabs(aux-mij)>epx)||(fabs(val/mij)>epx))
{
epx_aux=fabs(F(mij)/mij);

if(F(a)*F(mij)<0)
{
if(opt==1) b=mij;
else
{
v_N[2]=v_N[1];
v_D[2]=v_D[1];
b=v_N[2]/v_D[2];
}
}
else if(F(mij)*F(b)<0)
{
if(opt==1) a=mij;
else
{
v_N[0]=v_N[1];
v_D[0]=v_D[1];
a=v_N[0]/v_D[0];
}
}

aux=mij;
if(opt==1) mij=(a+b)/2;
else
{
v_N[1]=v_N[0]+v_N[2];
v_D[1]=v_D[0]+v_D[2];
mij=(v_N[0]+v_N[2])/(v_D[0]+v_D[2]);
}

c++;

cout.precision(30);
cout<< endl <<" Epx="<< epx_aux<<" ; nr it="<< c;
val=fabs(F(mij));
}
cout.precision(10);
cout<<"\n\nAm iesit pe :";

if(c>=max) cout<<"\n No. of iterations ="<< c;
if(fabs(F(mij))<=epx) cout<<"\n Criterion with epx ="<< fabs(F(mij)/(mij));

cout<<"\n";
cout<<"\nNo. of iterations PRG ="<< max;

cout<< endl<< endl<<"Solution of the equation is, x= "<< mij;

getche();
}

if(opt==3)
{
clrscr();
cout<<"Enter the start value: ";
cin>>x0;

NrIt=0;

xp=x0;
xc=xp;
aux=0;
while(fabs((F(xc))>=EPS)||(fabs(aux-xc)>=EPS))
{
NrIt++;
xc=xp-(2*F(xp)*dF(xp))/(2*dF(xp)*dF(xp)-F(xp)*ddF(xp));
aux=xp;
xp=xc;
}

cout<<"\nThe solution is: "<< xc<<" with the precision "<< EPS;
cout<<"\n\nNo. of iterations= "<< NrIt;

getche();

}

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

if(opt==4)
{
clrscr();
cout<<"Enter the start value : ";
cin>>x0;

NrIt=0;

double _xa,_xb,_mij,_aux,DIF;

xp=x0;
xc=xp;
aux=0;

_xa=a;
_xb=b;
_mij=(a+b)/2;
_aux=0;
while(fabs((F(xc))>=EPS)||(fabs(aux-xc)>=EPS)||(fabs(_aux-xc)>=EPS))
{
NrIt++;

//
if(F(_xa)*F(_mij)<0)_xb=_mij;
else if(F(_xb)*F(_mij)<0) _xa=_mij;
_mij=(_xa+_xb)/2;
DIF=_mij-_aux;
_aux=_mij;
//

xc=xp-(2*F(xp))/(2*dF(xp)-ddF(xp)*DIF);
aux=xp;
xp=xc;
}

cout<<"\nThe solution is: "<< xc<<" with the precision "<< EPS;
cout<<"\n\nNo of iteartions= "<< NrIt;

getche();

}

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

if(opt==5)
{
clrscr();
cout<<"Enter the start value : ";
cin>>x0;

NrIt=0;

double _xa,_xb,_mij,_aux,DIF;

xp=x0;
xc=xp;
aux=0;

_xa=a;
_xb=b;
//_mij=(a+b)/2;
//_aux=mij;

v_N[0]=a;
v_D[0]=1;
v_N[2]=b;
v_D[2]=1;
v_N[1]=v_N[0]+v_N[2];
v_D[1]=v_D[0]+v_D[2];
_mij=v_N[1]/+v_D[1];
_aux=0;

while(fabs((F(xc))>=EPS)||(fabs(aux-xc)>=EPS)||(fabs(_aux-xc)>=EPS))
{
NrIt++;

//
if(F(_xa)*F(_mij)<0)
{
v_N[2]=v_N[1];
v_D[2]=v_D[1];
_xb=v_N[2]/v_D[2];
}
else if(F(_xb)*F(_mij)<0)
{
v_N[0]=v_N[1];
v_D[0]=v_D[1];
_xa=v_N[0]/v_D[0];
}

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

DIF=_mij-_aux;
_aux=_mij;
//

xc=xp-(2*F(xp))/(2*dF(xp)-ddF(xp)*DIF);
aux=xp;
xp=xc;
}

cout<<"\nThw solution is: "<< xc<<" with the precision "<< EPS;
cout<<"\n\nNo. of iterations= "<< NrIt;

getche();

}


//****************************************************************************
clrscr();

cout<<"Please choose an option :\n";
cout<<"\t1. Method bisection\n";
cout<<"\t2. Metoda bisection using the FAREY addition\n";

cout<<"\t3. Method Halley\n";
cout<<"\t4. Method Halley_Bisection\n";
cout<<"\t5. Method Halley_BisectionFarey\n";

printf("\t6. Enter the interval");
cout<<"\n\t7. END THE PROGRAM\n";

cin>>opt;
}

}
//****************************************************************************