How does matlab solve a pair of coupled differential equations?
5 views (last 30 days)
Show older comments
William Royle
on 15 Aug 2017
Commented: William Royle
on 15 Aug 2017
I'm trying to solve a pair of coupled differential equations following the Matlab article in:
https://uk.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html
When trying to use this example to solve my own equations the code runs but the diff function gives a different function to what I was expecting. I was expecting to get an equation of the form N(t) = N_{0}exp(-t/T). Instead I get N(t) = A - A*exp(-t/T), where A is constant defined by the boundary conditions. This is giving me the wrong time dependence of N(t).
Why am I getting the incorrect solution? Is there something wrong with my code or is there a problem with one of the Matlab functions?
Thanks
Simplified code below.
% Define lifetime variables
T_th = 10e-11;
% set up problems, represent u and v with symbolic functions
syms N_ex(t) N_cb(t)
% set up the differential equations.
A = [ -1/T_th 0 ;
1/T_th 0 ];
B = [N_ex ; N_cb];
ordinary_diff_eqns = diff(B) == A*B
% apply the initial conditions
Initial_conds = B(0) == [1000 ; 0]
% solve the differential equations
[N_exSol(t), N_cbSol(t)] = dsolve(ordinary_diff_eqns, Initial_conds)
%Plot the solutions to see the time dependence.
clf
hold on
fplot(N_exSol)
xlim([0,10e-9])
ylim([0,1000])
xlabel('Time')
ylabel('Population')
fplot(N_cbSol)
0 Comments
Accepted Answer
Steven Lord
on 15 Aug 2017
"When solving for multiple functions, dsolve returns a structure by default. Alternatively, you can directly assign solutions to functions or variables by specifying the outputs explicitly as a vector. dsolve sorts outputs in alphabetical order using symvar."
dsolve returns the correct solutions, just not in the order you expect. Call dsolve with one output and extract the appropriate fields of the struct array to view/use the solutions.
More Answers (1)
Torsten
on 15 Aug 2017
Correct solution is
N_ex(t) = 1000*exp(-t/T_th)
N_cb(t) = 1000*(1-exp(-t/T_th))
Best wishes
Torsten.
See Also
Categories
Find more on Matrix Computations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!