1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
fun = @(x) x(1)^2 + x(2)^2;
A = []; b = []; Aeq = [1 1]; beq = 1; lb = [-Inf, -Inf]; ub = [Inf, 1/4];
x0 = [0, 0];
options = optimoptions('fmincon','Display','iter','Algorithm','interior-point'); [x,fval] = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, [], options);
disp('The solution is:'), disp(x) disp('The minimum value of the objective function is:'), disp(fval)
fun = @(x) x(1)^2 + x(2)^2 + x(3)^2; g = @(x) abs(x(1) + 2*x(2) - x(3) - 4) + abs(x(1) - x(2) + x(3) + 2); P = @(x, r) fun(x) + r * g(x);
x0 = [0, 0, 0]; r = 1;
options = optimoptions('fminunc','Display','iter','Algorithm','quasi-newton'); [x,fval] = fminunc(@(x) P(x, r), x0, options);
disp('The solution is:'), disp(x) disp('The minimum value of the objective function is:'), disp(fval)
fun = @(x) -(x + 5*sin(5*x) + 10*cos(4*x));
numberOfVariables = 1;
lb = 0; ub = 10;
[x,fval] = ga(fun,numberOfVariables,[],[],[],[],lb,ub);
disp('The solution is:'), disp(x) disp('The maximum value of the objective function is:'), disp(-fval)
fun = @(x) x(1)^2 + x(2)^2; g = @(x) x(1) - 2; P = @(x, r) fun(x) - r * log(g(x));
x0 = [3, 0]; r = 0.0001;
options = optimoptions('fminunc','Display','iter','Algorithm','quasi-newton'); [x,fval] = fminunc(@(x) P(x, r), x0, options);
disp('The solution is:'), disp(x) disp('The minimum value of the objective function is:'), disp(fval)
fun = @(x) sum(x.^2);
alphas = [0.001,0.1,0.5, 1, 2, 5]; T = 100; T_min = 1e-3; cooling_rate = 0.95; max_iter = 100;
lb = -15 * ones(1, 10); ub = 15 * ones(1, 10);
results = zeros(length(alphas), 2);
for a = 1:length(alphas) x = lb + (ub - lb) .* rand(1, 10); alpha = alphas(a);
T_current = T; while T_current > T_min for i = 1:max_iter x_new = x + alpha * (2*rand(1, 10) - 1); x_new = max(min(x_new, ub), lb);
delta_f = fun(x_new) - fun(x);
if delta_f < 0 || rand() < exp(-delta_f / T_current) x = x_new; end end
T_current = cooling_rate * T_current; end
results(a, :) = [alpha, fun(x)];
fprintf('For alpha = %f:\n', alpha); disp('The solution is:'), disp(x) disp('The minimum value of the objective function is:'), disp(fun(x)) end
figure; semilogy(results(:, 1), results(:, 2), '-o', 'Color', [0.2 0.4 0.6], 'LineWidth', 2, 'MarkerSize', 8); xlabel('Alpha'); ylabel('Minimum Value of Objective Function'); title('Impact of Alpha on the Solution');
|