Concerning some error For Loop in my optimal control problem
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mojtaba Mohareri
el 16 de En. de 2020
Comentada: Mojtaba Mohareri
el 18 de En. de 2020
There's error ezplot(sol_a.x1,[0 1]) in my code below for solving a special optimal control problem
% State equations
syms x1 x2 p1 p2 u;
for i=0:0.1:1
Dx1 = -2*x2+u;
Dx2 = 2*x1;
% Cost function inside the integral
syms g;
g = u^2;
% Hamiltonian
syms p1 p2 H;
H = g + p1*Dx1 + p2*Dx2;
% Costate equations
Dp1 = -diff(H,x1);
Dp2 = -diff(H,x2);
% solve for control u
du = diff(H,u);
sol_u = solve(du, 'u');
% Substitute u to state equations
Dx1 = subs(Dx1, u, sol_u);
% convert symbolic objects to strings for using 'dsolve'
eq1 = strcat('Dx1=',char(Dx1));
eq2 = strcat('Dx2=',char(Dx2));
eq3 = strcat('Dp1=',char(Dp1));
eq4 = strcat('Dp2=',char(Dp2));
sol_h = dsolve(eq1,eq2,eq3,eq4);
%% use boundary conditions to determine the coefficients
conA1 = 'x1(0) = 3-i';
conA2 = 'x2(0) = 3-i';
conA3 = 'x1(1) = 0.5-(0.5)*i';
conA4 = 'x2(1) = 0.5-(0.5)*i';
sol_a = dsolve(eq1,eq2,eq3,eq4,conA1,conA2,conA3,conA4);
% plot solutions
figure(1);
ezplot(sol_a.x1,[0 1]);
axis([0 1 -4 3]);
hold on;
end
When I run the above code for every value i=0,0.1,...,1 separately, it works, but for "For Loop" it won't work.
0 comentarios
Respuesta aceptada
Walter Roberson
el 17 de En. de 2020
Inside a quoted string 'i' the symbolic engine will always interpret 'i' as sqrt(-1) and never as the current value of i
You should assume that you are no longer permitted to pass character vectors to dsolve and rewrite everything to symbolic expressions
You are doing some invalid calculations. Dx1 only makes sense if x1 is a function, but if it is a function then you cannot take the derivative of H with respect to it. Taking the derivative of a function with respect to a function is an invalid operation in normal calculus and requires Calculus of Variations instead. Unknown functions cannot be assumed to be independent, especially in a situation like this where the functions are being defined by their relationships to each other.
4 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Conversion Between Symbolic and Numeric en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!