Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Help with for loop

2 vues (au cours des 30 derniers jours)
Jim Tom
Jim Tom le 6 Nov 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaires
KSSV
KSSV le 6 Nov 2017
Define all the variables...you have not ,mentioned all variables.
Rena Berman
Rena Berman le 7 Nov 2017
(Answers Dev) Restored edit

Réponses (2)

KSSV
KSSV le 6 Nov 2017
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.

Walter Roberson
Walter Roberson le 6 Nov 2017
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 commentaire
Walter Roberson
Walter Roberson le 6 Nov 2017
i<1 is never true in "for i=1:3"
Your code accesses f1(i) but never defines it.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by