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