For looping problem: storing all answer in array

Hi, can anyone help me with the output value in looping. I try to run the code but the answer just shows the answer from final iteration only (when Lm=90). How can I put all answer in an array like when Lm(0,30,60,90), so I have 4 last answers. Thank you.
function[qopt,ASr,ASs,AS]= loopingtry(alpha,Av,Std,n)
p=15;v=2;w=10;Lc=30/365;Lv=30/365;dx=0.01;c=4;
gammac=alpha*exp(alpha*Lc)*(1/(1-exp(-alpha/n)));
gammav=alpha*exp(-alpha*(Lv+(1/n)))*(1/(1-exp(-alpha/n)));
for Lm=0:30:90
Lz=Lm/365;
gammaz=alpha*exp(-alpha*Lz)*(1/(1-exp(-alpha/n)));
Proba=(n*p-w*gammaz)/(n*p-v*gammav);
qopt=ceil(Av+norminv(Proba)*Std);
I1=0;I2=0;
for y=0:dx:qopt-dx
fx=pdf('Normal',y,Av,Std);
fxplus=pdf('Normal',y+dx,Av,Std);
I1=I1+0.5*(y*fx +(y + dx)*fxplus)*dx;
I2 = I2+0.5*(fx + fxplus)*dx;
end
loss = (Av - qopt + (qopt * I2) - I1);
ASr= n*p * (Av - loss) + v * (qopt - Av + loss) * gammav- w*qopt*gammaz;
ASs=w * qopt * gammaz - c * qopt * gammac;
AS=ASr+ASs;
end
end
[qopt,ASr,ASs,AS]= loopingtry(0.2,1000,800,1)

 采纳的回答

...
IDX=0:30:90; % make the loop values variable
qopt=zeros(size(IDX);, % to save temporary when preallocate
ASr=qopt;, ASs=qopt; AS=qopt; % and to make mod's easier later on
idx=0; % initial loop count index
for Lm=IDX
idx=idx+1;
...
qopt(idx)=ceil(Av+norminv(Proba)*Std);
I1=0;I2=0;
for y=0:dx:qopt-dx
fx=pdf('Normal',y,Av,Std);
fxplus=pdf('Normal',y+dx,Av,Std);
I1=I1+0.5*(y*fx +(y + dx)*fxplus)*dx;
I2 = I2+0.5*(fx + fxplus)*dx;
end
loss = (Av - qopt + (qopt * I2) - I1);
ASr(idx)= n*p * (Av - loss) + v * (qopt - Av + loss) * gammav- w*qopt*gammaz;
ASs(idx)=w * qopt * gammaz - c * qopt * gammac;
AS(idx)=ASr+ASs;
end
end

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by