error in matlab code

4 vues (au cours des 30 derniers jours)
Devapriya
Devapriya le 18 Déc 2024
Commenté : Walter Roberson le 13 Jan 2025
I have been trying to solve an optimal problem in which a body of mass m have to [be] transfered to y(tf) = 0 from its initial position y(o) = 0 such that its final velocity is zero satisfying the performance index.
, where t = [to, tf].
But I am not getting the optimal trajectories using this code. Could anyone help me to find where the mistake [is]?
clear all
c0 = [0.1; 0.1; 0.1; 0.1; 5]; % Make a starting guess at the solution
[c, fval] = fsolve (@myfun1, c0) % Call optimizer
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 5.000000e+02.
c = 5×1
0.0046 -0.0198 -0.0881 0.7725 12.1323
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = 5×1
-0.2275 0.0271 -0.0881 -0.0968 0.0024
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c1 = c(1); c2 = c(2); c3 = c(3); c4 = c(4); c5 = c(5);
t=0:0.01:c5;
x1op = -(c1*(t.^3))/6-(c2*(t.^2))/2+(c3.*t)+c4
x1op = 1×1214
0.7725 0.7716 0.7707 0.7698 0.7690 0.7681 0.7672 0.7663 0.7655 0.7646 0.7638 0.7629 0.7620 0.7612 0.7603 0.7595 0.7586 0.7578 0.7569 0.7561 0.7552 0.7544 0.7536 0.7527 0.7519 0.7511 0.7502 0.7494 0.7486 0.7477
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x2op =-(c1*(t.^2))/2+-(c2.*t)+c3
x2op = 1×1214
-0.0881 -0.0879 -0.0877 -0.0875 -0.0873 -0.0871 -0.0869 -0.0867 -0.0865 -0.0863 -0.0861 -0.0859 -0.0857 -0.0855 -0.0853 -0.0851 -0.0850 -0.0848 -0.0846 -0.0844 -0.0842 -0.0840 -0.0838 -0.0836 -0.0834 -0.0833 -0.0831 -0.0829 -0.0827 -0.0825
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
uop = -(c1.*t)-c2
uop = 1×1214
0.0198 0.0198 0.0198 0.0197 0.0197 0.0196 0.0196 0.0195 0.0195 0.0194 0.0194 0.0193 0.0193 0.0193 0.0192 0.0192 0.0191 0.0191 0.0190 0.0190 0.0189 0.0189 0.0188 0.0188 0.0187 0.0187 0.0187 0.0186 0.0186 0.0185
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lam1= c1;
H = 1.5 * uop.^2 + x2op.* lam1;
figure (1)
plot(t, uop)
figure(2)
plot(t,x1op,t,x2op)
figure(3)
plot(t,H)
function F = myfun1(c)
x10 = 1;
x20 = 0;
x1f = 0;
x2f = 0;
u=-(c(1)*c(5))-c(2);
ud = c(1);
% the conditions x1(t = 0), x1(t = tf), x2(t = 0), x2(t = tf)
f1 = c(4)-x10;
f2 =-(c(1)*(c(5)^3))/6 -(c(2)*(c(5)^2))/2 +(c(3)*c(5)) + 1 -x1f;
f3 = c(3)-x20;
f4 = -(c(1)*(c(5)^2))/2 -(c(2)*c(5))-x2f;
f5 = 1.5 *u.^2 -f4.*ud ;% the condition H(tf) = 0
F = [f1;f2;f3;f4;f5]; % the five unknown system F(C) = 0
end
  1 commentaire
Walter Roberson
Walter Roberson le 13 Jan 2025
clear all
c0 = [0.1; 0.1; 0.1; 0.1; 5]; % Make a starting guess at the solution
opts = optimoptions('fsolve', 'MaxIterations', 1e6, 'MaxFunEval', 1e7);
[c, fval] = fsolve (@myfun1, c0, opts) % Call optimizer
Equation solved, inaccuracy possible. The vector of function values is near zero, as measured by the value of the function tolerance. However, the last step was ineffective.
c = 5×1
0.0000 -0.0001 -0.0075 0.9828 210.0654
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = 5×1
-0.0172 0.0000 -0.0075 -0.0026 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c1 = c(1); c2 = c(2); c3 = c(3); c4 = c(4); c5 = c(5);
t=0:0.01:c5;
x1op = -(c1*(t.^3))/6-(c2*(t.^2))/2+(c3.*t)+c4
x1op = 1×21007
0.9828 0.9827 0.9826 0.9826 0.9825 0.9824 0.9823 0.9823 0.9822 0.9821 0.9820 0.9820 0.9819 0.9818 0.9817 0.9817 0.9816 0.9815 0.9814 0.9814 0.9813 0.9812 0.9811 0.9811 0.9810 0.9809 0.9808 0.9808 0.9807 0.9806
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x2op =-(c1*(t.^2))/2+-(c2.*t)+c3
x2op = 1×21007
-0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075 -0.0075
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
uop = -(c1.*t)-c2
uop = 1×21007
1.0e-03 * 0.1026 0.1026 0.1026 0.1026 0.1026 0.1026 0.1025 0.1025 0.1025 0.1025 0.1025 0.1025 0.1025 0.1025 0.1025 0.1024 0.1024 0.1024 0.1024 0.1024 0.1024 0.1024 0.1024 0.1024 0.1023 0.1023 0.1023 0.1023 0.1023 0.1023
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lam1= c1;
H = 1.5 * uop.^2 + x2op.* lam1;
figure (1)
plot(t, uop)
figure(2)
plot(t,x1op,t,x2op)
figure(3)
plot(t,H)
function F = myfun1(c)
x10 = 1;
x20 = 0;
x1f = 0;
x2f = 0;
u=-(c(1)*c(5))-c(2);
ud = c(1);
% the conditions x1(t = 0), x1(t = tf), x2(t = 0), x2(t = tf)
f1 = c(4)-x10;
f2 =-(c(1)*(c(5)^3))/6 -(c(2)*(c(5)^2))/2 +(c(3)*c(5)) + 1 -x1f;
f3 = c(3)-x20;
f4 = -(c(1)*(c(5)^2))/2 -(c(2)*c(5))-x2f;
f5 = 1.5 *u.^2 -f4.*ud ;% the condition H(tf) = 0
F = [f1;f2;f3;f4;f5]; % the five unknown system F(C) = 0
end

Connectez-vous pour commenter.

Réponses (1)

Divyanshu
Divyanshu le 13 Jan 2025
Hello @Devapriya,
I think the reason for the incorrect trajectories can be the error 'Solver stopped prematurely'. This MATLAB answer thread addresses a similar issue, just try to follow this and re-run your script.
Hopefully, after modifying the 'MaxIterations' and 'MaxFunctionEvaluations' parameters you would get correct results from the script.

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by