exp 1-6: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-26 19:14:30 +05:30
commit be590b5cfd
9 changed files with 386 additions and 0 deletions

23
2.c Normal file
View 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));
}