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?
1 次查看(过去 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
0 个评论
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!