How do you solve a coupled ODE when one of the ODE results in a vector of length 3 and the other results in a scalar of length 1?

2 visualizzazioni (ultimi 30 giorni)
For instance, in the following example that I found online, if dz(2) were actually a vector, how would you modify this?
[v z] = ode45(@myode,[0 500],[0 1]);
function dz = myode(v,z)
alpha = 0.001;
C0 = 0.3;
esp = 2;
k = 0.044;
f0 = 2.5;
dz = zeros(2,1);
dz(1) = k*C0/f0*(1-z(1)).*z(2)./(1-esp*z(1));
dz(2) = -alpha*(1+esp*z(1))./(2*z(2));
end

Risposta accettata

Torsten
Torsten il 5 Ott 2023
Modificato: Torsten il 5 Ott 2023
All solution components have to be aggregated in one big vector z, and also the derivatives have to be supplied in this vector form. E.g. if the unknows were composed of a vector x of length 4 and a vector y of length 7, you had to work with vectors z and dz of length 4 + 7 = 11.
  4 Commenti
Torsten
Torsten il 5 Ott 2023
Modificato: Torsten il 5 Ott 2023
Let x be a scalar and y a vector of length 2.
Let the equations be
dx/dt = x
dy1/dt = 2*y1
dy2/dt = 3*y2
with initial conditions
x(0) = 1,
y1(0) = 2,
y2(0) = 3.
Then you can set up the problem as
x0 = 1;
y10 = 2;
y20 = 3;
z0 = [x0;[y10;y20]];
tspan = [0 1];
[T,Z] = ode45(@fun,tspan,z0);
X = Z(:,1);
Y = Z(:,2:3);
figure(1)
plot(T,X)
figure(2)
plot(T,Y)
function dzdt = fun(t,z)
x = z(1);
y = z(2:3);
dxdt = x;
dydt = [2*y(1);3*y(2)];
dzdt = [dxdt;dydt];
end

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by