Guest User

Untitled

a guest
May 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef double(*fun)(double x);
  5.  
  6. double f(double x)
  7. {
  8. return 0.1*pow(x,2)-x*log(x);
  9. }
  10. double g(double x)
  11. {
  12. return tan(x)-(1/3.0)*pow(tan(x),3)+(1/5.0)*pow(tan(x),5)-(1/3.0);
  13. }
  14. double f1(fun f, double x)
  15. {
  16. double h = 0.0000001;
  17. return (f(x+h)-f(x))/h;
  18. }
  19. double f2(fun f, double x)
  20. {
  21. double h = 0.0000001;
  22. return (f(x-h)+f(x+h)-2*f(x))/(h*h);
  23. }
  24. double epsilon()
  25. {
  26. long double x=1;
  27. for (;1+x/2 !=1;x=x/2){}
  28. return x;
  29. }
  30. double dihotomiya(double A, double B, fun f)
  31. {
  32. double eps=epsilon()*100;
  33. double a=A;
  34. double b=B;
  35. while(fabs(a-b) >eps )
  36. {
  37. if( f(a)*f((a+b)/2) > 0 )
  38. a=(a+b)/2;
  39. else
  40. b=(a+b)/2;
  41. }
  42. return (a+b)/2;
  43. }
  44. double interaziya(double A,double B, fun f)
  45. {
  46. double x=(A+B)/2;
  47. double a=A;
  48. int flag=0;
  49. long double eps=epsilon()*1e10;
  50. while(a<=B)
  51. {
  52. if(f1(f,a)<1)
  53. flag=1;
  54. a=a+eps;
  55. }
  56. while(fabs(f(x)) > eps && flag==1)
  57. x=(fabs(f(x))-x);
  58. if(flag==1)
  59. printf("Iteration: %f\n", x);
  60. else
  61. printf("Iteration: Don't exist\n");
  62. }
  63. double newton(double A,double B,fun f)
  64. {
  65. double x=(A+B)/2;
  66. double temp=0;
  67. double a=A;
  68. long double eps=epsilon()*1e10;
  69. int flag=0;
  70.  
  71. while(a<=B)
  72. {
  73. if(fabs(f(a)*f2(f,a))< pow(f1(f,a),2))
  74. flag=1;
  75. a=a+eps;
  76. }
  77. while(fabs(f(x)) > eps && flag ==1)
  78. x=x-f(x)/f1(f,x);
  79. if(flag==1)
  80. printf("Newton: %f\n", x);
  81. else
  82. printf("Newton: Don't exist\n");
  83. }
  84.  
  85. int main()
  86. {
  87. long i;
  88. printf("Computer epsilon: %e\n", epsilon());
  89. printf("Variant 20\n");
  90. printf("Dichotomy: %f\n", dihotomiya(1, 2,f));
  91. interaziya(1, 2,f);
  92. newton(1, 2,f);
  93. printf("Variant 21\n");
  94. printf("Dichotomy: %f\n", dihotomiya(0, 0.8, g));
  95. interaziya(0, 0.8, g);
  96. newton(0, 0.8, g);
  97. scanf("%d",&i);
  98. return 0;
  99. }
Add Comment
Please, Sign In to add comment