Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. double e = 2.718281828;
  2. double eps;
  3. double deltha = 0.5;
  4. double goldenRatio = (1 + Math.Sqrt(5)) / 2;
  5. double x0, a, b, q, l, r1, r2;
  6. bool condition = false;
  7. double f(double x)
  8. {
  9. return (Math.Pow(x, 5) + x - 3);
  10. }
  11. double derf(double x)
  12. {
  13. return (5*Math.Pow(x, 4) + 1);
  14. }
  15.  
  16. Console.WriteLine("*SIMPLE ITERATION METHOD*");
  17. Console.WriteLine("Input precision");
  18. Console.Write("eps = ");
  19. eps = double.Parse(Console.ReadLine());
  20. Console.WriteLine("Input start of interval");
  21. Console.Write("a = ");
  22. a = double.Parse(Console.ReadLine());
  23. Console.WriteLine("Input end of interval");
  24. Console.Write("b = ");
  25. b = double.Parse(Console.ReadLine());
  26. x0 = (a + b) / 2;
  27. double a1 = a, b1 = b;
  28.  
  29. while (Math.Abs(b1 - a1) > eps)
  30. {
  31. r1 = b1 - (b1 - a1) / goldenRatio;
  32. r2 = a1 + (b1 - a1) / goldenRatio;
  33. if (Math.Abs(derf(r1)) <= Math.Abs(derf(r2)))
  34. a1 = r1;
  35. else
  36. b1 = r2;
  37. }
  38. l = 1/ Math.Abs(derf((a1 + b1) / 2));
  39.  
  40. double phi(double x)
  41. {
  42. return( x - l * (Math.Pow(x, 5) + x - 3));
  43. }
  44. double derphi(double x)
  45. {
  46. return (1 - l* (5 * Math.Pow(x, 4) + 1));
  47. }
  48.  
  49. a1 = a;
  50. b1 = b;
  51. while (Math.Abs(b1 - a1) > eps)
  52. {
  53. r1 = b1 - (b1 - a1) / goldenRatio;
  54. r2 = a1 + (b1 - a1) / goldenRatio;
  55. if (Math.Abs(derphi(r1)) <= Math.Abs(derphi(r2)))
  56. a1 = r1;
  57. else
  58. b1 = r2;
  59. }
  60. q = Math.Abs(derphi((a1 + b1) / 2));
  61.  
  62.  
  63. if ((phi(x0) - x0) < deltha * (1 - q)) condition = true;
  64. else
  65. {
  66. condition = false;
  67. Console.WriteLine("Convergence condition is not implemented");
  68. }
  69.  
  70. double x1 = 0;
  71. int i = 0;
  72. double abs = Math.Abs(x1 - x0);
  73.  
  74. if (condition)
  75. {
  76. while(abs>=eps)
  77. {
  78. i++;
  79. x1 = phi(x0);
  80. abs = Math.Abs(x1 - x0);
  81. x0 = x1;
  82. Console.WriteLine("x" + i + " = " + Math.Round(x1, 8));
  83.  
  84.  
  85. }
  86.  
  87. Console.WriteLine
  88. (
  89. "Approximated root: {0}; " +
  90. "Number of iterations: {1}; " +
  91. "Estimated number of iterations: {2};", Math.Round(x1, 8), i, Math.Round(Math.Log((b - a) / ((1 - q) * eps)) / Math.Log(1 / q), 0) + 1
  92. );
  93. }
  94. Console.WriteLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement