23
2.c
Normal file
23
2.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define EPSILON 0.0000001
|
||||
#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36)
|
||||
double bisection(double a, double b) {
|
||||
double x;
|
||||
if (f(a) * f(b) > 0) {
|
||||
printf("The values of function at the respective initial guesses must have "
|
||||
"opposite signs");
|
||||
exit(1);
|
||||
}
|
||||
x = (a + b) / 2;
|
||||
if (f(x) == 0 || fabs(b - a) < EPSILON)
|
||||
return x;
|
||||
if (f(x) > 0)
|
||||
return bisection(x, b);
|
||||
return bisection(a, x);
|
||||
}
|
||||
int main() {
|
||||
printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf",
|
||||
bisection(1.6, -4));
|
||||
}
|
Reference in New Issue
Block a user