Skip to content

Instantly share code, notes, and snippets.

@Kinjalrk2k
Created January 22, 2020 10:11
Show Gist options
  • Save Kinjalrk2k/cf5dc397405a4a131263358c53b8926c to your computer and use it in GitHub Desktop.
Save Kinjalrk2k/cf5dc397405a4a131263358c53b8926c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#define ex 0.01
double foo(double x){
return pow(x,3) - x - 4;
}
double bisection(){
int n = 1, i;
double a = 0.0;
while(foo(a)*foo(a+1) > 0)
a++;
double b = a+1, x0;
printf("%cn\t%ca\t%cb\t%cf(a)\t%cf(b)\t%cx0\t%cf(x0)\t%c(b-a)\t%c\n", 179, 179, 179, 179, 179, 179, 179, 179, 179, 179);
printf("%c",195);
for(i=0; i<63; i++)
printf("%c", 196);
printf("%c",180);
printf("\n");
do{
x0 = (a+b)/2;
printf("%c%d\t%c%.2lf\t%c%.2lf\t%c%.2lf\t%c%.2lf\t%c%.2lf\t%c%.2lf\t%c%.2lf\t%c\n", 179, n++, 179, a, 179, b, 179, foo(a), 179, foo(b), 179, x0, 179, foo(x0), 179, (b-a), 179);
if(foo(x0)>0)
b = x0;
else if(foo(x0)<0)
a = x0;
else
return x0;
}while((b-a)>ex);
return x0;
}
int main(void) {
double b = bisection();
int i;
printf("%c",192);
for(i=0; i<63; i++)
printf("%c", 196);
printf("%c",217);
printf("\n\nThe root is: %g", b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment