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?

조회 수: 3 (최근 30일)
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
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by