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?

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

 採用された回答

Torsten
Torsten 2023 年 10 月 5 日
編集済み: Torsten 2023 年 10 月 5 日
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 件のコメント

L'O.G.
L'O.G. 2023 年 10 月 5 日
編集済み: L'O.G. 2023 年 10 月 5 日
Thanks. Could you please elaborate using this example? Here z is already a vector. To supply the derivatives in vector form, we could no longer have dz(2). It would have to be something like a cell? Specifically, I would like to know what happens if the first ode results in a scalar and the second results in a vector as mentioned given this example.
In this sample snippet of code, x is a scalar and y is a vector. z contains the contents of both x and y.
x = 42;
y = [4; 8; 15; 16; 23; 42];
z = [x; y]
z = 7×1
42 4 8 15 16 23 42
I could have done this with indexing but concatenation works just as well.
w = zeros(numel(x)+numel(y), 1);
w(1) = x;
w(2:end) = y
w = 7×1
42 4 8 15 16 23 42
Torsten
Torsten 2023 年 10 月 5 日
編集済み: Torsten 2023 年 10 月 5 日
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
L'O.G.
L'O.G. 2023 年 10 月 5 日
Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

製品

リリース

R2021b

質問済み:

2023 年 10 月 5 日

編集済み:

2023 年 10 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by