Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*1*)
- intervalHalving[f_, a_, b_, eps_] :=
- Module[{xm = (a + b)/2, L = b - a, x1 = a + L/4, x2 = b - L/4},
- While[Abs[L] < eps, x1 = a + L/4;
- x2 = b - L/4;
- If[(f /. x -> x1) < f /. x -> xm, b = xm;
- xm = x1,
- If[(f /. x -> x2) < (f /. x -> xm),
- a = xm;
- xm = x2,
- a = x1;
- b = x2];
- ];
- L = b - a]
- Print["Optimal solution for function f: ", xm];
- ]
- intervalHalving[(x - 3)^2, -1, 5, 0.1]
- intervalHalving[x^4 + 5 x^2 - 10 x, -2, 4, 0.1]
- Optimal solution for function f: 2
- Optimal solution for function f: 1
- (*2*)
- goldenPartition[f_, a_, b_, eps_] :=
- Module[{p = 0.382, xm, w1 = a + p*(b - a), w2 = a + (1 - p)*(b - a),
- fw1 = f /. x -> w1, fw2 = f /. x -> w2}, While[Abs[b - a] < eps,
- w1 = a + p*(b - a);
- w2 = a + (1 - p) (b - a);
- If[fw1 > fw2,
- a = w1,
- If[fw2 > fw1,
- b = w2,
- a = w1;
- b = w2];
- ];
- ];
- xm = (a + b)/2;
- Print["Approximate minimal point of function f is: ", xm];
- ]
- goldenPartition[(x - 3)^2, -1, 5, 0.5]
- goldenPartition[x^4 + 5 x^2 - 10 x, -2, 4, 0.5]
- Approximate minimal point of function f is: 2
- Approximate minimal point of function f is: 1
- (*3*)
- fibonacciNumbers[f_, a_, b_, n_] := Module[{k = 1, x1, x2, xm},
- L[k_] := Fibonacci[n - k + 1]/Fibonacci[n + 1];
- While[k == n, k = k + 1;
- x1 = a + L[k];
- x2 = b - L[k];
- If[x1 < x2, ,
- temp = x1;
- x1 = x2;
- x2 = temp];
- If[(f /. x -> x1) > (f /. x -> x2),
- a = x1,
- If[(f /. x -> x2) > (f /. x -> x1), b = x2, a = x1;
- b = x2];
- ];
- ];
- xm = (a + b)/2;
- Print["Approximate minimal point of function f: ", xm];
- ]
- fibonacciNumbers[(x - 3)^2, -1, 5, 4]
- fibonacciNumbers[x^4 + 5 x^2 - 10 x, -2, 4, 4]
- Approximate minimal point of function f: 2
- Approximate minimal point of function f: 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement