Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #define EPS 1e-15
- //prototypes
- double f(double);
- void solve(double (*f)(double), double, double);
- double root(double (*f)(double), double, double);
- //----------
- double f(double x) { return (x*x-9)*(x-1)*sin(x)/(x*x*x+2*x*x+9); }
- int main(void)
- {
- double l = -4, r = 4; //границы корней
- solve(f,l,r);
- return 0;
- }
- void solve(double (*f)(double x), double l, double r)
- {
- const int n = 1e+8 + 3; int i;
- double h = (r-l)/n, x1 = l, x2;
- for (i=0;i<n;i++)
- {
- x2 = l + i*h;
- if (f(x1)*f(x2)<0)
- printf("root = %.10f\n",root(f,x1,x2));
- x1 = x2;
- }
- }
- double root(double (*f)(double x), double a, double b)
- {
- double c, fa = f(a), fb = f(b), fc;
- if (fabs(fa)<EPS)
- return a;
- if (fabs(fb)<EPS)
- return b;
- while (b-a>EPS)
- {
- c = (a+b)/2;
- fc = f(c);
- if (fa*fc < 0)
- b = c;
- else
- a = c;
- }
- return (a+b)/2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement