Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define TOL 1e-6
- double root(double a, double b, double(*f)(double)) {
- double mid = (a + b) / 2;
- if ((b - a)/2 <= TOL) {
- return mid;
- }
- if (f(a) * f(mid) < 0) {
- return root(a, mid, f);
- }
- return root(mid, b, f);
- }
- double f(double x) {
- return 2*x*x*x - 6*x - 1;
- }
- int main() {
- printf("%g\n", root(1, 2, f));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement