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!