MATLAB: Solve system of differential equations with vector input

differential equationsdsolvesolvesymsvector

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);

Best Answer

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.