我试图用牛顿的方法,用Python来近似Colebrook方程的根。目前代码没有错误,但也没有打印/绘图。我明白这可能是一个个案,并感谢所有的帮助提前! enter image description here
numpy as np
import random
import matplotlib.pyplot as plt
#Define Variables, k=e/D, E+04<=x=Re<=E+07
k=0.0001
#Define Colebrook function, f=x=independent variable
def f(x):
return -0.86*(np.log(( 2.51/(Re*np.sqrt(x))) + ( k/3.7))) - (1/np.sqrt(x))
#Define derivative function by definition & approximation
def derivative(f,x,h):
return (f(x + h) - f(x))/h
#Newton's method approximation with initial gussed x value
x=0.01
for Re in range (10^4,10^7, 10000):
m=derivative(f,x,h=0.01)
b=f(x)-m*x #y=mx+b
newx=-b/m #new x value determined by the tangent line
if abs(g(newx))<= (1/10000000000):
print ('root =' + newx)
else:
x=newx
#plot
for Re in range (10^4, 10^7,10000):
x=newx
plt.plot (f(x))
我已经知道我要找的根大约是0.03。你知道吗
最明显的问题是:
没有输出。您应该查看
range
的定义:第三个输入是步长,而不是步数;我想您应该使用“power”、10**4
、10**7
。^
符号是按位异或。你知道吗我看到了几个问题:
g(x)
函数,而您在任何地方都没有定义它。你知道吗相关问题 更多 >
编程相关推荐