Concerning some error For Loop in my optimal control problem

1 回表示 (過去 30 日間)
Mojtaba Mohareri
Mojtaba Mohareri 2020 年 1 月 16 日
コメント済み: Mojtaba Mohareri 2020 年 1 月 18 日
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.

採用された回答

Walter Roberson
Walter Roberson 2020 年 1 月 17 日
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 件のコメント
Mojtaba Mohareri
Mojtaba Mohareri 2020 年 1 月 18 日
It works properly. I appreciate you. Thank you so much for your help.
Mojtaba Mohareri
Mojtaba Mohareri 2020 年 1 月 18 日
I used twise "For Loop" with different conditions in my main problem. I want to use different colors for each group of solutions obtained by each loop (red color for loop 1 and blue color for loop 2). For this, I've written ez1=ezplot(sol_a.x1,[0 1]);
set(ez1,'color',[1 0 0])
at the end of loop1 and
ez2=ezplot(sol_a.x1,[0 1]);
set(ez2,'color',[0 0 1])
at the end of loop 2.
Finaly I've used legend('Lower Bounds','Upper Bounds') but my problem is that these two exprssions are red.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by