/*
Program to find out the roots of equation using regula-falsi method.
*/
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
float f(float x)
{
return(x*x*x-2*x-5);
}
void regular(float *m, float x0, float x1, float fx0,float fx1, int *itr)
{
*m=x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
printf(“Iteration no %d x= %7.5fn”,*itr,*m) ;
}
int main()
{
clrscr();
int itr=0,maxitr;
float x0,x1,x2,aerr,x3;
printf(“nnEnter the values of x0, x1, allowed error and maximum iterations==>n”);
scanf(“%f %f %f %d”,&x0,&x1,&aerr,&maxitr);
regular(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
if(f(x0)*f(x1)<0)
x1=x2;
else
x0=x2;
regular(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<aerr)
{
printf(“After%d Iterations, root=%6.4fn”,itr,x3);
return 0;
}
x2=x3;
}
while(itr<maxitr);
printf(“nSolution does not converge, Iterations not Sufficient”);
return 1;
}
1 response so far ↓
Piyush Upmanyu // February 3, 2009 at 9:22 pm |
Thanks a lot. Really needed this, though why’d you use pointers?
Couldn’t it have been done simply with float type variables?
Pointers make me go really crazy.
Thanks once again