Guest User

Untitled

a guest
May 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define K 10000000 // параметр K
  4. double epsilon; //значение машинного эпсилон
  5. double f(double x); //функция F(x), вариант #9
  6. double f_for_iteration(double x); //функций f(x) для вычисление методом итераций
  7. double g(double x); // функция G(x), вариант #23
  8. double g_for_iteration(double x); // функция g(x) для вычисления методом итераций
  9. typedef double(*FUNCTION)(double x);
  10. double dichotomy(FUNCTION f, double a, double b); // метод дихотомии
  11. double iteration(FUNCTION f, double a, double b); // метод итерации
  12. double Newton(FUNCTION f, double a, double b); // метод Ньютона
  13. double diff(FUNCTION f, double x); // производная функции в точке x
  14. double diff_second(FUNCTION f, double x); // вторая производная функции в точке x
  15. double iteration_check(FUNCTION f, double a, double b); // провера сходимости метода итераций
  16. double Newton_check(FUNCTION f, double a, double b); // проверка сходимости метода Ньютона
  17. int nDichotomy=0;
  18. int nIteration=0;
  19. int nNewton=0;
  20.  
  21. double f(double x)
  22. {
  23. return acos(x)-sqrt(1-0.3*x*x*x);
  24. }
  25. double f_for_iteration(double x)
  26. {
  27. return cos(sqrt(1-0.3*x*x*x));
  28. }
  29. double dichotomy(FUNCTION f, double a, double b)
  30. {
  31. double a0, b0;
  32. while(fabs(a - b) > epsilon*K)
  33. {
  34. a0 = a;
  35. b0 = b;
  36. ++nDichotomy;
  37. if (f(a0)*f((a0+b0)/2.0)>0)
  38. {
  39. a = (a0 + b0)/2.0;
  40. b = b0;
  41. }
  42. else
  43. {
  44. a = a0;
  45. b = (a0+b0)/2.0;
  46. }
  47. }
  48. return (a+b)/2;
  49. }
  50. double iteration(FUNCTION f, double a, double b)
  51. {
  52. double x0 = (a+b)/2.0;
  53. double x = f(x0);
  54. while(fabs(x - x0)>epsilon*K)
  55. {
  56. ++nIteration;
  57. x0 = x;
  58. x = f(x0);
  59. }
  60. return x;
  61. }
  62. double diff(FUNCTION f, double x)
  63. {
  64. double h = 0.0000001;
  65. return (f(x+h)-f(x))/h;
  66. }
  67. double diff_second(FUNCTION f, double x)
  68. {
  69. double h = 0.0000001;
  70. return (f(x-h)+f(x+h)-2*f(x))/(h*h);
  71. }
  72. double Newton(FUNCTION f, double a, double b)
  73. {
  74. double x0 = (a+b)/2.0;
  75. double x = x0-f(x0)/diff(f, x0);
  76. while(fabs(x - x0)>epsilon)
  77. {
  78. ++nNewton;
  79. x0 = x;
  80. x = x0-f(x0)/diff(f, x0);
  81. }
  82. return x;
  83. }
  84. double iteration_check(FUNCTION f, double a, double b)
  85. {
  86. double step = 0.005;
  87. double x;
  88. for(x=a; x<b; x+=step)
  89. {
  90. if(fabs(diff(f, x))>1.0)
  91. {
  92. return 0;
  93. break;
  94. }
  95. }
  96. return 1;
  97. }
  98. double Newton_check(FUNCTION f, double a, double b)
  99. {
  100. double step = 0.005;
  101. double x, differential;
  102. for(x=a; x<b; x+=step)
  103. {
  104. differential = diff(f, x);
  105. if(fabs(f(x)*diff_second(f,x))>differential*differential)
  106. {
  107. return 0;
  108. break;
  109. }
  110. }
  111. return 1;
  112. }
  113.  
  114. int main()
  115. {
  116. // вычисляем машинный эпсилон fixed
  117. epsilon = 1.0;
  118. while(epsilon/2.0 + 1.0 > 1.0)
  119. {
  120. epsilon = epsilon/2.0;
  121. }
  122.  
  123. // вариант #22
  124. printf("Problem#22\n");
  125. printf("Dichotomy: %f,Steps:%d\n", dichotomy(f, 0.0, 1.0),nDichotomy);
  126. if(iteration_check(f_for_iteration, 0.0, 1.0))
  127. printf("Iteration: %f,Steps:%d\n", iteration(f_for_iteration, 0.0, 1.0),nIteration);
  128. else
  129. printf("Iteration: No");
  130. if(Newton_check(f, 0.0, 1.0))
  131. printf("Newton: %f, Steps:%d\n", Newton(f, 0.0, 1.0),nNewton);
  132. else
  133. printf("Newton: No");
  134. return 0;
  135. }
Add Comment
Please, Sign In to add comment