Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "f.h"
  4. int solve(double (*g)(double y),double (*f)(double t),double a,double b, double eps,double k)
  5. {
  6. double x1,x2,fx1,fx2,fc,c,n,otv,d,r;
  7. int i;
  8. x1=a;
  9. x2=a+k;
  10. n=(b-a)/k;
  11. for(i=2;i<=n;i++)
  12. {
  13. fx1=f(x1);
  14. fx2=f(x2);
  15. if(fx1*fx2<0)
  16. {
  17. while(x2-x1>eps)
  18. {
  19. c=(x1+x2)/2;
  20. if((x1==c)||(x2==c))
  21. {
  22. otv=c;
  23. printf("%lf\n",otv);
  24. break;
  25. }
  26. fc=f(c);
  27. if(fx1*fc<0)
  28. {
  29. x2=c;
  30. fx2=fc;
  31. }
  32. else
  33. {
  34. x1=c;
  35. fx1=fc;
  36. }
  37. }
  38. otv=c;
  39. printf("%lf\n",otv);
  40. }
  41. else
  42. {
  43. d=fun(g,x1,x2,eps,&r);
  44. if(fabs(d)<eps)
  45. {
  46. printf("%lf\n",r);
  47. }
  48. }
  49. x1=x2;
  50. x2=a+i*k;
  51. }
  52. return 0;
  53. }
  54.  
  55.  
  56. double fun(double(*f)(double y),double a,double b,double eps,double *x)
  57. {
  58. double x1,x2,fx1,fx2,alfa=2/(3-sqrt(5));
  59. x1=a+(b-a)/alfa;
  60. x2=b-(b-a)/alfa;
  61. fx1=f(x1);
  62. fx2=f(x2);
  63. while(fabs(a-b)>eps)
  64. {
  65. if(fabs(a-x1)<eps || fabs(x2-x1)<eps || fabs(b-x2)<eps)
  66. {
  67. *x=f(a);
  68. }
  69. if(fx1>fx2)
  70. {
  71. b=x2;
  72. x2=x1;
  73. x1=a+(b-a)/alfa;
  74. fx2=fx1;
  75. fx1=f(x1);
  76. }
  77. else
  78. {
  79. a=x1;
  80. x1=x2;
  81. x2=b-(b-a)/alfa;
  82. fx1=fx2;
  83. fx2=f(x2);
  84. }
  85. }
  86. *x=x1;
  87. return fx1;;
  88. }
  89. double g(double x)
  90. {
  91. return fabs(f(x));
  92. }
  93.  
  94. double f(double x)
  95. {
  96.  
  97. return x*x*(x-1);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement