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 CenterFile Exchange에서 Equation Solving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by