How to create a loop for iteration?
43 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
KSSV
2021-9-28
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!