help in this plz

1 view (last 30 days)
Nasir Qazi
Nasir Qazi on 19 Feb 2012
I have Values like this
C2 = 0.10;
C3 = 0.10;
C4 = 0.20;
C5 = 0.20;
C6 = 0.20;
P1=190;
P2=72.2;
P3=51.6;
P4=20.44;
P5=15.57;
P6= 4.956;
K1=P1/P;
K2=P2/P;
K3=P3/P;
K4=P4/P;
K5=P5/P;
K6=P6/P;
note :- this is very lengthy and I want to make it shorter and calculating like this
for c = [0.20,0.10,0.10,0.20,0.20,0.20]
for P = [190,72.2,51.6,20.44,15.57,4.956]
PG = 50;
K = (P/PG);
A = c*(K-1);
B = c*(K-1)/K;
nv = A/(A-B);
disp(nv)
end
end
Can I use these code to calculate it in a simpler way or I am need to change the code , suggest plz
thanks
  1 Comment
Image Analyst
Image Analyst on 19 Feb 2012
Inside your loop, the c's cancel out in the nv division, so nv, K, and PG don't depend on c at all so why are they in a loop over c? Also, in the first chunk of code, I assume P1 = P(1), so what is P? You can't do K(1) = P(1) ./ P because the dimensions don't match so I have no idea what you want to do.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 19 Feb 2012
Yes, you can use loops such as you show. Your "for" syntax is valid.
You might want to store the answers after you calculate them, though.
  3 Comments
Walter Roberson
Walter Roberson on 19 Feb 2012
Unfortunately I do not have access to a MATLAB license on weekends.
Jan
Jan on 19 Feb 2012
The code runs fine and "nv" is calculated - but only the last value is stored. Because "nv" does not depend on "c" at all, the outer loop can be omitted.

Sign in to comment.


Jan
Jan on 19 Feb 2012
You nv does not depend on c, such that the calculation of nv can be simplified to:
nv = K / (K-1);
or:
nv = 1 / (1 - PG/P);

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!