Solve system of differential equations with vector input

4 ビュー (過去 30 日間)
Xen
Xen 2020 年 1 月 14 日
コメント済み: Star Strider 2020 年 1 月 15 日
Assume that I want to solve the system of equations below for u and v. A and B are known numbers (say, 1 and 2 respectively) and a(t) is a fixed vector that I want to pass to the solution and get the result. How can I do it? The code works fine when a(t) is not used.
syms A B u(t) v(t) a(t)
ode1 = diff(u,t) == A * (a - u);
ode2 = diff(v,t) == B * (u - v);
odes = [ode1; ode2];
[uSol(t), vSol(t)] = dsolve(odes);
% Above work fine; below don't
uF = matlabFunction(uSol);
vF = matlabFunction(vSol);
resultu = uF(0:10, 1, 2); % t, A, B
resultv = vF(0:10, 1, 2);

採用された回答

Star Strider
Star Strider 2020 年 1 月 14 日
In the derivation, let ‘a’ simply be an undefined constant, and define the initial conditions as 0 (unless you want them to be something else). Then in the solution, provide ‘a’ as a row vector to get the solution:
syms A B u(t) v(t) a
ode1 = diff(u,t) == A * (a - u);
ode2 = diff(v,t) == B * (u - v);
odes = [ode1; ode2];
[uSol(t), vSol(t)] = dsolve(odes, u(0)==0, v(0)==0)
% Above work fine; below don't
uF = matlabFunction(uSol);
vF = matlabFunction(vSol);
av = randn(1, 11);
resultu = uF(0:10, 1, 2, av); % t, A, B
resultv = vF(0:10, 1, 2, av);
This evaluates them as functions of whatever you want to define ‘a’ to be.
  2 件のコメント
Xen
Xen 2020 年 1 月 15 日
That was way too simple... Thanks!
Star Strider
Star Strider 2020 年 1 月 15 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEquation Solving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by