MATLAB Command Window Page 1
>> % Define the original objective function
original_obj_fn = @(x1, x2) (x1^2 + x2 - 11)^2 + (x1 + x2^2 - 7)^2;
% Compute the gradient with respect to x1
gradient_fx1 = @(x1, x2) 2 * (x1^2 + x2 - 11) * (2 * x1) + 2 * (x1 + x2^2 - 7);
% Compute the gradient with respect to x2
gradient_fx2 = @(x1, x2) 2 * (x1^2 + x2 - 11) + 2 * (x1 + x2^2 - 7) * (2 * x2);
% Define the constraints
g1 = @(x1, x2) 26 - (x1 - 5)^2 - x2^2;
g2 = @(x1, x2) -x1;
g3 = @(x1, x2) -x2;
% Calculate the derivatives of the constraints with respect to x1 and x2
gradient_g1_wrt_x1 = @(x1, x2) -2 * (x1 - 5);
gradient_g1_wrt_x2 = @(x1, x2) -2 * x2;
gradient_g2_wrt_x1 = @(x1, x2) -1;
gradient_g2_wrt_x2 = @(x1, x2) 0;
gradient_g3_wrt_x1 = @(x1, x2) 0;
gradient_g3_wrt_x2 = @(x1, x2) -1;
p_g = 0.1; % Set the initial penalty parameter
x1 = 0; % Initial guess for variable x1
x2 = 0; % Initial guess for variable x2
e = 0.05; % Define the precision for convergence
iter_limit = 100; % Set the maximum number of iterations
for l=1:4
for k = 1:iter_limit
% Calculate the penalty terms based on the constraints
pen_g1 = max(0, g1(x1, x2));
pen_g2 = max(0, g2(x1, x2));
pen_g3 = max(0, g3(x1, x2));
% Construct the penalty function
penalty_function = original_obj_fn(x1, x2) + p_g * (pen_g1^2 + pen_g2^2 +
pen_g3^2);
% Calculate the gradients of the penalty function
grad_pen_fun_wrt_x1 = @(x1, x2) gradient_fx1(x1, x2) + 2 * p_g * (pen_g1 *
gradient_g1_wrt_x1(x1, x2) + pen_g2 * gradient_g2_wrt_x1(x1, x2) + pen_g3 *
gradient_g3_wrt_x1(x1, x2));
grad_pen_fun_wrt_x2 = @(x1, x2) gradient_fx2(x1, x2) + 2 * p_g * (pen_g1 *
gradient_g1_wrt_x2(x1, x2) + pen_g2 * gradient_g2_wrt_x2(x1, x2) + pen_g3 *
gradient_g3_wrt_x2(x1, x2));
% Determine lamda for x1
lamda_x1 = fminbnd(@(lamda_x1) original_obj_fn(x1 - lamda_x1 *
grad_pen_fun_wrt_x1(x1, x2), x2) + p_g * (max(0, g1(x1 - lamda_x1 *
grad_pen_fun_wrt_x1(x1, x2), x2))^2 + ...
max(0, g2(x1 - lamda_x1 * grad_pen_fun_wrt_x1(x1, x2), x2))^2 + max(0,
g3(x1 - lamda_x1 * grad_pen_fun_wrt_x1(x1, x2), x2))^2), ...
MATLAB Command Window Page 2
-1, 1);
% Determine lamda for x2
lamda_x2 = fminbnd(@(lamda_x2) original_obj_fn(x1, x2 - lamda_x2 *
grad_pen_fun_wrt_x2(x1, x2)) + p_g * (max(0, g1(x1, x2 - lamda_x2 *
grad_pen_fun_wrt_x2(x1, x2)))^2 + ...
max(0, g2(x1, x2 - lamda_x2 * grad_pen_fun_wrt_x2(x1, x2)))^2 + max(0,
g3(x1, x2 - lamda_x2 * grad_pen_fun_wrt_x2(x1, x2)))^2), ...
-1, 1);
% Update x1 and x2 with the new values
x1_new = x1 - lamda_x1 * grad_pen_fun_wrt_x1(x1, x2);
x2_new = x2 - lamda_x2 * grad_pen_fun_wrt_x2(x1, x2);
% Recalculate penalty terms
pen_g1_new = max(0, g1(x1_new, x2_new));
pen_g2_new = max(0, g2(x1_new, x2_new));
pen_g3_new = max(0, g3(x1_new, x2_new));
new_penalty_fn = original_obj_fn(x1_new, x2_new) + p_g * (pen_g1_new^2 +
pen_g2_new^2 + pen_g3_new^2);
% Output
fprintf('Iteration: %d, Function Value: %.6f, x: [%f, %f], p_g: %.6f\n' ,
...
k, new_penalty_fn, x1_new, x2_new, p_g);
% Check for convergence
if abs(new_penalty_fn - penalty_function) < e
break;
end
% Update x1 and x2 for the next iteration
x1 = x1_new;
x2 = x2_new;
end
% Increase the penalty parameter for the next iteration
p_g = p_g * 10;
end
Iteration: 1, Function Value: 46.927116, x: [3.212607, 2.897679], p_g: 0.100000
Iteration: 2, Function Value: 54.643040, x: [-2.793639, -2.208435], p_g: 0.100000
Iteration: 3, Function Value: 77.811116, x: [3.492771, 3.131410], p_g: 0.100000
Iteration: 4, Function Value: 52.961172, x: [-2.797239, -2.255788], p_g: 0.100000
Iteration: 5, Function Value: 78.147115, x: [3.496873, 3.131420], p_g: 0.100000
Iteration: 6, Function Value: 52.958876, x: [-2.796869, -2.256464], p_g: 0.100000
Iteration: 7, Function Value: 78.152020, x: [3.496932, 3.131421], p_g: 0.100000
Iteration: 8, Function Value: 52.958667, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 9, Function Value: 78.152093, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 10, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 11, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 12, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 13, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 14, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
MATLAB Command Window Page 3
Iteration: 15, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 16, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 17, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 18, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 19, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 20, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 21, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 22, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 23, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 24, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 25, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 26, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 27, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 28, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 29, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 30, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 31, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 32, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 33, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 34, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 35, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 36, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 37, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 38, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 39, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 40, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 41, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 42, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 43, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 44, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 45, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 46, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 47, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 48, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 49, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 50, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 51, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 52, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 53, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 54, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 55, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 56, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 57, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 58, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 59, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 60, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 61, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 62, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 63, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 64, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 65, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 66, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 67, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
MATLAB Command Window Page 4
Iteration: 68, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 69, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 70, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 71, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 72, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 73, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 74, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 75, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 76, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 77, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 78, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 79, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 80, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 81, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 82, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 83, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 84, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 85, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 86, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 87, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 88, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 89, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 90, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 91, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 92, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 93, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 94, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 95, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 96, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 97, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 98, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 99, Function Value: 78.152094, x: [3.496933, 3.131422], p_g: 0.100000
Iteration: 100, Function Value: 52.958664, x: [-2.796868, -2.256474], p_g: 0.100000
Iteration: 1, Function Value: 29.309594, x: [-3.461156, 3.131032], p_g: 1.000000
Iteration: 2, Function Value: 7.626650, x: [-2.718545, 3.131251], p_g: 1.000000
Iteration: 3, Function Value: 7.626520, x: [-2.717499, 3.129729], p_g: 1.000000
Iteration: 1, Function Value: 54.822080, x: [-1.776983, 3.129729], p_g: 10.000000
Iteration: 2, Function Value: 54.755908, x: [-1.776759, 3.089678], p_g: 10.000000
Iteration: 3, Function Value: 54.755097, x: [-1.769154, 3.089662], p_g: 10.000000
Iteration: 1, Function Value: 61.533006, x: [0.939708, 3.089662], p_g: 100.000000
Iteration: 2, Function Value: 1111.919199, x: [0.945319, -2.994656], p_g:
100.000000
Iteration: 3, Function Value: 62.749848, x: [0.873798, 3.088867], p_g: 100.000000
Iteration: 4, Function Value: 1143.889013, x: [0.944282, -2.903077], p_g:
100.000000
Iteration: 5, Function Value: 64.776966, x: [0.756036, 3.090282], p_g: 100.000000
Iteration: 6, Function Value: 1358.158766, x: [0.945856, -2.729694], p_g:
100.000000
Iteration: 7, Function Value: 66.673038, x: [0.635242, 3.097790], p_g: 100.000000
Iteration: 8, Function Value: 1816.209079, x: [0.951142, -2.534129], p_g:
100.000000
Iteration: 9, Function Value: 70.312844, x: [0.424118, 3.160330], p_g: 100.000000
Iteration: 10, Function Value: 67.815707, x: [-0.035027, 2.827094], p_g: 100.000000
MATLAB Command Window Page 5
Iteration: 11, Function Value: 67.577141, x: [-0.011767, 2.903334], p_g: 100.000000
Iteration: 12, Function Value: 60.450124, x: [0.809663, 2.899736], p_g: 100.000000
Iteration: 13, Function Value: 981.400674, x: [-0.016089, -2.810469], p_g:
100.000000
Iteration: 14, Function Value: 64.672984, x: [0.547594, 2.899804], p_g: 100.000000
Iteration: 15, Function Value: 91.208945, x: [0.807343, 2.803625], p_g: 100.000000
Iteration: 16, Function Value: 963.379876, x: [0.715440, -2.800638], p_g:
100.000000
Iteration: 17, Function Value: 68.190817, x: [-0.011568, 2.768977], p_g: 100.000000
Iteration: 18, Function Value: 63.803011, x: [0.609739, 2.899787], p_g: 100.000000
Iteration: 19, Function Value: 98.970903, x: [0.807299, 2.791830], p_g: 100.000000
Iteration: 20, Function Value: 962.748983, x: [0.732536, -2.800593], p_g:
100.000000
Iteration: 21, Function Value: 68.020531, x: [-0.011903, 2.788941], p_g: 100.000000
Iteration: 22, Function Value: 65.877925, x: [0.445231, 2.899663], p_g: 100.000000
Iteration: 23, Function Value: 80.201155, x: [0.807101, 2.822728], p_g: 100.000000
Iteration: 24, Function Value: 967.094585, x: [0.742353, -2.808354], p_g:
100.000000
Iteration: 25, Function Value: 67.835454, x: [-0.012517, 2.815507], p_g: 100.000000
Iteration: 26, Function Value: 67.347641, x: [0.251902, 2.899894], p_g: 100.000000
Iteration: 27, Function Value: 67.644623, x: [-0.016660, 2.856967], p_g: 100.000000
Iteration: 28, Function Value: 65.489619, x: [0.481422, 2.900490], p_g: 100.000000
Iteration: 29, Function Value: 84.175865, x: [0.807691, 2.816073], p_g: 100.000000
Iteration: 30, Function Value: 971.077298, x: [0.731759, -2.814287], p_g:
100.000000
Iteration: 31, Function Value: 68.019803, x: [-0.012698, 2.789086], p_g: 100.000000
Iteration: 32, Function Value: 64.271429, x: [0.577181, 2.899786], p_g: 100.000000
Iteration: 33, Function Value: 94.556993, x: [0.807034, 2.797953], p_g: 100.000000
Iteration: 34, Function Value: 967.392327, x: [0.722806, -2.807696], p_g:
100.000000
Iteration: 35, Function Value: 68.019554, x: [-0.012205, 2.789084], p_g: 100.000000
Iteration: 36, Function Value: 65.335909, x: [0.494310, 2.899710], p_g: 100.000000
Iteration: 37, Function Value: 85.003065, x: [0.807108, 2.813734], p_g: 100.000000
Iteration: 38, Function Value: 969.569106, x: [0.734019, -2.811938], p_g:
100.000000
Iteration: 39, Function Value: 67.874420, x: [-0.012598, 2.809309], p_g: 100.000000
Iteration: 40, Function Value: 66.848528, x: [0.335082, 2.899893], p_g: 100.000000
Iteration: 41, Function Value: 71.517999, x: [0.807266, 2.842554], p_g: 100.000000
Iteration: 42, Function Value: 965.806613, x: [0.759008, -2.803217], p_g:
100.000000
Iteration: 43, Function Value: 61.082407, x: [0.746731, 2.840276], p_g: 100.000000
Iteration: 44, Function Value: 63.438865, x: [0.766374, 2.811952], p_g: 100.000000
Iteration: 45, Function Value: 943.675126, x: [0.747499, -2.745684], p_g:
100.000000
Iteration: 46, Function Value: 61.937036, x: [0.695464, 2.816100], p_g: 100.000000
Iteration: 47, Function Value: 66.602748, x: [0.750333, 2.774829], p_g: 100.000000
Iteration: 48, Function Value: 923.963566, x: [0.719473, -2.722796], p_g:
100.000000
Iteration: 49, Function Value: 61.916212, x: [0.693412, 2.779216], p_g: 100.000000
Iteration: 50, Function Value: 61.399206, x: [0.726251, 2.775211], p_g: 100.000000
Iteration: 51, Function Value: 68.087433, x: [-0.008471, 2.780625], p_g: 100.000000
Iteration: 52, Function Value: 67.580025, x: [-0.008655, 2.899174], p_g: 100.000000
Iteration: 53, Function Value: 60.407860, x: [0.806780, 2.899255], p_g: 100.000000
MATLAB Command Window Page 6
Iteration: 54, Function Value: 60.407280, x: [0.806837, 2.899587], p_g: 100.000000
>>