objective function being miscalculated on this nonlineal optimization problem with loop structure on the objective function
조회 수: 2 (최근 30일)
이전 댓글 표시
So I'm trying to understand why my objective function seems to be misscalculated by any solver I use, here's the code:
% Optimization problem solution end effector four bar mechanism - mech eng
% Create optimization variables
r12 = optimvar("r1","LowerBound",0,"UpperBound",1000);
r22 = optimvar("r2","LowerBound",0,"UpperBound",1000);
Dx2 = optimvar("Dx","LowerBound",0,"UpperBound",2000);
r32 = optimvar("r3","LowerBound",0,"UpperBound",1000);
rp2 = optimvar("rp","LowerBound",0,"UpperBound",1000);
theta42 = optimvar("theta4","LowerBound",0,"UpperBound",6);
r4x2 = optimvar("r4x","LowerBound",0,"UpperBound",1000);
r4y2 = optimvar("r4y","LowerBound",-1000,"UpperBound",0);
theta1deinicio2 = optimvar("theta1deinicio","LowerBound",0,"UpperBound",...
6.28318530718);
Dy2 = optimvar("Dy","LowerBound",0,"UpperBound",263);
% Set initial starting point for the solver
initialPoint.r1 = repmat(200,size(r12));
initialPoint.r2 = repmat(928.2,size(r22));
initialPoint.Dx = repmat(337.62,size(Dx2));
initialPoint.r3 = repmat(615.65,size(r32));
initialPoint.rp = repmat(396.22,size(rp2));
initialPoint.theta4 = repmat(0.3926991,size(theta42));
initialPoint.r4x = repmat(722.52,size(r4x2));
initialPoint.r4y = repmat(-652.24,size(r4y2));
initialPoint.theta1deinicio = zeros(size(theta1deinicio2));
initialPoint.Dy = repmat(200,size(Dy2));
% Create problem
problem = optimproblem;
% Define problem objective
problem.Objective = fcn2optimexpr(@objectiveFcn,r12,r22,Dx2,r32,rp2,theta42,r4x2,...
r4y2,Dy2,theta1deinicio2);
% Define problem constraints
problem.Constraints = r12-sqrt(r4x2^2+r4y2^2)-r32+r22 <= 0;
% Set nondefault solver options
options = optimoptions("fmincon","PlotFcn","optimplotfvalconstr");
% Display problem information
show(problem);
% Solve problem
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint,...
"Solver","fmincon","Options",options);
% Display results
solution
reasonSolverStopped
objectiveValue
% Clear variables
clearvars r12 r22 Dx2 r32 rp2 theta42 r4x2 r4y2 theta1deinicio2 Dy2 initialPoint...
options reasonSolverStopped objectiveValue
% Create variables with the same name as the fields and assign their values
Dx = solution.Dx;
Dy = solution.Dy;
r1 = solution.r1;
r2 = solution.r2;
r3 = solution.r3;
r4x = solution.r4x;
r4y = solution.r4y;
rp = solution.rp;
theta1deinicio = solution.theta1deinicio;
theta4 = solution.theta4;
columna5_111=[22.1941 22.2252 22.2057 22.1055 21.8999 21.5734 21.1196 20.5384 19.8340 19.0129 18.0821 17.0483 15.9172 14.6941 13.3829 11.9873 10.5101 8.9532 7.3191 5.6105 3.8307 1.9837 0.0743 -1.8923 -3.9100 -5.9708 -8.0641 -10.1760 -12.2872 -14.3702 -16.3874 -18.2934 -20.0408 -21.5872 -22.8985 -23.9500 -24.7268 -25.2235 -25.4442 -25.4032 -25.1254 -24.6455 -24.0024 -23.2369 -22.3861 -21.4818 -20.5483 -19.6017 -18.6510 -17.7012 -16.7555 -15.8161 -14.8844 -13.9625 -13.0538 -12.1622 -11.2902 -10.4376 -9.6025 -8.7820 -7.9722 -7.1722 -6.3842 -5.6121 -4.8585 -4.1247 -3.4099 -2.7114 -2.0255 -1.3478 -0.6717 0.0110 0.7059 1.4147 2.1355 2.8651 3.6007 4.3392 5.0781 5.8178 6.5604 7.3093 8.0673 8.8370 9.6191 10.4118 11.2119 12.0162 12.8221 13.6283 14.4347 15.2417 16.0500 16.8600 17.6706 18.4758 19.2658 20.0331 20.7747 21.4928 22.1904];
columna5_222=[7.8413 8.3896 8.9730 9.5763 10.1823 10.7732 11.3302 11.8334 12.2626 12.5988 12.8252 12.9286 12.9012 12.7416 12.4542 12.0483 11.5363 10.9327 10.2537 9.5159 8.7363 7.9310 7.1159 6.3074 5.5225 4.7783 4.0927 3.4834 2.9669 2.5568 2.2612 2.0796 2.0014 2.0060 2.0661 2.1515 2.2329 2.2864 2.2963 2.2563 2.1694 2.0452 1.8963 1.7357 1.5738 1.4182 1.2737 1.1416 1.0213 0.9108 0.8080 0.7110 0.6181 0.5288 0.4431 0.3623 0.2874 0.2198 0.1606 0.1106 0.0702 0.0396 0.0182 0.0053 0 0.0015 0.0094 0.0237 0.0445 0.0720 0.1070 0.1503 0.2031 0.2663 0.3402 0.4248 0.5198 0.6245 0.7385 0.8620 0.9956 1.1405 1.2979 1.4692 1.6554 1.8571 2.0746 2.3085 2.5594 2.8287 3.1185 3.4320 3.7732 4.1470 4.5583 5.0099 5.5019 6.0333 6.6014 7.2028 7.8331];
columna5_11=-columna5_111*10;
columna5_22=columna5_222*10;
for i = 1:101
Ppy(i) = columna5_22(i) - Dy;
theta11gradosi = rad2deg(theta1deinicio);
theta1(i) = deg2rad(theta11gradosi) + ((2*pi)/101)*(i-1);
Ppx(i) = columna5_11(i) - Dx;
A = r1*cos(theta1(i))+r4x;
B = r1*sin(theta1(i))+r4y;
C = (r3^2 - A^2 - r2^2 - B^2)/(2*r2);
theta2 = 2*atan((B - sqrt(B^2 + A^2 - C^2))/(A + C));
rpp=rp;
thetap=theta2+theta4;
EEPx(i)=r1*cos(theta1(i))+rpp*cos(thetap);
EEPy(i)=r1*sin(theta1(i))+rpp*sin(thetap);
EEPx(102)=EEPx(1);
EEPy(102)=EEPy(1);
end
plot(EEPx,EEPy,Ppx,Ppy)
function objective = objectiveFcn(r1,r2,Dx,r3,rp,theta4,r4x,r4y,Dy,theta1deinicio)
columna5_111=[22.1941 22.2252 22.2057 22.1055 21.8999 21.5734 21.1196 20.5384 19.8340 19.0129 18.0821 17.0483 15.9172 14.6941 13.3829 11.9873 10.5101 8.9532 7.3191 5.6105 3.8307 1.9837 0.0743 -1.8923 -3.9100 -5.9708 -8.0641 -10.1760 -12.2872 -14.3702 -16.3874 -18.2934 -20.0408 -21.5872 -22.8985 -23.9500 -24.7268 -25.2235 -25.4442 -25.4032 -25.1254 -24.6455 -24.0024 -23.2369 -22.3861 -21.4818 -20.5483 -19.6017 -18.6510 -17.7012 -16.7555 -15.8161 -14.8844 -13.9625 -13.0538 -12.1622 -11.2902 -10.4376 -9.6025 -8.7820 -7.9722 -7.1722 -6.3842 -5.6121 -4.8585 -4.1247 -3.4099 -2.7114 -2.0255 -1.3478 -0.6717 0.0110 0.7059 1.4147 2.1355 2.8651 3.6007 4.3392 5.0781 5.8178 6.5604 7.3093 8.0673 8.8370 9.6191 10.4118 11.2119 12.0162 12.8221 13.6283 14.4347 15.2417 16.0500 16.8600 17.6706 18.4758 19.2658 20.0331 20.7747 21.4928 22.1904];
columna5_222=[7.8413 8.3896 8.9730 9.5763 10.1823 10.7732 11.3302 11.8334 12.2626 12.5988 12.8252 12.9286 12.9012 12.7416 12.4542 12.0483 11.5363 10.9327 10.2537 9.5159 8.7363 7.9310 7.1159 6.3074 5.5225 4.7783 4.0927 3.4834 2.9669 2.5568 2.2612 2.0796 2.0014 2.0060 2.0661 2.1515 2.2329 2.2864 2.2963 2.2563 2.1694 2.0452 1.8963 1.7357 1.5738 1.4182 1.2737 1.1416 1.0213 0.9108 0.8080 0.7110 0.6181 0.5288 0.4431 0.3623 0.2874 0.2198 0.1606 0.1106 0.0702 0.0396 0.0182 0.0053 0 0.0015 0.0094 0.0237 0.0445 0.0720 0.1070 0.1503 0.2031 0.2663 0.3402 0.4248 0.5198 0.6245 0.7385 0.8620 0.9956 1.1405 1.2979 1.4692 1.6554 1.8571 2.0746 2.3085 2.5594 2.8287 3.1185 3.4320 3.7732 4.1470 4.5583 5.0099 5.5019 6.0333 6.6014 7.2028 7.8331];
columna5_11=-columna5_111*10;
columna5_22=columna5_222*10;
j=1;
i=1;
J2 = 0;
for i = 1:101
Ppy(i) = columna5_22(i) - Dy;
theta11gradosi = theta1deinicio;
theta1(i) = deg2rad(theta11gradosi) + ((2*pi)/101)*(i-1);
Ppx(i) = columna5_11(i) - Dx;
A = r1*cos(theta1(i))+r4x;
B = r1*sin(theta1(i))+r4y;
C = (r3^2 - A^2 - r2^2 - B^2)/(2*r2);
theta2 = 2*atan((B - sqrt(B^2 + A^2 - C^2))/(A + C));
rpp=rp;
thetap=theta2+theta4;
EEPx(i)=r1*cos(theta1(i))+rpp*cos(thetap);
EEPy(i)=r1*sin(theta1(i))+rpp*sin(thetap);
EEPx(102)=EEPx(1);
EEPy(102)=EEPy(1);
end
min_dist = Inf;
accumulated_dist = zeros(101, 1); % Initialize an array to store the minimum distances
for i = 1:101
% Find the closest point on the second curve to the i-th point on the first curve
distances = sqrt((EEPx - Ppx(i)).^2 + (EEPy - Ppy(i)).^2);
[dist, j] = min(distances);
KA=0;
% Check if the distance is smaller than the current minimum
if dist < min_dist
% Calculate the orthogonal distance between the two curves
if i == 101
i1 = 1;
else
i1 = i+1;
end
if j == 101
j1 = 1;
else
j1 = j+1;
end
v1 = [EEPx(j) - Ppx(i), EEPy(j) - Ppy(i)];
v2 = [EEPx(j1) - Ppx(i1), EEPy(j1) - Ppy(i1)];
orth_dist = abs(det([v1; v2])) / norm(v2 - v1);
% Update the minimum distance and accumulated distance if necessary
min_dist = dist * sin(acos(dot(v1, v2) / (norm(v1) * norm(v2)))) + orth_dist;
accumulated_dist(i) = min_dist;
else
% If the new distance is not smaller than the current minimum, set the accumulated distance to the previous minimum
accumulated_dist(i) = min_dist;
end
end
objective = sum(accumulated_dist)
end
So it seems ok, when I run the code it gives me a solution for which apparently the objective function has a pretty "low" value:
I want to add that the problem has a solution, I got it from a paper, both curves (the orange and blue) do get really close, not perfect, but close, and just think, why does the objective value that is the result of the sum of the distance of all the 101 points could be so small.
I ran the objective function in another code with the solution values it gave me:
Not even close, not even true. so yeah, something is off.
댓글 수: 10
Torsten
2023년 2월 27일
Me too ... But there are two curves. Black in the image corresponds to red in MATLAB and red in the image corresponds to blue in MATLAB ? Are there also coordinates given in the image ?
José David Castillo Blanco
2023년 2월 27일
편집: José David Castillo Blanco
2023년 2월 27일
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!