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

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.

 Réponse acceptée

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 commentaires

Hi @Askic V, thanks a lot. This method shows no error.
It indeed shows no error, but if this is applicable to your complicated example, (since you stated you want numerical approach)?
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.

Connectez-vous pour commenter.

Plus de réponses (2)

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 commentaire

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.

Connectez-vous pour commenter.

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)
ans = 4.5000
Y(1,2)
ans = 2.5000
Y(1,3)
ans = 1
grid on

4 commentaires

Hi @Torsten, I'm just considering the below example,
clear
clc
syms x1 x2 x3
a = 1;
b = 2;
c = 3;
d = 4;
Eq1 = (a+1i*b)*x1 + 1i/2*x2*c - 1i/2*conj(x2)*d == 0;
Eq2 = (-a+1i*c)*x1 + 1i/2*d*x2 + 1i/2*b*x3 + a*conj(x3) == 0;
Solution = solve([Eq1, Eq2], [x1 x2]);
% Extract x1 from Solution:
Solve_x1 = extractfield(Solution, 'x1');
Solution_x1 = vertcat(Solve_x1{:});
I want to solve the differential equation to find x3, like
syms x3(t) x0
ode = diff(x3, t) == 1i*c*Solution_x1;
dsolve(ode, x3(0) == x0)
Matlab warns that "Unable to find symbolic solution." because of the nonlinear equation (I guess). So I try instead:
fun = matlabFunction(Solution_x1)
tspan = [0 1];
[t, x3] = ode15s(fun, tspan, 1);
plot(t, x3, '-')
But there are errors. How can I use ode15s or others to solve this? Thank you very much.
I stick to your program structure of first solving the algebraic equations and inserting the result into the differential equations.
This won't usually work if the algebraic equations are difficult.
You should try my method from above for more complicated systems (i.e. solving algebraic and differential equations all together using ode15s).
syms x1 x2 x3
a = 1;
b = 2;
c = 3;
d = 4;
Eq1 = (a+1i*b)*x1 + 1i/2*x2*c - 1i/2*conj(x2)*d == 0;
Eq2 = (-a+1i*c)*x1 + 1i/2*d*x2 + 1i/2*b*x3 + a*conj(x3) == 0;
Solution = solve([Eq1, Eq2], [x1 x2]);
x1 = matlabFunction(Solution.x1);
x2 = matlabFunction(Solution.x2);
options = odeset('RelTol',1e-10,'AbsTol',1e-10);
fun = @(x,t) 1i*c*x1(x(1));
tspan = [0:0.05: 1];
y0 = 1;
[T,X3] = ode15s(fun,tspan,y0,options);
figure(1)
plot(T,[real(X3),imag(X3),abs(X3)])
figure(2)
plot(T,[real(x1(X3)),imag(x1(X3)),abs(x1(X3))])
figure(3)
plot(T,[real(x2(X3)),imag(x2(X3)),abs(x2(X3))])
Yes! I made it! Thank you @Torsten, you help me a lot.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by