How to create a loop for iteration?

44 Ansichten (letzte 30 Tage)
Ajay Guliya
Ajay Guliya am 28 Sep. 2021
Kommentiert: Ajay Guliya am 28 Sep. 2021
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)

Akzeptierte Antwort

KSSV
KSSV am 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)

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by