Skip to content

Instantly share code, notes, and snippets.

@Colk-tech
Created October 13, 2023 03:07
Show Gist options
  • Save Colk-tech/811a2b6e99d8838300ca84e7572889a7 to your computer and use it in GitHub Desktop.
Save Colk-tech/811a2b6e99d8838300ca84e7572889a7 to your computer and use it in GitHub Desktop.
Bisection method for equations
#include <stdio.h>
#include <math.h>
double f(double x) {
double result = (x * x * x) - (3 * x) - 1;
return result;
}
int main(void) {
double a, b, c;
double eps = 0.0001;
printf("a,b=");
scanf("%lf,%lf", &a, &b);
do {
c = (a + b) / 2;
if (f(c) == 0) {
break;
}
if (f(b - a) < 0) {
b = c;
} else {
a = c;
}
} while (fabs(a - b) > eps);
printf("x=%f\n", c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment