【发布时间】:2019-01-09 20:17:36
【问题描述】:
我正在尝试使用牛顿法用 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。
【问题讨论】:
-
不确定这是如何工作的,但您目前只是在返回东西,从未真正在返回的函数之一上调用 print()。这就是为什么。 print(f(x)) 会给你那个值。
-
您似乎想以相同的方式调用
plot()1000 次
标签: python python-3.x jupyter-notebook