Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. (*1*)
  2. intervalHalving[f_, a_, b_, eps_] :=
  3. Module[{xm = (a + b)/2, L = b - a, x1 = a + L/4, x2 = b - L/4},
  4. While[Abs[L] < eps, x1 = a + L/4;
  5. x2 = b - L/4;
  6. If[(f /. x -> x1) < f /. x -> xm, b = xm;
  7. xm = x1,
  8. If[(f /. x -> x2) < (f /. x -> xm),
  9. a = xm;
  10. xm = x2,
  11. a = x1;
  12. b = x2];
  13. ];
  14. L = b - a]
  15. Print["Optimal solution for function f: ", xm];
  16. ]
  17.  
  18.  
  19.  
  20.  
  21. intervalHalving[(x - 3)^2, -1, 5, 0.1]
  22. intervalHalving[x^4 + 5 x^2 - 10 x, -2, 4, 0.1]
  23.  
  24. Optimal solution for function f: 2
  25.  
  26. Optimal solution for function f: 1
  27.  
  28. (*2*)
  29. goldenPartition[f_, a_, b_, eps_] :=
  30. Module[{p = 0.382, xm, w1 = a + p*(b - a), w2 = a + (1 - p)*(b - a),
  31. fw1 = f /. x -> w1, fw2 = f /. x -> w2}, While[Abs[b - a] < eps,
  32. w1 = a + p*(b - a);
  33. w2 = a + (1 - p) (b - a);
  34. If[fw1 > fw2,
  35. a = w1,
  36. If[fw2 > fw1,
  37. b = w2,
  38. a = w1;
  39. b = w2];
  40. ];
  41. ];
  42. xm = (a + b)/2;
  43. Print["Approximate minimal point of function f is: ", xm];
  44. ]
  45.  
  46.  
  47. goldenPartition[(x - 3)^2, -1, 5, 0.5]
  48. goldenPartition[x^4 + 5 x^2 - 10 x, -2, 4, 0.5]
  49.  
  50.  
  51. Approximate minimal point of function f is: 2
  52.  
  53. Approximate minimal point of function f is: 1
  54.  
  55. (*3*)
  56. fibonacciNumbers[f_, a_, b_, n_] := Module[{k = 1, x1, x2, xm},
  57. L[k_] := Fibonacci[n - k + 1]/Fibonacci[n + 1];
  58. While[k == n, k = k + 1;
  59. x1 = a + L[k];
  60. x2 = b - L[k];
  61. If[x1 < x2, ,
  62. temp = x1;
  63. x1 = x2;
  64. x2 = temp];
  65. If[(f /. x -> x1) > (f /. x -> x2),
  66. a = x1,
  67. If[(f /. x -> x2) > (f /. x -> x1), b = x2, a = x1;
  68. b = x2];
  69. ];
  70. ];
  71. xm = (a + b)/2;
  72. Print["Approximate minimal point of function f: ", xm];
  73. ]
  74.  
  75.  
  76. fibonacciNumbers[(x - 3)^2, -1, 5, 4]
  77. fibonacciNumbers[x^4 + 5 x^2 - 10 x, -2, 4, 4]
  78.  
  79.  
  80. Approximate minimal point of function f: 2
  81.  
  82. Approximate minimal point of function f: 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement