Symbolic solution is too long

39 views (last 30 days)
Bohan Li
Bohan Li on 3 Apr 2024 at 16:37
Answered: Star Strider on 3 Apr 2024 at 17:58
Hi there,
After attempting to solve a system of differential equation using the dsolve function, the symbolic solutions I have obtained seemed to be continuing at tedious length. Attached is the matlab code:
clear
clc
syms t y1(t) y2(t) y3(t) y4(t) % Define symbolic functions
% Parameters
syms k1 k2 k3 k4 A B alpha; % Define symbolic parameters
% Define system of differential equations
eqn1 = diff(y1(t), t) == k1*A*alpha*y3(t) - k2*y1(t) + k3*alpha*B*y2(t) - k4*y1(t);
eqn2 = diff(y2(t), t) == k1*A*y4(t) - k2*y2(t) - k3*alpha*B*y2(t) + k4*y1(t);
eqn3 = diff(y3(t), t) == -k1*A*alpha*y3(t) + k2*y1(t) + k3*B*y4(t) - k4*y3(t);
eqn4 = diff(y4(t), t) == -k1*A*y4(t) + k2*y2(t) - k3*B*y4(t) + k4*y3(t);
% Initial conditions
initCond = [y1(0) == 0, y2(0) == 0, y3(0) == 0, y4(0) == 1];
% Solve the system of differential equations
sol = dsolve([eqn1, eqn2, eqn3, eqn4], initCond);
% Extracting solutions
y1Sol(t) = sol.y1;
y2Sol(t) = sol.y2;
y3Sol(t) = sol.y3;
y4Sol(t) = sol.y4;
% Display general solutions
disp("General Solutions:");
disp(y1Sol);
disp(y2Sol);
disp(y3Sol);
disp(y4Sol);
Did I take the wrong approach in solving this system of differential equations?

Answers (2)

Torsten
Torsten on 3 Apr 2024 at 17:09
Edited: Torsten on 3 Apr 2024 at 17:10
Did I take the wrong approach in solving this system of differential equations?
It depends on what you want.
If you want a general solution with the numerical values of all parameters being unspecified, you could try your approach with the modifications
syms k1 k2 k3 k4 A B alpha real
instead of
syms k1 k2 k3 k4 A B alpha
and
sol = dsolve([eqn1, eqn2, eqn3, eqn4], initCond,'MaxDegree',4);
instead of
sol = dsolve([eqn1, eqn2, eqn3, eqn4], initCond);
If you want solution curves with values specified for k1 k2 k3 k4 A B alpha, use "ode45" or "ode15s" instead of "dsolve".

Star Strider
Star Strider on 3 Apr 2024 at 17:58
I added the simplify calls and let this run for a while in MATLAB Online —
clear
clc
tic
syms t y1(t) y2(t) y3(t) y4(t) % Define symbolic functions
% Parameters
syms k1 k2 k3 k4 A B alpha; % Define symbolic parameters
% Define system of differential equations
eqn1 = diff(y1(t), t) == k1*A*alpha*y3(t) - k2*y1(t) + k3*alpha*B*y2(t) - k4*y1(t);
eqn2 = diff(y2(t), t) == k1*A*y4(t) - k2*y2(t) - k3*alpha*B*y2(t) + k4*y1(t);
eqn3 = diff(y3(t), t) == -k1*A*alpha*y3(t) + k2*y1(t) + k3*B*y4(t) - k4*y3(t);
eqn4 = diff(y4(t), t) == -k1*A*y4(t) + k2*y2(t) - k3*B*y4(t) + k4*y3(t);
% Initial conditions
initCond = [y1(0) == 0, y2(0) == 0, y3(0) == 0, y4(0) == 1];
% Solve the system of differential equations
sol = dsolve([eqn1, eqn2, eqn3, eqn4], initCond);
% Extracting solutions
y1Sol(t) = simplify(sol.y1, 500);
y2Sol(t) = simplify(sol.y2, 500);
y3Sol(t) = simplify(sol.y3, 500);
y4Sol(t) = simplify(sol.y4, 500);
% Display general solutions
disp("General Solutions:");
disp(y1Sol);
disp(y2Sol);
disp(y3Sol);
disp(y4Sol);
toc
The results are in the file (too long to include here as text).
I defer to you to determine if that is an improvement.
With:
Elapsed time is 2462.077667 seconds.
or 41 minutes, 2.0777 seconds.
.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!