Advertisement
dmkozyrev

task22

May 27th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <math.h>
  3. #include <stdio.h>
  4.  
  5. typedef double (*function) (double x) ;
  6.  
  7. double search_solution(double a, double b, double eps, function f){
  8.     double dx = b - a;
  9.     double x = dx / 2;
  10.  
  11.     while ( abs( (*f)(x) ) > eps ){
  12.         dx = dx / 2;
  13.         x = a + dx;
  14.         if ( (*f)(x) * (*f)(b) < 0 )
  15.             a = x;
  16.         else
  17.             b = x;
  18.     }
  19.  
  20.     return x;
  21. }
  22.  
  23. double F(double x){
  24.     return ( sin(x)-x+1 );
  25. }
  26.  
  27. int main(){
  28.     double x = search_solution(M_PI/2, M_PI, 0.000001, F);
  29.     printf("Solution: %f", x);
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement