البرامج التطبيقية للطرق العددية التكرارية :
3-1-1- Bisection Method  :
#include<iostream.h>
#include<stdio.h>
#include<math.h>
float fx(float);
main (){
const float ep=0.001;
int k=0;
float a,b,x,fa,fb,fxx;
cout<<"Enter a and b";
cin>>a>>b;
do{
fa=fx(a);
fb=fx(b);
x=(a+b)/2;
fxx=fx(x);
if ((fa * fxx)>0)
a=x;
else b=x;
k+=1;
cout<<endl<<"\nIt No.="<<k<<"\t";
cout<<a<<"\t"<<b<<"\t"<<x<<"\t";
printf("f(a)=%.5f ,\tf(b)=%.5f ,\tf(x)=%.5f",fa,fb,fxx);
}
while(fabs(fxx)>ep);
printf("\nThe Root x=%.5f",x);
return 0;
}
float fx(float x){
float y=x+log(x); //float y=pow(x,3)+x*x-3*x-3;
return y;
}

3-1-2-Newton Raphson Method  :
#include<iostream.h>
#include<math.h>
float f(float);
main()
{
const float ep=0.001;
long int k=0;
float x,dx,fx,dv;
cout<<"Enter x & dx";
cin>>x>>dx;
do{
dv=(f(x+dx)-f(x-dx))/2*dx;
fx=f(x);
x=x-fx/dv;
cout<<endl<<k<<"\t"<<x;
k=k+1;
}
while(fabs(f(x))>ep);
cout<<"\n\n The Root is x="<<x;
return 0;
}
float f(float x){
float y;
y=x*x-2;
return y ;


3-1-3-Secant Method  :
#include<iostream.h>
#include<stdio.h>
#include<math.h>
float f(float);
main(){
const float ep=0.001;
long int k=0;
float x,x0,x1,dx,fx1,fx0,fx;
cout<<"Enter x0 & x1";
cin>>x0>>x1;
do{
dx=x1-x0;
fx=f(x);
fx1=f(x1);
fx0=f(x0);
x=x1-((fx1*dx)/(fx1-fx0));
cout<<endl<<"\n"<<k<<"\t"<<x0<<"\t"<<x1<<"\t"<<x<<"\t";
k=k+1;
x0=x1;
x1=x ;
}
while(fabs(f(x))>ep);
cout<<"\n\n The Root is x="<<x;
return 0;
}
float f(float x){
float y;
y=x*x-3*x+1;
return y ;


3-1-4 - fails  position method  :
#include"iostream.h"
#include<stdlib.h>
#include<iomanip.h>
#include<conio.h>
#include<math.h>
double f(double);
void   main()
   {
         clrscr();
    float x0,xi;
    int   no,i;
          /* FIXED POINT  METHOD FIND f(x)= cos(x)-x */
          cout<<"\n\n PLEASE ENTER THE INITIAL VALUE:  ";
          cout<<"x0=  ";
          cin>>x0;
         cout<<"\n\n PLEASE ENTER THE NUMBRE OF ITERATIONS:  ";
          cout<<"no=  ";
          cin>>no;
cout<<"===========================================";         cout<<"\n"<<setw(16)<<"no"<<setw(14)<<"xi"<<setw(14)<<"f(xi)"<<"\n";
cout<<"===========================================\n";
      i=1;
    while(i<=no)
           {
             xi=cos(x0);
         if (fabs(f(xi))<=0.00001)
           {
             cout<<"\n\n\t  PROGRAM COMPLETE SUCCESSFULY ";
             getch();
             exit(1);
                }
                     cout<<setw(16)<<i<<setw(16)<<x0<<setw(16)<<f(x0)<<endl;
                   i++;
                   x0=xi;
                   }
                cout<<"\n\n\t PROCEDURE COMPLETED UN_SUCCESSFULY";
               getch();
}
3-1-5-Fixed Point Method  :
#include<iostream.h>
#include<math.h>
float g(float);
main(){
long int k=0;
float x,gx;
cout<<"enter intial value x";
cin>>x;
do{
gx=g(x);
cout<<"\n"<<k<<"\t"<<x<<"\t"<<gx;
//gx=x;
k=k+1;
x=gx;
}
while(fabs(gx)>1);
cout<<"\n\nThe Root is x="<<x;
return 0;
}
float g(float x){
float h=((x*x)+1)/3;//pow(1-x,1/3);
return h;
}
3-1-6- Muller method :
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + 2*(x)*(x) + 10*(x) - 20
void main()
{
double x1,x2,x3,x4_1,x4_2,fx1,fx2,fx3,
     h1,h2,h3_1,h3_2,h4,D,d1,d2,a1,a2,a0;
int i=1;
  clrscr();
  printf("\nEnter the value of x1: ");
  scanf("%lf",&x1);
  printf("\nEnter the value of x2: ");
  scanf("%lf",&x2);
  printf("\nEnter the value of x3: ");
  scanf("%lf",&x3);
  fx1 = F(x1);
  printf("\n\n f(x1) = %lf",fx1);
  getch();
  fx2 = F(x2);
  printf("\n\n f(x2) = %lf",fx2);
  getch();
  fx3 = a0 = F(x3);
  printf("\n\n f(x3) = %lf",fx3);
  getch();
  h1 = x1-x3;
  h2 = x2-x3;

  d1 = fx1-fx3;
  d2 = fx2-fx3;

  D = h1*h2*(h1-h2);

  a1 = (d2*h1*h1 - d1*h2*h2)/D;
  a2 = (d1*h2 - d2*h1)/D;

  h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
  h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));
if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
         ((a1 - sqrt(fabs(a1*a1 - (4*a2*a0))))) )
  {
   h4 = h3_1;
  }
else
  {
   h4 = h3_2;
  }
  x4_1 = x3 + h4;
  printf("\n\n\n x4 = %lf \n",x4_1);

  x1=x2;
  x2=x3;
  x3=x4_1;
  printf("\n\nx1 = %lf",x1);
  printf("\n\nx2 = %lf",x2);
  printf("\n\nx3 = %lf",x3);
  getch();
do
  {
   fx1 = F(x1);
   fx2 = F(x2);
   fx3 = a0 = F(x3);

   h1 = x1-x3;
   h2 = x2-x3;

   d1 = fx1-fx3;
   d2 = fx2-fx3;

   D = h1*h2*(h1-h2);

   a1 = (d2*h1*h1 - d1*h2*h2)/D;
   a2 = (d1*h2 - d2*h1)/D;
   h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
   h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));

if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
         (a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))) )
   {
    h4 = h3_1;
   }
else
   {
    h4 = h3_2;
   }
   x4_2 = x3 + h4;
   printf("\n\n\n x4 = %lf \n",x4_2);
   getch();
if(fabs(x4_1 - x4_2) < ESP)
   {
    printf("\n\nREAL ROOT = %.3lf",x4_2);
    i=0;
   }
else
   {
     x4_1=x4_2;
     x1=x2;
     x2=x3;
     x3=x4_1;
     printf("\n\nx1 = %lf",x1);
     printf("\n\nx2 = %lf",x2);
     printf("\n\nx3 = %lf",x3);                                            
   }
  }while(i!=0);
getch();
}
___________________
     OUT PUT
Enter the value of x1: 0
Enter the value of x2: 1
Enter the value of x3: 2
 f(x1) = -20.000000
 f(x2) = -7.000000
 f(x3) = 16.000000
 x4 = 1.354066
x1 = 1.000000
x2 = 2.000000
x3 = 1.354066
x4 = 1.368647
x1 = 2.000000
x2 = 1.354066
x3 = 1.368647
x4 = 1.368808
REAL ROOT = 1.369


3-1-1- Bisection Method  :
#include<iostream.h>
#include<stdio.h>
#include<math.h>
float fx(float);
main (){
const float ep=0.001;
int k=0;
float a,b,x,fa,fb,fxx;
cout<<"Enter a and b";
cin>>a>>b;
do{
fa=fx(a);
fb=fx(b);
x=(a+b)/2;
fxx=fx(x);
if ((fa * fxx)>0)
a=x;
else b=x;
k+=1;
cout<<endl<<"\nIt No.="<<k<<"\t";
cout<<a<<"\t"<<b<<"\t"<<x<<"\t";
printf("f(a)=%.5f ,\tf(b)=%.5f ,\tf(x)=%.5f",fa,fb,fxx);
}
while(fabs(fxx)>ep);
printf("\nThe Root x=%.5f",x);
return 0;
}
float fx(float x){
float y=x+log(x); //float y=pow(x,3)+x*x-3*x-3;
return y;
}

3-1-2-Newton Raphson Method  :
#include<iostream.h>
#include<math.h>
float f(float);
main()
{
const float ep=0.001;
long int k=0;
float x,dx,fx,dv;
cout<<"Enter x & dx";
cin>>x>>dx;
do{
dv=(f(x+dx)-f(x-dx))/2*dx;
fx=f(x);
x=x-fx/dv;
cout<<endl<<k<<"\t"<<x;
k=k+1;
}
while(fabs(f(x))>ep);
cout<<"\n\n The Root is x="<<x;
return 0;
}
float f(float x){
float y;
y=x*x-2;
return y ;


3-1-3-Secant Method  :
#include<iostream.h>
#include<stdio.h>
#include<math.h>
float f(float);
main(){
const float ep=0.001;
long int k=0;
float x,x0,x1,dx,fx1,fx0,fx;
cout<<"Enter x0 & x1";
cin>>x0>>x1;
do{
dx=x1-x0;
fx=f(x);
fx1=f(x1);
fx0=f(x0);
x=x1-((fx1*dx)/(fx1-fx0));
cout<<endl<<"\n"<<k<<"\t"<<x0<<"\t"<<x1<<"\t"<<x<<"\t";
k=k+1;
x0=x1;
x1=x ;
}
while(fabs(f(x))>ep);
cout<<"\n\n The Root is x="<<x;
return 0;
}
float f(float x){
float y;
y=x*x-3*x+1;
return y ;


3-1-4 - fails  position method  :
#include"iostream.h"
#include<stdlib.h>
#include<iomanip.h>
#include<conio.h>
#include<math.h>
double f(double);
void   main()
   {
         clrscr();
    float x0,xi;
    int   no,i;
          /* FIXED POINT  METHOD FIND f(x)= cos(x)-x */
          cout<<"\n\n PLEASE ENTER THE INITIAL VALUE:  ";
          cout<<"x0=  ";
          cin>>x0;
         cout<<"\n\n PLEASE ENTER THE NUMBRE OF ITERATIONS:  ";
          cout<<"no=  ";
          cin>>no;
cout<<"===========================================";         cout<<"\n"<<setw(16)<<"no"<<setw(14)<<"xi"<<setw(14)<<"f(xi)"<<"\n";
cout<<"===========================================\n";
      i=1;
    while(i<=no)
           {
             xi=cos(x0);
         if (fabs(f(xi))<=0.00001)
           {
             cout<<"\n\n\t  PROGRAM COMPLETE SUCCESSFULY ";
             getch();
             exit(1);
                }
                     cout<<setw(16)<<i<<setw(16)<<x0<<setw(16)<<f(x0)<<endl;
                   i++;
                   x0=xi;
                   }
                cout<<"\n\n\t PROCEDURE COMPLETED UN_SUCCESSFULY";
               getch();
}
3-1-5-Fixed Point Method  :
#include<iostream.h>
#include<math.h>
float g(float);
main(){
long int k=0;
float x,gx;
cout<<"enter intial value x";
cin>>x;
do{
gx=g(x);
cout<<"\n"<<k<<"\t"<<x<<"\t"<<gx;
//gx=x;
k=k+1;
x=gx;
}
while(fabs(gx)>1);
cout<<"\n\nThe Root is x="<<x;
return 0;
}
float g(float x){
float h=((x*x)+1)/3;//pow(1-x,1/3);
return h;
}
3-1-6- Muller method :
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + 2*(x)*(x) + 10*(x) - 20
void main()
{
double x1,x2,x3,x4_1,x4_2,fx1,fx2,fx3,
     h1,h2,h3_1,h3_2,h4,D,d1,d2,a1,a2,a0;
int i=1;
  clrscr();
  printf("\nEnter the value of x1: ");
  scanf("%lf",&x1);
  printf("\nEnter the value of x2: ");
  scanf("%lf",&x2);
  printf("\nEnter the value of x3: ");
  scanf("%lf",&x3);
  fx1 = F(x1);
  printf("\n\n f(x1) = %lf",fx1);
  getch();
  fx2 = F(x2);
  printf("\n\n f(x2) = %lf",fx2);
  getch();
  fx3 = a0 = F(x3);
  printf("\n\n f(x3) = %lf",fx3);
  getch();
  h1 = x1-x3;
  h2 = x2-x3;

  d1 = fx1-fx3;
  d2 = fx2-fx3;

  D = h1*h2*(h1-h2);

  a1 = (d2*h1*h1 - d1*h2*h2)/D;
  a2 = (d1*h2 - d2*h1)/D;

  h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
  h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));
if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
         ((a1 - sqrt(fabs(a1*a1 - (4*a2*a0))))) )
  {
   h4 = h3_1;
  }
else
  {
   h4 = h3_2;
  }
  x4_1 = x3 + h4;
  printf("\n\n\n x4 = %lf \n",x4_1);

  x1=x2;
  x2=x3;
  x3=x4_1;
  printf("\n\nx1 = %lf",x1);
  printf("\n\nx2 = %lf",x2);
  printf("\n\nx3 = %lf",x3);
  getch();
do
  {
   fx1 = F(x1);
   fx2 = F(x2);
   fx3 = a0 = F(x3);

   h1 = x1-x3;
   h2 = x2-x3;

   d1 = fx1-fx3;
   d2 = fx2-fx3;

   D = h1*h2*(h1-h2);

   a1 = (d2*h1*h1 - d1*h2*h2)/D;
   a2 = (d1*h2 - d2*h1)/D;
   h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
   h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));

if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
         (a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))) )
   {
    h4 = h3_1;
   }
else
   {
    h4 = h3_2;
   }
   x4_2 = x3 + h4;
   printf("\n\n\n x4 = %lf \n",x4_2);
   getch();
if(fabs(x4_1 - x4_2) < ESP)
   {
    printf("\n\nREAL ROOT = %.3lf",x4_2);
    i=0;
   }
else
   {
     x4_1=x4_2;
     x1=x2;
     x2=x3;
     x3=x4_1;
     printf("\n\nx1 = %lf",x1);
     printf("\n\nx2 = %lf",x2);
     printf("\n\nx3 = %lf",x3);                                            
   }
  }while(i!=0);
getch();
}
___________________
     OUT PUT
Enter the value of x1: 0
Enter the value of x2: 1
Enter the value of x3: 2
 f(x1) = -20.000000
 f(x2) = -7.000000
 f(x3) = 16.000000
 x4 = 1.354066
x1 = 1.000000
x2 = 2.000000
x3 = 1.354066
x4 = 1.368647
x1 = 2.000000
x2 = 1.354066
x3 = 1.368647
x4 = 1.368808
REAL ROOT = 1.369

Post a Comment

Previous Post Next Post