help in this plz

1 次查看(过去 30 天)
Nasir Qazi
Nasir Qazi 2012-2-19
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 个评论
Image Analyst
Image Analyst 2012-2-19
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.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2012-2-19
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 个评论
Walter Roberson
Walter Roberson 2012-2-19
Unfortunately I do not have access to a MATLAB license on weekends.
Jan
Jan 2012-2-19
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.

请先登录,再进行评论。


Jan
Jan 2012-2-19
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);

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by