www.gusucode.com > symbolic 源码程序 matlab案例代码 > symbolic/SolveASystemOfDifferentialEquations1Example.m

    %% Solve a System of Differential Equations
%% Code1
% Define the system.
syms f(t) g(t)
eqn1 = diff(f) == 3*f + 4*g;
eqn2 = diff(g) == -4*f + 3*g;
S = dsolve(eqn1, eqn2)
%% 
% 

fSol(t) = S.f
gSol(t) = S.g
%% 
% 

[fSol(t), gSol(t)] = dsolve(eqn1, eqn2)
%% 
% 

c1 = f(0) == 0;
c2 = g(0) == 1;
[fSol(t), gSol(t)] = dsolve(eqn1, eqn2, c1, c2)
%% Plot1

fplot(fSol)
hold on
fplot(gSol)
grid on
legend('fSol','gSol','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')
%% 
%