필터 지우기
필터 지우기

returning values for iterations

조회 수: 1 (최근 30일)
harley
harley 2013년 8월 26일
Partial code below. I want to use the Va calculated at the bottom as the Vo in the next iteration, how would i do this. The initial Vo is only there to get iterations rolling.
Vn = 2:0.1:30;% iteration length.
Vo = 2;% initial Vo to calc Re and f.
%
Re = (Vo.*D) / nu;
%
if Re < 2300
f = 64 / Re;
else
darbyFormula = @(x) 1/sqrt(x)+2*log10(eoverD/3.7 + 2.51/Re/sqrt(x));
f = fzero(darbyFormula,0.01);
end
%
dPloss_1 = (f*rho*(L/D)) * ((Vn*Vo)./2);
Va = (alpha*Vn)+((1-alpha)*Vo); % this to become the new Vo for next iteration.
Q=Va*A

채택된 답변

David Sanchez
David Sanchez 2013년 8월 26일
Just set the equality:
Vo = Va; % right after your last line of code.
Like this:
Vn = 2:0.1:30;% iteration length.
Vo = 2;% initial Vo to calc Re and f.
%
Re = (Vo.*D) / nu;
%
if Re < 2300
f = 64 / Re;
else
darbyFormula = @(x) 1/sqrt(x)+2*log10(eoverD/3.7 + 2.51/Re/sqrt(x));
f = fzero(darbyFormula,0.01);
end
%
dPloss_1 = (f*rho*(L/D)) * ((Vn*Vo)./2);
Va = (alpha*Vn)+((1-alpha)*Vo); % this to become the new Vo for next iteration.
Q=Va*A;
Vo = Va;
But shouldn't you use a while or for , instead of a if in your code? I don't know how it is implemented, but it should go more or less like this:
Vo = 2;% initial Vo to calc Re and f.
for Vn = 2:0.1:30;% iteration length.
%
Re = (Vo.*D) / nu;
%
if Re < 2300
f = 64 / Re;
else
darbyFormula = @(x) 1/sqrt(x)+2*log10(eoverD/3.7 + 2.51/Re/sqrt(x));
f = fzero(darbyFormula,0.01);
end
%
dPloss_1 = (f*rho*(L/D)) * ((Vn*Vo)./2);
Va = (alpha*Vn)+((1-alpha)*Vo); % this to become the new Vo for next iteration.
Q=Va*A;
Vo = Va;
end % for Vn = 2:0.1:30;
  댓글 수: 1
harley
harley 2013년 8월 26일
편집: harley 2013년 8월 26일
thanks David, when i run the above, it only gives me a value when Vn is 30. That is it doesn't iterate from 2:0.1:30

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by