Skip to content

Instantly share code, notes, and snippets.

@ashim888
Created March 9, 2014 09:55
Show Gist options
  • Save ashim888/9445435 to your computer and use it in GitHub Desktop.
Save ashim888/9445435 to your computer and use it in GitHub Desktop.
BISECTION METHOD IN C
#include<stdio.h>
#include <math.h>
#define ESP 0.01
//#define F(x) (x)*(x)*(x) + (x)*(x) + (x) + 7
#define F(x) (x)*(x) - 3
//#define F(x) (x)*(x)*(x)-0.165*(x)*(x)+0.0003993
void main()
{
int i = 1;
float low,up,mid;
double flower,fupper,fmiddle;
float error;
printf("\nEnter the lower value: ");
scanf("%f",&low);
printf("\nEnter the upper value: ");
scanf("%f",&up);
printf("\n___________________________________________________________________________________________\n");
printf("\niter low\t mid\t up\t f(lower)\t f(mid)\t f(upper)");
printf("\n___________________________________________________________________________________________\n");
do
{
mid=(low+up)/2;
flower=F(low);
fupper=F(up);
fmiddle=F(mid);
printf("\n%d \t%f \t%f \t%f \t%lf \t%lf \t%lf", i, low,mid,up,flower,fmiddle,fupper);
if(flower*fmiddle<0)
{
up=mid;
}
else
{
low=mid;
}
i++;
error=up-low;
printf("\tERROR %d =%f",i,fabs(error));
}while(fabs(up-low)>ESP);
printf("\n______________________________________________________________________________________________\n");
printf("\n\nApp.root = %f\n\n",mid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment