How to numerically solve system of equations and differential equations simultaneously?
21 views (last 30 days)
Show older comments
Hi all,
I have 3 variables A, B, C, where A and B can be solved by system of equations. For example,
A + B = C + 6
A - B = 2*C
While C should be solved by differential equation,
diff(C,t) == 6*A
This is just a simple example, actually my equations are very complicated. I have tried:
- Using "solve". I think the equations are so complicated that the empty solutions appear. So I tend to find the numerical solution.
- Using "fsolve". The problem is that there is an undefined variable C. Because of the error "FSOLVE requires all values returned by functions to be of data type double.", I can't use symbolic.
- Using "vpasolve". There are similar problems with "fsolve". The error is "Symbolic parameters not supported in nonpolynomial equations.".
Are there other methods I should try? Thank you for any advice.
0 Comments
Accepted Answer
More Answers (2)
Torsten
on 19 Apr 2023
Edited: Torsten
on 19 Apr 2023
MATLAB's ode solvers allow a mixture of differential and algebraic equations as in your case.
These systems are called differential-algebraic equations. For an example, see
Solve Robertson Problem as Semi-Explicit Differential Algebraic Equations (DAEs)
under
Or as a solution for your simple example:
M = [0 0 0;0 0 0; 0 0 1];
tspan = [0 1];
fun = @(t,y) [y(1)+y(2)-y(3)-6;y(1)-y(2)-2*y(3);6*y(1)];
options = odeset('Mass',M,'MStateDependence','none','MassSingular','yes','RelTol',1e-7,'AbsTol',1e-7);
% Starting values for A and B are taken arbitrary ;
% They will be adjusted according to the algebraic equations 1 and 2 by
% ode15s to y0(1) = 4.5 and y0(2) = 2.5 (see below)
y0=[0 0 1];
[T,Y] = ode15s(fun,tspan,y0,options);
plot(T,Y)
Y(1,1)
Y(1,2)
Y(1,3)
grid on
Sam Chak
on 19 Apr 2023
Hi @I CHUN LIN
It seems that simple Substitution-and-Elimination method produces the solution

Thus, the linear ODE becomes

which indicates that it is possible to find an explicit solution of the differential equation analytically.
syms C(t)
eqn = diff(C, t) == 9*(C + 2);
S = dsolve(eqn)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!