Simple equation involving time step

I'm a beginer to MatLAb and I'm looking to practice with the ODE function with model equations. I haven't seen many tutorials on youtube for preditor prey like models. I'm working with a simple equation:
dY/dt = (Y+Z)Y
dZ/dt = (Y+Z)Z
initial at time 0: Y=1, Z=2
total time span is: t = 0:10
time step: 1
I've programmed this function:
tspan = [0 10];
y_Z0 = [1;2];
[t,y_Z] = ode15s(@fun3,tspan,y_Z0);
y=y_Z(:,1);
z=y_Z(:,2);
plot(t,y,t,z)
function dY_dZ_dT = fun3(t,y_Z)
dY_dZ_dT = zeros(2,1);
dY_dZ_dT(1) = (y_Z(1)+y_Z(2)).*y_Z(1);
dY_dZ_dT(2) = (y_Z(1)+y_Z(2)).*y_Z(2);
end
I'm not sure this is the output I'm expecting could I have help with just the general layout of the model as a template so I can practice similar equations for experience?
Thanks in advance

3 个评论

The problem is that that both variables increase exponentially, so after about 0.33 time units, the results become infinite. Although ‘fun3’ appears to be coded correctly with respect to ‘dY/dt’ and ‘dZ/dt’ those functions may not be correct.
tspan = [0 10];
y_Z0 = [1;2];
[t,y_Z] = ode15s(@fun3,tspan,y_Z0);
Warning: Failure at t=3.306903e-01. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (8.881784e-16) at time t.
y=y_Z(:,1);
z=y_Z(:,2);
figure
plot(t,y,t,z)
figure
semilogy(t,y,t,z)
grid
function dY_dZ_dT = fun3(t,y_Z)
dY_dZ_dT = zeros(2,1);
dY_dZ_dT(1) = (y_Z(1)+y_Z(2)).*y_Z(1);
dY_dZ_dT(2) = (y_Z(1)+y_Z(2)).*y_Z(2);
end
.
I agree with Star Strider that it seems like you implemented those equations correctly, but I don't think they're the correct equations. Take a look at the Wikipedia page for the Lotka-Volterra equations. α, β, γ, and δ in those equations are all positive and real, so each growth rate equation has one positive term and one negative term. Your equations have two positive terms.
I see thank you so the equation itself was the problem as oppsed to the programming

请先登录,再进行评论。

回答(0 个)

类别

产品

版本

R2022b

标签

提问:

2023-7-26

移动:

2023-7-26

Community Treasure Hunt

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

Start Hunting!

Translated by