Solve system of differential equations with vector input

6 vues (au cours des 30 derniers jours)
Xen
Xen le 14 Jan 2020
Commenté : Star Strider le 15 Jan 2020
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);

Réponse acceptée

Star Strider
Star Strider le 14 Jan 2020
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 commentaires
Xen
Xen le 15 Jan 2020
That was way too simple... Thanks!
Star Strider
Star Strider le 15 Jan 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by