Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Help with for loop
조회 수: 2 (최근 30일)
이전 댓글 표시
I realize there is probably a lot wrong with this code, but I have all the values defined correctly that are used. Im trying to iterate f1 three times with the first one being a guess. I am calculating the correct first V1 and Re but im having trouble solving for the next f1 and then I want to throw that one back in to the top in place of my first guess. My goal is to get the f1 value until it is asymptotes to one value. Thanks
% code
r=1.94
u=2.09*10^-5
L1=50
L2=50
ed=0.0024
g=32.2
d1=((2.5)/12)
d2=(3/12)
w=7.5*550
A1=pi*(d1/2)^2
A2=pi*(d2/2)^2
hp=((w)/(g*r))
K1=0.95+(0.64-0.95)*(((d1)*12-2)/(4-2))
K2=0.95+(0.64-0.95)*(((d2)*12-2)/(4-2))
for i=1:3
f1(i)=0.024
V1(i)=sqrt((2*g*d1*hp)/(L1*f1(i)))
Re(i)=(r*V1(i)*d1)/u
syms f1
eqn = (1/sqrt(f1(i))) == -2.0*log10((ed)/(3.7)+(2.51)/(Re(i)*sqrt(f1(i))))
S = solve(eqn,f1(i))
end
댓글 수: 2
답변 (2개)
KSSV
2017년 11월 6일
r=1.94 ;
u=2.09*10^-5 ;
L1=50 ;
L2=50 ;
ed=0.0024 ;
g=32.2 ;
d1=((2.5)/12) ;
d2=(3/12) ;
w=7.5*550 ;
A1=pi*(d1/2)^2 ;
A2=pi*(d2/2)^2 ;
hp=((w)/(g*r)) ;
K1=0.95+(0.64-0.95)*(((d1)*12-2)/(4-2)) ;
K2=0.95+(0.64-0.95)*(((d2)*12-2)/(4-2)) ;
for i=1:3
f1(i)=0.024 ;
V1(i)=sqrt((2*g*d1*hp)/(L1*f1(i))) ;
Re(i)=(r*V1(i)*d1)/u ;
syms f2
% eqn = (1/sqrt(f1(i))) == -2.0*log10((ed)/(3.7)+(2.51)/(Re(i)*sqrt(f1(i)))) ;
eqn = (1/sqrt(f2)) == -2.0*log10((ed)/(3.7)+(2.51)/(Re(i)*sqrt(f2))) ;
S = solve(eqn,f2) ;
end
f1 cannot be both a double and sym...you have to use different variable for sym.
댓글 수: 0
Walter Roberson
2017년 11월 6일
Your code
for i=1:3
f1(i)=0.024
V1(i)=sqrt((2*g*d1*hp)/(L1*f1(i)))
Re(i)=(r*V1(i)*d1)/u
syms f1
eqn = (1/sqrt(f1(i))) == -2.0*log10((ed)/(3.7)+(2.51)/(Re(i)*sqrt(f1(i))))
S = solve(eqn,f1(i))
end
is exactly equivalent to the code
for i=1:3
f1(i)=0.024
V1(i)=sqrt((2*g*d1*hp)/(L1*f1(i)))
Re(i)=(r*V1(i)*d1)/u
f1 = sym('f1');
eqn = (1/sqrt(f1(i))) == -2.0*log10((ed)/(3.7)+(2.51)/(Re(i)*sqrt(f1(i))))
S = solve(eqn,f1(i))
end
Notice how just before eqn, you replace the entire numeric array f1 with a scalar symbol f1 . The first iteration that does something because a scalar symbol can be indexed at location 1.
Then on the second iteration, with f1 now being symbolic, you set the second element of the symbolic array f1 to sym(0.024), which is a valid operation. Then you again replace all of f1 with sym('f1'), making f1 a scalar symbol again. Then you try to access f1(2) which does not exist because f1 is a scalar symbol.
You should almost always avoid using the same variable name for symbolic and numeric purposes; the chances are just too high that you will get confused about whether the name is currently referring to symbolic or numeric entries.
댓글 수: 1
Walter Roberson
2017년 11월 6일
i<1 is never true in "for i=1:3"
Your code accesses f1(i) but never defines it.
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!