I am starting to learn python and since I have had some programming before ( intro to java last semester and intro to c++ a couple years ago) the teacher gave me an interesting problem to solve. It is an equation called the Prandtl equation from fluid flow measurement it is 1/sqrt(f) = 2.0*log(Re*sqrt(f))-0.8 and the reason the Reynolds number needs to be above 4000 is because the flow must be turbulent for the Prandtl equation to apply to that system

I wrote a program that should(?) solve the equation relatively accurately but I can not get it to execute properly to find out if it works or not, the code is below and I am using pythonwin editor in my attempts to execute it

import math
print "this program solves the prandtl equation by iteration"
Re = input("please input the renolds number now")
if Re<4000: 
		print "not a valid entry"
else:
		for i in range(1,11):
			f= 0.054
			A = 1/sqrt(f)
			B = 2.0*log(Re*sqrt(f))-0.8
			if A<B: 
				f = f-((f-0.008)/2)
				i = i+1
			elif B>A: 
				f= f+((0.1-f)/2)
				i = i+1
			else:
				break
		
print "the friction factor is",f

the program will execute through accepting the Reynolds number but then it stops an error msg that says that sqrt is not defined since I imported the math library I am not sure what that is about
any ideas on what I am doing wrong?

thanks
oneill

Recommended Answers

All 4 Replies

Your import is flawed - try "from math import sqrt"

-- Paul

You don't use the "i" from the for loop so "A" and "B" will always be the same for all 10 iterations through the loop. Also, try running this with these print statements added, which show that the two variables named "i" are different blocks of memory, and that "A" and "B" remain the same. It is bad practice to use "i", "l", or "o" as single letter variable names as they look like numbers.

import math

print "this program solves the prandtl equation by iteration"
rn = input("please input the reynolds number now: ")
f = -1
if rn<4000: 
      print "not a valid entry"
else:
      for i in range(1,11):
         print "for loop i =", i
         f = 0.054
         A = 1/math.sqrt(f)
         B = 2.0*math.log(rn*math.sqrt(f))-0.8
         print "A & B", A, B
         if A<B: 
            f = f-((f-0.008)/2)
            i = 0
            print "     other 'i' =", i
         elif B>A: 
            f= f+((0.1-f)/2)
            i = i+1
         else:
            break
      
print "the friction factor is",f

Also gastables from here https://launchpad.net/ubuntu/karmic/+source/gastables/0.2-2ubuntu1 will solve for Prandtl. You can use this program to check your results for correctness.

Thanks for the suggestions I will try them in a few hours and post the results

Your import is flawed - try "from math import sqrt"

-- Paul

adding that and one for the log function fixed the execution problem

now I just have the logical errors that woooee pointed out to fix but that will have to wait as it is time to get some sleep

thanks for the help I appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.