How to create a loop for iteration?
44 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi
I am new to Matlab, and trying to create code to calculate some values. I the code below, for different value of k, I am finding the value of lambda and then value of P. Instead of taking value of k one by one, is there a more efficient way of doing it, say by using a loop?
e=2.7183;
gamma=0.1333;
k = [1,9,23,7,23,9,20,29,63,102,73,59,27,130,75,185,70,92,326]
Rt=0:0.01:10;
lambda = k(1)*exp(gamma*(Rt-1))
P1 = ((lambda.^k(1+1)).*exp(-lambda))/factorial(k(1+1))
lambda = k(2)*exp(gamma*(Rt-1))
P2 = ((lambda.^k(2+1)).*exp(-lambda))/factorial(k(2+1))
lambda = k(3)*exp(gamma*(Rt-1))
P3 = ((lambda.^k(3+1)).*exp(-lambda))/factorial(k(3+1))
lambda = k(4)*exp(gamma*(Rt-1))
P4 = ((lambda.^k(4+1)).*exp(-lambda))/factorial(k(4+1))
plot(Rt,P1,Rt,P2,Rt,P3,Rt,P4)
0 commentaires
Réponse acceptée
KSSV
le 28 Sep 2021
e=2.7183;
gamma=0.1333;
k = [1,9,23,7,23,9,20,29,63,102,73,59,27,130,75,185,70,92,326] ;
Rt=0:0.01:10;
P = zeros(length(k)-1,length(Rt)) ;
for i = 1:length(k)-1
lambda = k(i)*exp(gamma*(Rt-1)) ;
P(i,:) = ((lambda.^k(i+1)).*exp(-lambda))/factorial(k(i+1)) ;
end
plot(Rt,P)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Multidimensional Arrays 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!