fmincon using fake values of the objective fuction

조회 수: 4 (최근 30일)
José David Castillo Blanco
José David Castillo Blanco 2023년 2월 26일
댓글: Walter Roberson 2023년 3월 12일
So, I've been trying to solve a nonlinear optimization problem using the Optimization Toolbox, trying to explain what I'm doing I have two curves that are the result of two sets of points, each given by two sets of vectors that give both x and y coordinates, one is a the trajectory I need, and the other is the trajectory of the end effector of a four bar mechanism, given by a set of points that is the same number of points of the trajectory objective, so, my issue here is that apparently everything is right, there's even a graph that tells me the equations I wrote don't have mistakes (at least that's what I think), but for some reason running fmincon it's like it's using another objective function for which the value gets really small, that's not possible for the objective function I wrote, In a sepparate code I evaluated my objective functions with the parameters that fmincon gave me and it gave me a reasonable number that does makes sense, I think that for some reason it's not getting my objective function right, mind that in the middle of the code there's a part which is used to graph both trajectories/curves.
%Solucion problema de optimización
% 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);
x = 9.5063e+04
% 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);
OptimizationProblem : Solve for: Dx, Dy, r1, r2, r3, r4x, r4y, rp, theta1deinicio, theta4 minimize : objectiveFcn(r1, r2, Dx, r3, rp, theta4, r4x, r4y, Dy, theta1deinicio) subject to : (((r1 - sqrt((r4x.^2 + r4y.^2))) - r3) + r2) <= 0 variable bounds: 0 <= Dx <= 2000 0 <= Dy <= 263 0 <= r1 <= 1000 0 <= r2 <= 1000 0 <= r3 <= 1000 0 <= r4x <= 1000 -1000 <= r4y <= 0 0 <= rp <= 1000 0 <= theta1deinicio <= 6.2832 0 <= theta4 <= 6
% Solve problem
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint,...
"Solver","fmincon","Options",options);
Solving problem using fmincon.
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 2.4085e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.4083e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 1.1007e+03
x = 6.2053e+03
x = 2.2735e+03
x = 933.4434
x = 933.4438
x = 933.4437
x = 933.4434
x = 933.4433
x = 933.4432
x = 933.4434
x = 933.4436
x = 933.4427
x = 933.4434
x = 933.4430
x = 460.5450
x = 460.5454
x = 460.5446
x = 460.5450
x = 460.5446
x = 460.5459
x = 460.5453
x = 460.5442
x = 460.5449
x = 460.5450
x = 460.5462
x = 2.5411e+03
x = 1.7859e+03
x = 1.4562e+03
x = 86.1400
x = 86.1410
x = 86.1400
x = 86.1395
x = 86.1399
x = 86.1402
x = 86.1401
x = 86.1398
x = 86.1388
x = 86.1400
x = 86.1402
x = 1.2949e+03
x = 98.5592
x = 88.7104
x = 128.5929
x = 1.2440e+03
x = 189.6439
x = 38.6887
x = 38.6877
x = 38.6888
x = 38.6893
x = 38.6891
x = 38.6882
x = 38.6884
x = 38.6891
x = 38.6898
x = 38.6887
x = 38.6881
x = 97.8826
x = 27.8167
x = 27.8157
x = 27.8169
x = 27.8173
x = 27.8171
x = 27.8162
x = 27.8164
x = 27.8171
x = 27.8178
x = 27.8167
x = 27.8161
x = 112.1928
x = 102.4601
x = 47.6635
x = 23.2101
x = 23.2110
x = 23.2099
x = 23.2095
x = 23.2097
x = 23.2105
x = 23.2103
x = 23.2097
x = 23.2090
x = 23.2101
x = 23.2106
x = 14.3017
x = 14.3026
x = 14.3016
x = 14.3012
x = 14.3014
x = 14.3022
x = 14.3020
x = 14.3014
x = 14.3008
x = 14.3017
x = 14.3023
x = 57.2988
x = 37.2194
x = 25.9132
x = 19.9546
x = 43.7016
x = 28.6775
x = 21.2193
x = 17.5035
x = 15.6488
x = 14.7223
x = 14.2593
x = 14.2583
x = 14.2594
x = 14.2598
x = 14.2596
x = 14.2588
x = 14.2590
x = 14.2596
x = 14.2603
x = 14.2593
x = 14.2587
x = 13.9585
x = 13.9594
x = 13.9583
x = 13.9580
x = 13.9581
x = 13.9589
x = 13.9587
x = 13.9581
x = 13.9575
x = 13.9585
x = 13.9590
x = 13.9297
x = 13.9287
x = 13.9298
x = 13.9302
x = 13.9300
x = 13.9292
x = 13.9294
x = 13.9301
x = 13.9308
x = 13.9297
x = 13.9291
x = 13.3352
x = 13.3344
x = 13.3353
x = 13.3357
x = 13.3355
x = 13.3347
x = 13.3349
x = 13.3356
x = 13.3363
x = 13.3352
x = 13.3346
x = 13.5802
x = 13.5811
x = 13.5801
x = 13.5797
x = 13.5799
x = 13.5806
x = 13.5805
x = 13.5798
x = 13.5792
x = 13.5802
x = 13.5807
x = 13.5401
x = 13.5391
x = 13.5402
x = 13.5407
x = 13.5404
x = 13.5396
x = 13.5398
x = 13.5405
x = 13.5412
x = 13.5401
x = 13.5395
x = 12.8386
x = 12.8376
x = 12.8388
x = 12.8392
x = 12.8390
x = 12.8382
x = 12.8383
x = 12.8390
x = 12.8397
x = 12.8386
x = 12.8380
x = 8.7165
x = 8.7175
x = 8.7165
x = 8.7160
x = 8.7164
x = 8.7167
x = 8.7167
x = 8.7164
x = 8.7154
x = 8.7165
x = 8.7168
x = 13.7662
x = 58.5852
x = 34.1225
x = 11.9904
x = 21.1166
x = 50.9357
x = 10.7610
x = 3.1328
x = 3.1318
x = 3.1329
x = 3.1334
x = 3.1330
x = 3.1326
x = 3.1327
x = 3.1330
x = 3.1340
x = 3.1328
x = 3.1326
x = 5.0980
x = 2.8046
x = 2.8036
x = 2.8046
x = 2.8051
x = 2.8047
x = 2.8044
x = 2.8044
x = 2.8047
x = 2.8057
x = 2.8046
x = 2.8043
x = 23.4245
x = 12.2120
x = 6.9634
x = 4.3471
x = 22.5979
x = 12.0854
x = 6.8947
x = 4.3154
x = 3.0297
x = 2.3879
x = 2.3888
x = 2.3878
x = 2.3873
x = 2.3877
x = 2.3881
x = 2.3880
x = 2.3877
x = 2.3867
x = 2.3879
x = 2.3881
x = 2.4437
x = 2.3670
x = 2.3680
x = 2.3670
x = 2.3665
x = 2.3669
x = 2.3672
x = 2.3672
x = 2.3669
x = 2.3659
x = 2.3670
x = 2.3673
x = 3.7441
x = 2.9387
x = 2.5368
x = 2.3360
x = 2.3350
x = 2.3361
x = 2.3366
x = 2.3362
x = 2.3358
x = 2.3359
x = 2.3362
x = 2.3372
x = 2.3360
x = 2.3358
x = 2.2618
x = 2.2628
x = 2.2617
x = 2.2612
x = 2.2616
x = 2.2620
x = 2.2619
x = 2.2616
x = 2.2607
x = 2.2618
x = 2.2620
x = 2.3802
x = 2.2733
x = 2.2723
x = 2.2734
x = 2.2739
x = 2.2735
x = 2.2731
x = 2.2732
x = 2.2735
x = 2.2745
x = 2.2733
x = 2.2731
x = 2.2435
x = 2.2424
x = 2.2435
x = 2.2440
x = 2.2436
x = 2.2433
x = 2.2433
x = 2.2436
x = 2.2446
x = 2.2435
x = 2.2432
x = 2.3488
x = 2.2882
x = 2.2557
x = 2.2390
x = 2.2400
x = 2.2389
x = 2.2384
x = 2.2388
x = 2.2392
x = 2.2391
x = 2.2388
x = 2.2378
x = 2.2390
x = 2.2392
x = 2.2291
x = 2.2280
x = 2.2291
x = 2.2296
x = 2.2292
x = 2.2289
x = 2.2289
x = 2.2292
x = 2.2303
x = 2.2291
x = 2.2288
x = 2.2175
x = 2.2164
x = 2.2175
x = 2.2180
x = 2.2176
x = 2.2173
x = 2.2173
x = 2.2176
x = 2.2187
x = 2.2175
x = 2.2172
x = 2.2113
x = 2.2123
x = 2.2113
x = 2.2108
x = 2.2111
x = 2.2115
x = 2.2115
x = 2.2112
x = 2.2102
x = 2.2113
x = 2.2116
x = 74.1826
x = 75.3897
x = 2.1891
x = 2.1901
x = 2.1891
x = 2.1886
x = 2.1889
x = 2.1893
x = 2.1893
x = 2.1890
x = 2.1880
x = 2.1891
x = 2.1894
x = 120.9777
x = 68.6317
x = 72.2179
x = 74.0854
x = 2.1803
x = 2.1813
x = 2.1803
x = 2.1798
x = 2.1802
x = 2.1805
x = 2.1805
x = 2.1802
x = 2.1792
x = 2.1803
x = 2.1806
x = 73.5252
x = 74.8790
x = 2.1604
x = 2.1593
x = 2.1604
x = 2.1609
x = 2.1605
x = 2.1601
x = 2.1602
x = 2.1605
x = 2.1615
x = 2.1604
x = 2.1601
x = 73.3246
x = 74.3837
x = 74.9140
x = 75.1794
x = 2.1567
x = 2.1577
x = 2.1566
x = 2.1561
x = 2.1565
x = 2.1569
x = 2.1568
x = 2.1565
x = 2.1555
x = 2.1567
x = 2.1570
x = 73.1073
x = 74.3678
x = 74.9005
x = 75.1670
x = 2.1552
x = 2.1544
x = 2.1552
x = 2.1558
x = 2.1554
x = 2.1550
x = 2.1550
x = 2.1554
x = 2.1564
x = 2.1552
x = 2.1549
x = 103.1146
x = 74.5344
x = 74.9826
x = 75.2084
x = 2.1550
x = 2.1561
x = 2.1550
x = 2.1545
x = 2.1549
x = 2.1552
x = 2.1552
x = 2.1549
x = 2.1547
x = 2.1550
x = 2.1553
x = 597.2594
x = 10.1469
x = 6.1650
x = 4.1636
x = 12.4734
x = 7.3360
x = 4.7524
x = 3.4568
x = 2.8080
x = 2.4834
x = 2.3210
x = 2.2398
x = 2.1991
x = 2.1786
x = 2.1681
x = 2.1614
x = 2.1578
x = 2.1560
x = 2.1551
x = 2.1547
x = 2.1557
x = 2.1546
x = 2.1544
x = 2.1545
x = 2.1549
x = 2.1548
x = 2.1545
x = 2.1551
x = 2.1547
x = 2.1549
x = 307.3561
x = 2.1548
x = 2.1547
x = 2.1547
x = 2.1550
x = 2.1547
x = 2.1548
x = 2.1547
x = 2.1552
x = 2.1548
x = 2.1544
x = 2.1545
x = 2.1548
x = 2.1558
x = 2.1547
x = 2.1544
x = 276.8682
x = 2.1545
x = 2.1550
x = 2.1545
x = 2.1551
x = 2.1547
x = 2.1543
x = 2.1543
x = 2.1547
x = 2.1557
x = 2.1545
x = 2.1542
x = 50.4338
x = 41.9026
x = 58.2192
x = 13.4405
x = 2.1547
x = 2.1547
x = 2.1548
x = 2.1548
x = 2.1548
x = 2.1548
x = 2.1548
x = 2.1544
x = 2.1555
x = 2.1544
x = 2.1546
x = 2.1543
x = 2.1547
x = 2.1546
x = 2.1543
x = 2.1552
x = 2.1544
x = 2.1547
x = 119.7865
x = 2.1545
x = 2.1545
x = 2.1544
x = 2.1543
x = 2.1552
x = 2.1543
x = 2.1548
x = 2.1544
x = 2.1544
x = 2.1544
x = 2.1544
x = 2.1554
x = 2.1543
x = 2.1545
x = 32.0311
x = 2.1543
x = 2.1543
x = 2.1543
x = 2.1553
x = 2.1551
x = 2.1550
x = 2.1547
x = 2.1545
x = 2.1544
x = 2.1543
x = 2.1543
x = 2.1543
x = 2.1543
x = 2.1543
x = 2.1543
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
% Display results
solution
solution = struct with fields:
Dx: 342.4900 Dy: 118.1782 r1: 173.7786 r2: 950.8467 r3: 638.5143 r4x: 708.5907 r4y: -622.1340 rp: 392.7791 theta1deinicio: 5.6040 theta4: 0.2238
reasonSolverStopped
reasonSolverStopped =
StepSizeBelowTolerance
objectiveValue
objectiveValue = 2.1543
% 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);
k=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
x=sum(accumulated_dist)
objective = x;
end
This code it's my attempt to basically do a mechanism synthesis, the end effector trajectory of the mechanism is supposed to be as close as posible to a trajectory reference, the trajectory reference is given by 101 points, so, for this process, I'm going to use 101 points of the end effector, basically the part where I find
EEPx(i)=r1*cos(theta1(i))+rpp*cos(thetap);
EEPy(i)=r1*sin(theta1(i))+rpp*sin(thetap);
It's used to evaluate the position of the end effector given each angle of theta, obviously, they are 101 angles.
The other part basically is to look about the minimum orthogonal distance between a point in the reference trajectory and my other set of points, that again, both are 101 in total, from the mechanism end effector "EEPx and "EEPy". And the objective "x" it's the result of all the distances between points, the sum of all of them.
So, trying to elaborate further since this is a four bar closed loop chain (The mechanism i'm synthesizing), there's an angle that it's supposed to rotate 360 degrees, which btw, as you can notice the program has to tell which angle is the best to begin.
So, this thing is giving me some weird "x" which is the sum of the distance between points, I don't know if it doesn't let the code do the sum for every 101 angles and points, or what, but the deal is that if I ask then for the value of "x" (Which again as I said is the sum of the distance between points" it says:
The objectiveValue for some reason is lower than the actual objective value, doesn't make any sense to me, is it no allowing the code to do all the 101 sums it need or what?
  댓글 수: 4
José David Castillo Blanco
José David Castillo Blanco 2023년 2월 26일
Hi Mr Roberson and Mr Matt J I already updated my post hope it's clear enough.
Walter Roberson
Walter Roberson 2023년 3월 12일
Why are you saying that the value cannot get that small? If you take the outputs from optimization and pass them into your objective function, then the value returned is the same as the value indicated by the optimization, and the inequality constraint checks out and all of the variables are between the bounds you established. This is happening when passing values directly into your function, not just when using the optimization expression version of it
%Solucion problema de optimización
% 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);
OptimizationProblem : Solve for: Dx, Dy, r1, r2, r3, r4x, r4y, rp, theta1deinicio, theta4 minimize : objectiveFcn(r1, r2, Dx, r3, rp, theta4, r4x, r4y, Dy, theta1deinicio) subject to : (((r1 - sqrt((r4x.^2 + r4y.^2))) - r3) + r2) <= 0 variable bounds: 0 <= Dx <= 2000 0 <= Dy <= 263 0 <= r1 <= 1000 0 <= r2 <= 1000 0 <= r3 <= 1000 0 <= r4x <= 1000 -1000 <= r4y <= 0 0 <= rp <= 1000 0 <= theta1deinicio <= 6.2832 0 <= theta4 <= 6
% Solve problem
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint,...
"Solver","fmincon","Options",options);
Solving problem using fmincon. Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
% Display results
solution
solution = struct with fields:
Dx: 342.4900 Dy: 118.1782 r1: 173.7786 r2: 950.8467 r3: 638.5143 r4x: 708.5907 r4y: -622.1340 rp: 392.7791 theta1deinicio: 5.6040 theta4: 0.2238
reasonSolverStopped
reasonSolverStopped =
StepSizeBelowTolerance
objectiveValue
objectiveValue = 2.1543
s = solution;
objectiveFcn(s.r1,s.r2,s.Dx,s.r3,s.rp,s.theta4,s.r4x,s.r4y,s.Dy,s.theta1deinicio)
ans = 2.1543
s.r1-sqrt(s.r4x^2+s.r4y^2)-s.r3+s.r2
ans = -456.8373
% 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);
k=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
x=sum(accumulated_dist);
objective = x;
end

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by