필터 지우기
필터 지우기

Symbolic solution is too long

조회 수: 6 (최근 30일)
Bohan Li
Bohan Li 2024년 4월 3일
답변: Star Strider 2024년 4월 3일
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?

답변 (2개)

Torsten
Torsten 2024년 4월 3일
편집: Torsten 2024년 4월 3일
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 2024년 4월 3일
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.
.

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by