Help With Running while loop for a list

1 次查看(过去 30 天)
Joe
Joe 2014-4-2
评论: Joe 2014-4-2
I am able to run this doing it for a single value for P, but now i would like to do it for a range of values for P (i.e. a vector of 100 P values from 1 to 100), however I am getting an error and am not sure that I understand it.
Here is the code with the single P value problem
function Z = RKZ(epsilon, Sigma, sigma, P, Pc, Tc, T, phi, xh, xl)
format long
Pr = P/Pc
Tr = T/Tc
x = xl + xh
beta = Sigma*(Pr/Tr)
q = phi/(Sigma*Tr^(1.5))
Y = @(X) 1 + beta - q*beta*((X-beta)/((X + epsilon*beta)*(X + sigma*beta))) - X
E = 0.0000000000001
Yx = Y(x)
while abs(Yx) > E
x = (xl + xh)/2
Yx = Y(x)
if Y(x) > 0
xh = x
else
xl = x
end
x
end
Z = x
end
And this is the code for the vector of P values (the one that I am having issues with):
function Z = Zplot(epsilon, Sigma, sigma, p1, p2, Pc, T, Tc, phi, xh, xl)
P = linspace(p1, p2, 100)
format long
Pr = P./Pc
Tr = T./Tc
i = 1:length(P)
x = ones(1,100)
beta = Sigma.*(Pr./Tr)
q = phi./(Sigma.*Tr.^(1.5))
Y = @(X) 1 + beta - q.*beta.*((X-beta)./((X + epsilon*beta).*(X + sigma*beta))) - X
E = 0.0000000000001
Yx = Y(x)
for i = 1:length(Yx)
while abs(Yx(i)) > E
x(i) = (xl + xh)/2
Yx(i) = Y(x(i))
if Y(x(i)) > 0
xh = x(i)
else
xl = x(i)
end
x (i)
end
Z = x
end
end
I input these values:
Zplot(0, 0.08664, 1, 1, 100, 73.83, 304.2, 325, 0.42748, 0, 1)
and this was the error that I got:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Zplot (line 17)
Yx(i) = Y(x(i))
  2 个评论
dpb
dpb 2014-4-2
...however I am getting an error and am not sure that I understand it.
Well, I'm sure we don't when you don't let us know what the error is. Cut 'n paste the whole error text in context w/ the code that used to call the function.
Joe
Joe 2014-4-2
I apologize for being unclear, I added these details in the question.

请先登录,再进行评论。

回答(0 个)

类别

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