write a C program using Full NR-Bisection algorithm to find the roots
时间: 2023-05-22 09:02:58 浏览: 105
np完全近似算法c语言
Sure, I can help you with that. Here's an example C program using Full NR-Bisection algorithm to find the roots:
```c
#include <stdio.h>
#include <math.h>
double func(double x) {
// define the function whose roots we want to find
return x*x - 3;
}
int main() {
double a = 1, b = 2, c, eps = 1e-6;
int iter = 0, maxiter = 100;
while (iter < maxiter) {
c = (a + b) / 2; // compute the midpoint of the interval
if (fabs(func(c)) < eps) {
// if we've found a root within the epsilon tolerance, break out of the loop
break;
}
if (func(a) * func(c) < 0) {
// if the sign of the function differs between a and c, then the root is in the left half
b = c;
} else {
// otherwise, the root is in the right half
a = c;
}
iter++; // increment iteration counter
}
printf("Root is %lf\n", c);
return 0;
}
```
This program uses the Full NR-Bisection algorithm to find the root of the function x^2 - 3 with an epsilon tolerance of 1e-6. The algorithm works by repeatedly bisecting the interval [a, b] and checking which half the root lies in. The program returns the approximate root after a maximum of 100 iterations.
阅读全文