Help solve a system of Differential Equations

1 回表示 (過去 30 日間)
Le Duc Long
Le Duc Long 2020 年 6 月 22 日
コメント済み: Le Duc Long 2020 年 6 月 22 日
Hi everybody,
I have code to solve a system of Differential Equations with matrix. I do not know my mistakes. When I run my, error in my code " Undefined operator '*' for input arguments of type 'function_handle'.Error in osc (line 22) odes=diff(P)==(0.2*A+s*B)*P;"
Can you see it and help me fix it. Thanks so much!
----------------------------------------------
This is my code:
T=100; g=0.5*T; n=3;s0=0.5;lamda=0.2;
% khai bao ham xa xe (KNTH)
s=@(t)(s0).*(((t-floor(t/T)*T)<g))+(0)*(((t-floor(t/T)*T)>g));
t=linspace(0,500);
plot(t,s(t));
grid;
% Khai bao mt A vuong cap n+2
syms p1(t) p2(t) p3(t) p4(t) p5(t)
A=[-1 0 0 0 0;
1 -1 0 0 0;
0 1 -1 0 0;
0 0 1 -1 0;
0 0 0 1 0];
B=[0 1 0 0 0;
0 -1 1 0 0;
0 0 -1 1 0;
0 0 0 -1 1;
0 0 0 0 -1];
P=[p1; p2; p3; p4; p5];
odes=diff(P)==(0.2*A+s*B)*P;
[p1Sol(t),p2Sol(t),p3Sol(t),p4Sol(t),p5Sol(t)]=dsolve(odes);
C=P(0)==[1;0;0;0;0];
[p1Sol(t),p2Sol(t),p3Sol(t),p4Sol(t),p5Sol(t)]=dsolve(odes,C);
clf
fplot(p1Sol)
hold on
fplot(p2Sol)
hold on
fplot(p3Sol)
hold on
fplot(p4Sol)
hold on
fplot(p5Sol)
grid on
legend('p1Sol','p2Sol','p3Sol','p4Sol','p5Sol','Location','best')
-----------------------------------
  2 件のコメント
John D'Errico
John D'Errico 2020 年 6 月 22 日
Since this is purely a numerical ODE solve, why are you using dsolve, instead of perhaps ODE45?
Le Duc Long
Le Duc Long 2020 年 6 月 22 日
Hi John D'Errico,
Thanks for your anwser. I tried to solve with fix value of function s(t) with dsolve and it was ok. So i think maybe use dsolve. I think that the problem with my code at "odes=diff(P)==(0.2*A+s*B)*P;". Because s=s(t). How your opion about this?
syms p1(t) p2(t) p3(t) p4(t) p5(t)
A=[-1 0 0 0 0;
1 -1 0 0 0;
0 1 -1 0 0;
0 0 1 -1 0;
0 0 0 1 0];
B=[0 1 0 0 0;
0 -1 1 0 0;
0 0 -1 1 0;
0 0 0 -1 1;
0 0 0 0 -1];
P=[p1; p2; p3; p4; p5];
odes=diff(P)==(0.5*A+B)*P;
[p1Sol(t),p2Sol(t),p3Sol(t),p4Sol(t),p5Sol(t)]=dsolve(odes);
C=P(0)==[1;0;0;0;0];
[p1Sol(t),p2Sol(t),p3Sol(t),p4Sol(t),p5Sol(t)]=dsolve(odes,C);
clf
fplot(p1Sol)
hold on
fplot(p2Sol)
hold on
fplot(p3Sol)
hold on
fplot(p4Sol)
hold on
fplot(p5Sol)
grid on
legend('p1Sol','p2Sol','p3Sol','p4Sol','p5Sol','Location','best')

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 22 日
編集済み: Walter Roberson 2020 年 6 月 22 日
syms t
s = piecewise((t-floor(t/T)*T)<g,s0,0);
fplot(s, [0 500])
%do not do the numeric assignment to t
Note that you do did explicitly define s for the case where the expression exactly equals g, but the numeric logic you used would have come out as 0 for that case anyhow.
  1 件のコメント
Le Duc Long
Le Duc Long 2020 年 6 月 22 日
Thanks Walter Roberson,
Can you explain for me this error meaning?
% Tham so dau vao
T=100; g=0.5*T; n=3;s0=0.5;lamda=0.2;
% khai bao ham xa xe (KNTH)
syms t
s = piecewise((t-floor(t/T)*T)<g,s0,0);
fplot(s,[0 500]);
% Khai bao mt A vuong cap n+2
syms p1(t) p2(t) p3(t) p4(t) p5(t)
A=[-1 0 0 0 0;
1 -1 0 0 0;
0 1 -1 0 0;
0 0 1 -1 0;
0 0 0 1 0];
B=[0 1 0 0 0;
0 -1 1 0 0;
0 0 -1 1 0;
0 0 0 -1 1;
0 0 0 0 -1];
P=[p1; p2; p3; p4; p5];
odes=diff(P)==(0.2*A+s*B)*P;
[p1Sol(t),p2Sol(t),p3Sol(t),p4Sol(t),p5Sol(t)]=dsolve(odes);
C=P(0)==[1;0;0;0;0];
[p1Sol(t),p2Sol(t),p3Sol(t),p4Sol(t),p5Sol(t)]=dsolve(odes,C);
clf
fplot(p1Sol)
hold on
fplot(p2Sol)
hold on
fplot(p3Sol)
hold on
fplot(p4Sol)
hold on
fplot(p5Sol)
grid on
legend('p1Sol','p2Sol','p3Sol','p4Sol','p5Sol','Location','best')

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by