How to Solve a system of first order differential equation in MATLAB?

66 views (last 30 days)
how to write the command to solve series of differential equation in MATLAB
Dx/Dt = (a+b+c)*x + k*y + l*z
Dy/Dt = k*y + a*x
Dz/Dt = l*z + b*x

Answers (2)

Vaclav Ondra
Vaclav Ondra on 4 Sep 2016
Edited: Vaclav Ondra on 4 Sep 2016
Have a look at examples on ode solvers page. There's an example of the system of ODEs. Simply rewrite your system into Matlab form, i.e.
function dydt = f(t,y)
dydt = [(a+b+c)y(1) + k*y(2) + I*y(3)
k*y(2) + a*y(1)
I*y(3) + b*y(1)];
end
and use, for example, ode45 solver
tspan = [0 1]; % your time interval
y0 = [1; 0; 0]; % your initial conditions
[t,y] = ode45(@f,tspan,y0); % solver
Everything is explained in the documentation.
  3 Comments
Muhammad Sinan
Muhammad Sinan on 19 Oct 2020
You can program this problem is a single script file such as
%% Parameter
a = ?;
b = ?;
c = ?;
k = ?;
I = ?;
%% Initial Conditions
IC = [?, ?, ?];
%% Time span
tinitial = ?;
tfinal = ?;
df = ?;
t = linspace(tinitial, tfinal, df);
%% Model/ system of differential equations
Model = @(t, X) [(a+b+c)*X(1) + k*X(2) + l*(3);
k*X(2) + a*X(1);
l*X(3) + b*X(1)];
%% Solution
sol = ode45(Model, t, IC);
sol = deval(sol, t)
%% Substituitions for the dependent variables
x = X(1);
y = X(2);
z = X(3);
%% Plot by a single figure
plot(t, sol)
legend("x", "y", "z")
%% Plot separately
figure(1)
plot(t, x)
xlabel("Time")
ylabel("Solution of Dx/Dt")
figure(2)
plot(t, y)
xlabel("Time")
ylabel("Solution of Dy/Dt")
figure(3)
plot(t, z)
xlabel("Time")
ylabel("Solution of Dz/Dt")
Please replace the "?" by your desired syntex.
omar A.alghafoor
omar A.alghafoor on 12 Nov 2020
there is error in
%% Substituitions for the dependent variables
x = X(1);
y = X(2);
z = X(3);
Undefined function 'X' for input arguments of type 'double'.
Error in solve_ode45_test (line 201)
x = X(1);
why ???

Sign in to comment.


Nur
Nur on 4 Feb 2023
syms y(x)
ode (x) =diff (y(x),x) == (2*((y(x))^2 + X^2)) / (y(x)*x);
ode(x) =
Invalid expression. Check for missing or extra characters.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
diff(y(x),x) == (2*y(x)^2) + 2*X^2)/(x*y(x))
cond = y(2) == -1;
ySol(x) = dsolve (ode , cond)
ySol(x) =
-(x^4*(X^2/16 + 1/16) - X^2)^(1/2)

Community Treasure Hunt

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

Start Hunting!