![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/320971/image.png)
Help me with code to solve for loop to solve ODE
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Le Duc Long
le 23 Juin 2020
Commenté : Le Duc Long
le 25 Juin 2020
Hello everybody,
I have a problem with my code. I want to write a code to solve ODE (image). I am trying it with function ode45, I can only write code when value s=const. But I do not how to write a code for s(t) in this case. Can you show me how to do in this case. Thanks so much! Best regards!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/320935/image.png)
0 commentaires
Réponse acceptée
Ameer Hamza
le 23 Juin 2020
Modifié(e) : Ameer Hamza
le 23 Juin 2020
Try this code
tspan = [0 500];
ic = [1; 0; 0; 0; 0];
[t, p] = ode45(@odeFun, tspan, ic);
plot(t, p)
legend({'p', 'p\_dot1', 'p\_dot2', 'p\_dot3', 'p\_dot4'})
function dpdt = odeFun(t, p)
A = [-1 0 0 0 0;
1 -1 0 0 0;
0 1 -1 0 0;
0 0 1 -1 0;
0 0 0 1 -1];
B = [0 1 0 0 0;
0 -1 1 0 0;
0 0 -1 1 0;
0 0 0 -1 1;
0 0 0 0 -1];
lambda = 0.2;
T = 100;
g = T/2;
s0 = 0.5;
if (t-floor(t/T)*T)<g
s = s0;
else
s = 0;
end
dpdt = (lambda*A + s*B)*p;
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/320971/image.png)
12 commentaires
Ameer Hamza
le 25 Juin 2020
Try using https://www.mathworks.com/help/stats/poissrnd.html to generate random number using poisson distribution.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!