%% Solve a System of Differential Equations
%% Code1
% Define the system.

syms u(t) v(t)
eqn1 = diff(u) == 3*u + 4*v;
eqn2 = diff(v) == -4*u + 3*v;
S = dsolve(eqn1, eqn2)

%

uSol(t) = S.u
vSol(t) = S.v

%

[uSol(t), vSol(t)] = dsolve(eqn1, eqn2)

%

c1 = u(0) == 0;
c2 = v(0) == 1;
[uSol(t), vSol(t)] = dsolve(eqn1, eqn2, c1, c2)

%
%% Plot1

fplot(uSol)
hold on
fplot(vSol)
grid on
legend('uSol','vSol','Location','best')

%
%% Code2

syms x(t) y(t)
A = [1 2; -1 1];
B = [1; t];
Y = [x; y];
eqn = diff(Y) == A*Y + B

%

[xSol(t), ySol(t)] = dsolve(eqn)

%

xSol(t) = simplify(xSol(t))
ySol(t) = simplify(ySol(t))

%

C = Y(0) == [2; -1];
[xSol(t), ySol(t)] = dsolve(eqn, C)

%
%% Plot2

clf
fplot(ySol)
hold on
fplot(xSol)
grid on
legend('ySol','xSol','Location','best')

%
% Copyright 2012 The MathWorks, Inc.