Method of Bisection for the Solution of Algebraic and Transcendental Equations–Programming in Language C
12:03 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 bisect(float *x,float a,float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
pf("iteration no %3d X = %7.3f\n",*itr,*x);
}
main()
{
int itr=0,maxitr;
float x,a,b,aerr,x1;
pf("enter the values of a,b,allowed error,maximum iterations\n");
sf("%f %f %f %d",&a,&b,&aerr,&maxitr);
bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
pf("after %d iterations root = %6.3f\n",itr,x1);
//return 0;
}
x=x1;
}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