Hi, I want to solve ode in different interval with different initial condition. Here is the code I have. I wonder if there is way to use while loop or for loop i can use to simplify the code?
st = 10;
n = 20;
c = 0.1;
x0 = 0.5;
p = [0.4, 1];
stoptime1 = linspace(0,st,n);
stoptime2 = linspace(st,2*st,n);
stoptime3 = linspace(2*st,3*st,n);
[t1,x1] = ode15s(@(t,x) 0.4*x*(1-x/k), stoptime1, x0);
[t2,x2] = ode15s(@(t,x) 0.4*x*(1-x/k), stoptime2, x1(end)*(1 - c));
[t3,x3] = ode15s(@(t,x) 0.4*x*(1-x/k), stoptime3, x2(end)*(1 - c));
x = [x1; x2; x3];
t = [t1; t2; t3];
plot(t, x)

 Réponse acceptée

Torsten
Torsten le 19 Août 2022
Modifié(e) : Torsten le 19 Août 2022
0.4*x*(1-x/k)
instead of
0.4*x(1-x/k)
and the variable k has to be set.
st = 10;
n = 20;
c = 0.1;
x0 = 0.5;
p = [0.4, 1];
k = 1;
T = [];
X = [];
for i = 1:3
stoptime = linspace((i-1)*st,i*st,n);
[t,x] = ode15s(@(t,x) 0.4*x*(1-x/k), stoptime, x0);
T = [T;t];
X = [X;x];
x0 = x(end)*(1-c);
end
plot(T,X)

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by