Advertisement
2607

Untitled

Mar 12th, 2020
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. float Func(float x)
  5. {
  6.     return x - sin(x);
  7. }
  8. float dFunc(float x)
  9. {
  10.     return 1 - cos(x);
  11. }
  12.  
  13. float solve(float x, float e)
  14. {
  15.     float x0;
  16.     int i = 0;
  17.     printf("%i x: %f\n", i, x);
  18.     do
  19.     {
  20.         i++;
  21.         x0 = x;
  22.         x = x0 - (Func(x)/dFunc(x));
  23.         printf("%i x: %f\n", i, x);
  24.     }
  25.     while(fabs(x-x0) > .1*e);
  26.     return x;
  27. }
  28.  
  29. int main() {
  30.     float root, x0;
  31.     float e;
  32.    
  33.     printf("Enter precision");
  34.     scanf("%f", &e);
  35.     printf("\n");
  36.     printf("Enter x0: ");
  37.     scanf("%f", &x0);
  38.     printf("\n");
  39.  
  40.     printf("e = %f, x0 = %f\n", e, x0);
  41.  
  42.     root = solve(x0, e);
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement