How to solve Coupled Differential Equations

Antworten (3)

Try this —
syms x(t) y(t) x0 y0
Dx = diff(x);
Dy = diff(y);
ode1 = Dy-Dx == 2 - x
ode1(t) = 
ode2 = 2-Dy - Dy == 3 + 2*y
ode2(t) = 
S = dsolve(ode1, ode2, x(0)==x0, y(0)==y0)
S = struct with fields:
y: [1×1 sym] x: [1×1 sym]
x(t) = simplify(S.x, 500)
x(t) = 
y(t) = simplify(S.y, 500)
y(t) = 
.

4 Kommentare

Lorenzo
Lorenzo am 8 Feb. 2025
There is a way to plot the solutions x(t) and y(t) ?
Thanks
No, it is not possible to plot y(t) . y(t) involves three independent variables: t, x0, and y0. There are no built-in functions for plotting 3 independent variables plus one resultant variable.
Lorenzo
Lorenzo am 8 Feb. 2025
Thank you
Of course, for given x0 and y0, it is easy enough to plot.
syms x(t) y(t) x0 y0
Dx = diff(x);
Dy = diff(y);
ode1 = Dy-Dx == 2 - x
ode1(t) = 
ode2 = 2-Dy - Dy == 3 + 2*y
ode2(t) = 
S = dsolve(ode1, ode2, x(0)==x0, y(0)==y0)
S = struct with fields:
y: exp(-t)*(y0 - exp(t)/2 + 1/2) x: exp(t)*(x0 - y0/2 + (9*exp(-t))/4 - 9/4) + (exp(-t)*(y0 - exp(t)/2 + 1/2))/2
x(t) = simplify(S.x, 500)
x(t) = 
y(t) = simplify(S.y, 500)
y(t) = 
X0 = -2;
Y0 = 1.5;
sX = subs(x, [x0, y0], [X0, Y0])
sX(t) = 
sY = subs(y, [x0, y0], [X0, Y0])
sY(t) = 
fplot(sX, [0 5])
fplot(sY, [0 5])

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 9 Mai 2021

0 Stimmen

yprime - xprime == 2-x, 2*xprime - yprime == 3 + 2*y
2*yprime - 2*xprime == 4-2*x, 2*xprime - yprime == 3 + 2*y
2*yprime - 2*xprime + 2*xprime - yprime == 4-2*x + 3 + 2*y
yprime == 7 - 2*x + 2*y
7 - 2*x + 2*y - xprime == 2*x
7 - 2*x + 2*y - 2*x == xprime
xprime = 7 - 4*x + 2*y
So...
function dxy = odefun(t, xy)
dxy = [7 - 4*xy(1) + 2*xy(2); 7 - 2*xy(1) + 2*xy(2)];
end

4 Kommentare

Tomasz
Tomasz am 9 Mai 2021
this:
2*yprime - 2*xprime == 4-2*x, 2*xprime - yprime == 3 + 2*y
2*yprime - 2*xprime + 2*xprime - yprime == 4-2*x + 3 + 2*y
is it OK??
2*yprime - 2*xprime == 4-2*x, 2*xprime - yprime == 3 + 2*y
add the two equations
(2*yprime - 2*xprime) + (2*xprime - yprime) == (4-2*x) + (3 + 2*y)
syms xprime yprime x y
eqn = [yprime - xprime == 2-x, 2*xprime - yprime == 3 + 2*y]
eqn = 
sol = solve(eqn, [xprime, yprime])
sol = struct with fields:
xprime: [1×1 sym] yprime: [1×1 sym]
sol.xprime
ans = 
sol.yprime
ans = 
Hmmm, where did I go wrong?
I did make a mistake, but it was the step after you indicated. I copied as 2*x instead of 2-x . The corrected version is
yprime - xprime == 2-x, 2*xprime - yprime == 3 + 2*y
2*yprime - 2*xprime == 4-2*x, 2*xprime - yprime == 3 + 2*y
2*yprime - 2*xprime + 2*xprime - yprime == 4-2*x + 3 + 2*y
yprime == 7 - 2*x + 2*y
7 - 2*x + 2*y - xprime == 2 - x %corrected
7 - 2*x + 2*y - (2 - x) == xprime
xprime = 5 - x + 2*y
So...
function dxy = odefun(t, xy)
dxy = [5 - xy(1) + 2*xy(2); 7 - 2*xy(1) + 2*xy(2)];
end

Melden Sie sich an, um zu kommentieren.

Sam Chak
Sam Chak am 8 Feb. 2025
Primarily, coupled ordinary differential equations (ODEs) in additive form can be decoupled using the substitution and elimination method (Grade 10 algebra). In this simple system, we can decouple the ODEs simultaneously using the matrix method.
The coupled ODEs
can be expressed in matrix form
and simplified to
.
Because of the square matrix property , we can manipulate the matrix equation to become
so that we can solve the following decoupled ODEs
.
Method 1: Numerical approach
% Describe the ODEs inside the function
function dx = ode(t, x)
dx(1) = 5 - 1*x(1) + 2*x(2); % ODE 1
dx(2) = 7 - 2*x(1) + 2*x(2); % ODE 2
dx = [dx(1)
dx(2)]; % arranged in column vector
end
% Call ode45 to numerically solve the system
tspan = [0 10]; % time interval from 0 to 10
xinit = [1; 0]; % initial condition at time 0
[t, x] = ode45(@ode, tspan, xinit);
plot(t, x), grid on, xlabel('Time'), legend('x(t)', 'y(t)')
title('Numerical Solutions')
Method 2: Symbolical approach to obtain the analytical solutions (using @Star Strider's code):
%% Requires Symbolic Math Toolbox
syms x(t) y(t) x0 y0
Dx = diff(x);
Dy = diff(y);
ode1 = Dy - Dx == 2 - x
ode1(t) = 
ode2 = 2*Dx - Dy == 3 + 2*y
ode2(t) = 
S = dsolve(ode1, ode2, x(0)==1, y(0)==0) % specify the initial condition here
S = struct with fields:
y: - exp(t/2)*sin((7^(1/2)*t)/2)*((exp(-t/2)*(21*sin((7^(1/2)*t)/2) + 25*7^(1/2)*cos((7^(1/2)*t)/2)))/14 - (17*7^(1/2))/14) - exp(t/2)*cos((7^(1/2)*t)/2)*((exp(-t/2)*(21*co... x: - ((3*exp(t/2)*cos((7^(1/2)*t)/2))/4 + (7^(1/2)*exp(t/2)*sin((7^(1/2)*t)/2))/4)*((exp(-t/2)*(21*cos((7^(1/2)*t)/2) - 25*7^(1/2)*sin((7^(1/2)*t)/2)))/14 - 3/2) - ((3*exp(...
x(t) = simplify(S.x, 500)
x(t) = 
y(t) = simplify(S.y, 500)
y(t) = 
fplot(x(t), [0 10]), hold on, grid on, ylim([-300, 500])
fplot(y(t), [0 10]), hold off, xlabel('Time'), legend('x(t)', 'y(t)')
title('Analytical Solutions')

Gefragt:

am 9 Mai 2021

Kommentiert:

am 8 Feb. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by