type optimizationtry.m
clc
clear
% Define lower and upper bounds for the design variables
lb = [100; 100; 20; 30; 10; 150; 10; 30; 10; 10; 10; 3.14; 3.14; 1.57; 1.57; 0; 0; 0; 100; 100;];
ub = [200; 200; 40; 60; 40; 260; 40; 60; 40; 50; 50; 3.45; 3.45; 3.14; 3.76; 6.28; 6.28; 6.28; 200; 200];
% Define the length of the design vector
n = length(lb);
x = lb+ (ub - lb).* rand(1, n);
[A, b] = l_constraints(x);
% Define options for the genetic algorithm
options = optimoptions('ga', 'MaxGenerations', 5, 'PopulationSize', 10);
% Perform optimization
[X_opt, fval] = ga(@(x) objectiveFunction(x), 20, A, b, [], [], lb, ub, @(x) nonlcon(x), options);
function[d_values] = d_calc(x)
[~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, d0, dT] = x_values(x);
d_values = linspace(d0, dT, 21);
end
%Linear constraints
function [A, b] = l_constraints(x)
[d_values] = d_calc(x);
for i = 1:length(d_values)
d = d_values(i);
A1 = [1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
-1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
-1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 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0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0];
b2 = [0; 0; 0; l5+53.6; -l5+53.6; -l5-53.6; l5+53.6; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]; %l6 = 53.6;
b3 = [0; 0; 0; 0; 0; 0; 0; 0; -24; 24; 24; 24; 24; 0; 0; 0; 0; 0; 0; 0]; %l12 = 24;
b = b1+b2+b3;
end
end
function f = objectiveFunction(x)
H_MCP = [0.0017 0.0860 0.1646 0.2377 0.3061 0.3699 0.4297 0.4855 0.5380 0.5879 0.6359 0.6819 0.7257 0.7679 0.8088 0.8487 0.8878 0.9261 0.9634 1.0003 1.0861];
H_PIP = [0 0.1036 0.2022 0.2962 0.3860 0.4733 0.5594 0.6445 0.7288 0.8121 0.8943 0.9752 1.0545 1.1315 1.2062 1.2789 1.3497 1.4189 1.4867 1.5533 1.6190];
H_DIP = [0 0.0157 0.0384 0.0679 0.1043 0.1464 0.1938 0.2459 0.3016 0.3598 0.4191 0.4789 0.5383 0.5966 0.6536 0.7085 0.7608 0.8108 0.8579 0.9018 0.9445];
% Calculate the weights r_MCP, r_PIP, and r_DIP
r_MCP = max(H_MCP) - min(H_MCP);
r_PIP = max(H_PIP) - min(H_PIP);
r_DIP = max(H_DIP) - min(H_DIP);
% Calculate the denominator
denominator = r_MCP + r_PIP + r_DIP;
%Calculate E
[~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, E_MCP, E_PIP, E_DIP] = calc_EFG(x);
% Calculate the objective function
f = (r_MCP/denominator)*sum((H_MCP-E_MCP).^2)+(r_PIP/denominator)*sum((H_PIP-E_PIP).^2)+(r_DIP/denominator)*sum((H_DIP - E_DIP).^2);
end
% Define nonlinear constraints
function [c, ceq] = nonlcon(x)
[~, ~, ~, ~, l7, ~, l10, l11, ~, l14, ~, ~, ~, ~, ~, ~, ~, ~, ~] = x_values(x);
[E3, F3, G3, E6, F6, G6, E10, F10, G10, E13, F13, G13, E_MCP, E_PIP, E_DIP] = calc_EFG(x);
c1 = (E3^2 + F3^2 - G3^2);
c2 = (E6^2 + F6^2 - G6^2);
c3 = (E10^2 + F10^2 - G10^2);
c4 = (E13^2 + F13^2 - G13^2);
c5 = (abs(53.6 - l7) + 3) - (53.6 + l7 - 3);
c6 = (abs(l10 - l11) + 3) - (l10 + l11 - 3);
c7 = (abs(24 - l14) + 3) - (24 + l14 - 3);
c8 = [E_MCP - 0, 1.57 - E_MCP];
c9 = [E_PIP - 0, 1.74 - E_PIP];
c10 = [E_DIP - 0, 1.39 - E_DIP];
c = [c1; c2; c3; c4; c5; c6; c7(:); c8; c9; c10];
ceq=[];
end
function [l5, l9] = calc_l59(x)
[l1, l2, ~, ~, ~, l8, ~, ~, ~, ~, ~, alpha1, alpha2, alpha3, ~, ~, ~, ~, ~] = x_values(x);
l5 = sqrt((l2*cos(alpha2)-l1*cos(alpha1))^2 + (l2*sin(alpha2)-l1*sin(alpha1))^2);
l9 = sqrt((l2*cos(alpha2)-l8*cos(alpha3))^2 + (l2*sin(alpha2)-l8*sin(alpha3))^2);
end
function [E3, F3, G3, E6, F6, G6, E10, F10, G10, E13, F13, G13, E_MCP, E_PIP, E_DIP] = calc_EFG(~)
num_iterations = 50;
lb = [10; 10; 10; 10; 10; 10; 10; 10; 10; 10; 10; 3.14; 3.14; 1.57; 1.57; 0; 0; 0; 100; 100;];
ub = [200; 200; 40; 60; 40; 260; 50; 60; 50; 50; 50; 3.45; 3.45; 3.14; 3.76; 6.28; 6.28; 6.28; 280; 280];
while true
try
for i = 1:num_iterations
x_design = lb + (ub - lb) .* rand(size(lb)); % Generate random design variables within bounds
[l1, l2, l3, l4, l7, l8, l10, l11, l13, l14, l15, alpha1, alpha2, alpha3, ~, alpha5, alpha6, alpha7, d0, dT] = x_values(x_design);
[l5, l9] = calc_l59(x_design);
d_values = linspace(d0, dT, 21); % Compute d_values for each iteration
for j = 1:length(d_values)
d = d_values(j);
E3 = 2*l1*l3*cos(alpha1);
F3 = 2*l1*l3*sin(alpha1);
G3 = l1^2 + l3^2 - d^2;
if (E3^2 + F3^2 - G3^2) >= 0 && (E3 - G3) ~= 0
phi3 = 2*atan((F3 + sqrt(E3^2 + F3^2 - G3^2)) / (E3 - G3));
phi4 = phi3 + alpha3 + pi;
phi5 = atan((l2*sin(alpha2)-l1*sin(alpha1)) / (l2*cos(alpha2)-l1*cos(alpha1))); %phi1 = alpha1, phi2 = alphai2
else
error('Unable to find valid phi3, phi4, or phi5');
end
% Print values for debugging
fprintf('For d = %.2f:\n', d);
fprintf('For e3 = %.2f:\n', E3);
fprintf('For f3 = %.2f:\n', F3);
fprintf('For g3 = %.2f:\n', G3);
fprintf('For phi3 = %.2f:\n', phi3);
fprintf('For phi5 = %.2f:\n', phi5);
E6 = 2*56.13*(l4*cos(phi4) + l5*cos(phi5)); %l6=53.16
F6 = 2*56.13*(l4*sin(phi4) + l5*sin(phi5));
G6 = (l4*sin(phi4) + l5*sin(phi5))^2 + (l4*cos(phi4) + l5*cos(phi5))^2 + 56.13^2 - l7^2;
fprintf('For e6 = %.2f:\n', E6);
fprintf('For f6 = %.2f:\n', F6);
fprintf('For g6 = %.2f:\n', G6);
if ((E6^2+F6^2-G6^2) >=0 && E6-G6 ~=0)
phi6 = 2*atan(F6 - sqrt(E6^2 + F6^2 + G6^2) / (E6 - G6));
phi9 = atan((l2*sin(alpha2)-l8*sin(alpha3))^2 / (l2*cos(alpha2)-l8*cos(alpha3)));
fprintf('For phi6 = %.2f:\n', phi6);
fprintf('For phi9 = %.2f:\n', phi9);
fprintf('For thetamcp = %.2f:\n', E_MCP);
E10 = 2*l10*(56.13*cos(phi6) + l9*cos(phi9));
F10 = 2*l10*(56.13*sin(phi6) + l9*sin(phi9));
G10 = (56.13*sin(phi6) + l9*sin(phi9))^2 + (56.13*cos(phi6) + l9*cos(phi9))^2 + l10^2 - l11^2;
fprintf('For e10 = %.2f:\n', E10);
fprintf('For f10 = %.2f:\n', F10);
fprintf('For g10 = %.2f:\n', G10);
if ((E10^2+F10^2-G10^2)>=0 && E10-G10 ~=0)
phi10 = 2 * atan((F10 + sqrt(E10^2 + F10^2 - G10^2)) / (E10 - G10));
phi12 = phi10 + alpha6;
phi15 = phi6 + alpha5;
E13 = 2*l13*(24*cos(phi12) + l15*cos(phi15)); %l12 = 24
F13 = 2*l13*(24*sin(phi12) + l15*sin(phi15));
G13 = (24*sin(phi12(1)) + l15*sin(phi15(1)))^2 + (24*cos(phi12(1)) + l15*cos(phi15(1)))^2 + l13^2 - l14^2;
if (E13^2 + F13^2 - G13^2) >= 0 && (E13 - G13) ~= 0
phi13 = 2 * atan((F13 + sqrt(E13^2 + F13^2 - G13^2)) / (E13 - G13));
phi16 = phi13 + alpha7;
E_MCP = phi_6 - pi;
E_PIP = phi_12 - phi_6;
E_DIP = phi16 - phi12;
else
error('Unable to find valid E13, F13, or G13');
end
else
error('Unable to find valid E10, F10, or G10');
end
else
error('Unable to find valid E6, F6, or G6');
end
end
end
% If the code reaches this point without errors, break out of the while loop
break;
catch exception
% Display the error message
disp(exception.message);
% Restart the calculations from the beginning
continue;
end
end
end
function [l1, l2, l3, l4, l7, l8, l10, l11, l13, l14, l15, alpha1, alpha2, alpha3, alpha4, alpha5, alpha6, alpha7, d0, dT] = x_values(x)
l1 = x(1);
l2 = x(2);
l3 = x(3);
l4 = x(4);
l7 = x(5);
l8 = x(6);
l10 = x(7);
l11 = x(8);
l13 = x(9);
l14 = x(10);
l15 = x(11);
alpha1 = x(12);
alpha2 = x(13);
alpha3 = x(14);
alpha4 = x(15);
alpha5 = x(16);
alpha6 = x(17);
alpha7 = x(18);
d0 = x(19);
dT = x(20);
end