How to numerically solve system of equations and differential equations simultaneously?

조회 수: 27 (최근 30일)
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:
  1. Using "solve". I think the equations are so complicated that the empty solutions appear. So I tend to find the numerical solution.
  2. 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.
  3. 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.

채택된 답변

Askic V
Askic V 2023년 4월 19일
편집: Askic V 님. 2023년 4월 19일
Does this make sense to you (in your simple example)?
syms A B C(t)
eqn1 = diff(C,t) == 6*A;
C_sol = dsolve(eqn1)
C_sol = 
eqn2 = A + B == C_sol + 6;
eqn3 = A - B == 2*C_sol;
[A,B] = equationsToMatrix([eqn2, eqn3], [A, B]);
X = linsolve(A,B)
X = 
  댓글 수: 3
I CHUN LIN 2023년 4월 19일
Thank you @Askic V. Regarding this, the output shows "struct with fields: [0×1 sym]". I'm still trying to debug. I think my equations shouldn't be no solution.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Torsten 2023년 4월 19일
편집: Torsten 님. 2023년 4월 19일
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)
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);
ans = 4.5000
ans = 2.5000
ans = 1
grid on
  댓글 수: 4

댓글을 달려면 로그인하십시오.

Sam Chak
Sam Chak 2023년 4월 19일
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)
S = 
  댓글 수: 1
I CHUN LIN 2023년 4월 19일
Thank you @Sam Chak, I know what you mean. However, if A and B are very complicated, I can not use the Substitution-and-Elimination method, but the numerical method. So I want to find whether matlab provides numerical tools (ex. vpasolve, fzero...) which contain symbolic variables.

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by