Spaces:
Sleeping
Sleeping
File size: 2,003 Bytes
9f135d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | // Bisection method for root finding
// Bracketing method with guaranteed convergence
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define TOL 1e-10
#define MAX_ITER 100
#define NUM_TRIALS 10000
// Test function: x^3 - 2*x - 5 (root near 2.09)
double f1(double x) {
return x * x * x - 2.0 * x - 5.0;
}
// Test function: cos(x) - x (root near 0.739)
double f2(double x) {
return cos(x) - x;
}
// Test function: e^x - 3*x^2 (multiple roots)
double f3(double x) {
return exp(x) - 3.0 * x * x;
}
double bisection(double (*f)(double), double a, double b, double tol, int max_iter) {
if (f(a) * f(b) >= 0) {
return NAN; // No root or multiple roots in interval
}
double c = a;
for (int i = 0; i < max_iter; i++) {
c = (a + b) / 2.0;
double fc = f(c);
if (fabs(fc) < tol || fabs(b - a) < tol) {
return c;
}
if (f(a) * fc < 0) {
b = c;
} else {
a = c;
}
}
return c;
}
int main() {
clock_t start = clock();
double sum = 0.0;
int found = 0;
for (int iter = 0; iter < NUM_TRIALS; iter++) {
double root;
// Find different roots
if (iter % 3 == 0) {
root = bisection(f1, 2.0, 3.0, TOL, MAX_ITER);
} else if (iter % 3 == 1) {
root = bisection(f2, 0.0, 1.0, TOL, MAX_ITER);
} else {
root = bisection(f3, 0.0, 1.0, TOL, MAX_ITER);
}
if (!isnan(root)) {
sum += root;
found++;
}
}
clock_t end = clock();
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("Bisection method: %d trials, %.6f seconds\n",
NUM_TRIALS, time_spent);
printf("Found %d roots, average: %.10f\n", found, sum / found);
return 0;
}
|