Regula-Falsi Method or Method of False position for the Solution of Algebraic and Transcendental Equations–Programming in Language C
12:07 AM Edit This 0 Comments »#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pf printf
#define sf scanf
float f(float x)
{
return (YOUR FUNCTION HERE);
}
void regula(float *x,float x0,float x1,float fx0, float fx1,int *itr)
{
*x= x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
pf("iteration no %3d X = %7.8f\n",*itr,*x);
}
main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,aerr;
pf("enter the values of x0,x1,allowed error,maximum iterations\n");
sf("%f %f %f %d",&x0,&x1,&aerr,&maxitr);
regula(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
if(f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
regula(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<aerr)
{
pf("after %d iterations root = %6.3f\n",itr,x3);
//return 0;
}
x2=x3;
}while (itr<maxitr);
pf("solution does not converge\niteration not sufficient");
//return 1;
getch();
}
BY – RAMEN MUKHERJEE
PS : adjust the value of error & output float range to get accurate value efficiently.
0 comments:
Post a Comment