Created
October 13, 2023 03:07
-
-
Save Colk-tech/811a2b6e99d8838300ca84e7572889a7 to your computer and use it in GitHub Desktop.
Bisection method for equations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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