How to create a loop for iteration?
6 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
KSSV
on 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)
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!