Pages

Monday, 1 August 2011

Regula Falsi or False Position Method

 #include<stdio.h>  
 #include<math.h>  
 #include<conio.h>  
 #define EPS 0.0000005  
 float funct(float x);  
 int c,iter;  
 void main()  
 {  
 float x1,x2,f1,f2,f0,x0,root;  
 printf("Enter the number of iterations ");  
 scanf("%d",&iter);  
 //find positive root  

 for(x2=1;x2<=5.00;x2=x2+.1)  
 {  
       f2=funct(x2);  
       if(f2>0)  
       {  
             break;  
       }  
 }  
 //find the negative root  
 for(x1=x2-.1;x1>0;x1=x1-.1)  
 {  
       f1=funct(x1);  
       if(f1<0)  
             break;  
 }  
 x2=x0=0;  
 for(c=1;c<=iter;c++)  
 {  
       printf("\n ITERATION= %d",c);  
       printf("\t: ROOT= %f\tX1=%f\tX2=%f",x0,x1,x2);  
       x0=x1-funct(x1)*((x2-x1)/funct(x2)-funct(x1));  
       f0=funct(x0);  
       if(f0==0)  
       {  
             break;  
       }  
       if(funct(x0)*funct(x1)<0)  
       {  
             x2=x0;  
       }  
       else  
       {  
             x1=x0;  
       }  
       if(fabs((x1-x2)/x1)<EPS)  
             break;  
 }  
 printf("\n===========================================");  
 printf("\n\tROOT=%7.4f",x0);  
 printf("\n\tITERATION=%d",c-1);  
 getch();  
 }  
 float funct(float x)  
 {  
 return(x*x-x-2);  
 }  



No comments:

Post a Comment