Saturday, 1 December 2012

False Position Method/Regula Falsi






       §       /* False position method*/


#include<stdio.h>
#include<math.h>

#define f(x) (x*x*x-4*x*x-9*x-12) // any equation can be used

void main()
{
       void false(float*,float,float,float,float,int*);
    int itr=0,N;
       float x,x0,x1,x2,x3,err;
       printf("----------METHOD OF FALSE POSITION FOR SOLVING NONLINEAR EQUATIONS-----------\n\n\n");
       printf("enter starting values,max error,max iteration\n\n");
       scanf("%f%f%f%d",&x0,&x1,&err,&N);
       false(&x2,x1,x0,f(x0),f(x1),&itr);
       do
       {
         if(f(x0)*f(x2)<0)
      x1=x2;
      else
      x0=x2;
         false(&x3,x0,x1,f(x0),f(x1),&itr);
         if(fabs(x3-x2)<err)
      {
         printf("after %d th iterations root=%6.4f\n",itr,x3);
      getch();
      return 0;
      }
      x2=x3;
        }while(itr<N);
      printf("solution has more error for given number of iteration");
      return 1;
      getch();
}



// now function false//

void false(float *x,float x0,float x1,float fx0,float fx1,int *itr)
{
     *x=x0-((x1-x0)/(fx1-fx0))*fx0;
     ++(*itr);
     printf("iteration no.%3d x=%7.5f\n",*itr,*x);
}



ª       Results: (False-position Method)
----------METHOD OF FALSE POSITION FOR SOLVING NONLINEAR EQUATIONS-----------
enter starting values,max error,max iteration
2 9 .00005 99
iteration no.  1 x=8.24000
iteration no.  2 x=2.98913
iteration no.  3 x=3.99729
iteration no.  4 x=4.81304
iteration no.  5 x=5.33790
iteration no.  6 x=5.62234
iteration no.  7 x=5.76140
iteration no.  8 x=5.82585
iteration no.  9 x=5.85496
iteration no. 10 x=5.86796
iteration no. 11 x=5.87373
iteration no. 12 x=5.87629
iteration no. 13 x=5.87743
iteration no. 14 x=5.87793
iteration no. 15 x=5.87815
iteration no. 16 x=5.87825
iteration no. 17 x=5.87829
after 17 th iterations root=5.87828970

No comments:

Post a Comment