Facebook
From DK, 4 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 222
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. double f(double x)
  7. {
  8. return x*x+x-1;
  9. }
  10.  
  11. double fp(double x)
  12. {
  13. return 2*x+1;
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19. double eps0,epsx,x0,x1,f0,f1;
  20. int i;
  21. eps0=1e-4;
  22. epsx=1e-4;
  23.  
  24.  
  25. printf("Podaj punkt startowy x0 =");
  26. scanf("%lf", &x0);
  27.  
  28. x1 = x0 -1;
  29. f0 = f(x0);
  30. i=64;
  31.  
  32. while (i && (fabs(x1 - x0) > epsx) && (fabs(f0) >eps0))
  33. {
  34.     f1 = fp(x0);
  35.     if(fabs(f1) < eps0)
  36.     {
  37.         printf("zly punkt startowy");
  38.         i=0;
  39.         break;
  40.     }
  41.     x1 = x0;
  42.     x0 = x0 - f0 / f1;
  43.     f0 = f(x0);
  44.     if(!(--i))
  45.         printf("przekroczono limit obiegow");
  46. }
  47. if(i) printf("x0= %lf",x0);
  48. printf("\n-----------------------------------------------------");
  49.  
  50.  
  51.  
  52.     return 0;
  53. }
  54.