How to create a loop for iteration?

조회 수: 5 (최근 30일)
Ajay Guliya
Ajay Guliya 2021년 9월 28일
댓글: Ajay Guliya 2021년 9월 28일
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)

채택된 답변

KSSV
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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by