Store value into matrix, and then perform calculation upon each element

조회 수: 2 (최근 30일)
Ying
Ying 2012년 2월 6일
편집: Matthew Crema 2013년 10월 8일
Hi, I have a question about performing calculation on each matrix elements.
My code is here:
syms m a x u;
a=16807;
m=2^31-1;
x=1;
for i=1:10^2
y=a*x
x=mod(y,m)
u(i)=x/m
X = betainv(u,2,3); % beta distributed random fraction of defective parts
Y = logninv(u,5,3); % lognormal distributed demand amount
Q = 100;
Revenue=min(Y,(1-X)*Q)*53; %the total revenue
Cost=27*Q;
Salvage=3*max(Q*(1-X)-Y,0)+X*Q; %all the unsolde parts are salvaged, defective parts included
p=Revenue+Salvage-Cost;
end;
expectedprofit=mean(p)
Basically, I generate a series of uniformly distributed random numbers. And then I use these numbers to genearate beta and lognormal distributed ones, and I would like to store them into matrix X and Y. Then I want to perform the profit calculation (revenue-cost+salvage cost etc.) for each of the values in X and Y. I just don't know how to do that.
I tried to put things as X(i) and Y(i) (to store value into matrix), but MLATLAB finds an error in that step.
I just started learning Monte Carlo simulation using MATLAB. Hope somebody can help me out here.
Thanks a lot! Ying

채택된 답변

Ondrej
Ondrej 2012년 2월 6일
I guess you want to do
X(i) = betainv(u(i),2,3);
Y(i) = logninv(u(i),5,3);
you either missed the u(i) index, or you want to store growin u, in that case X(:,i), Y(:,i) would be some matrices (but with growing columnsize..size in your case u is growing).
  댓글 수: 2
Ying
Ying 2012년 2월 6일
Hi Ondrej,
Thanks for such quick answer.
I tried your way of writing X(:,i) and Y(:,i) and it works!
But the other way your suggested which is to write up u(i) and X(i) and Y(i) still yields error message in MATLAB.
The u value is growing from 1 column to 100 column as I put in the loop,and I am puzzled that why wouldn't
X(i) = betainv(u(i),2,3);
Y(i) = logninv(u(i),5,3);
work? What's different from writing X(:,i) and Y(:,i)?
Thanks for helping me out!
Ying
Ondrej
Ondrej 2012년 2월 11일
Well, the difference is that X(i) means X is a vector and X(:,i) is a matrix (meaning the i-th column of X). See http://www.mathworks.com/help/techdoc/math/f1-85462.html
Btw. if you do for loops with variables growing inside, consider preallocating: http://www.mathworks.com/support/solutions/en/data/1-18150/
As for your code, it doesn't work for me because of the symbolic variables in betainv, so I think I can't help you with that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by