【发布时间】:2020-06-18 08:49:38
【问题描述】:
import math
import decimal
n=5
s=3.5*n -14
def float_range(start, stop, step):
while start < stop:
yield float(start)
start += decimal.Decimal(step)
initial=list(float_range(-14, 15, s))
def diff(f, x):
dx = 1e-6
r1 = f(x + dx) - f(x)
f_x = r1 / dx
return float(f_x)
def sinc(x):
if x==0 :
return
else :
return math.sin(x)/x
def Dsinc(x):
Dsinc=diff(sinc,x)
return Dsinc
def nraphson(f, Df, x, dx) :
x1= x - f(x)/Df(x)
while abs(x1-x) < dx:
x=x1
print(x1)
for x in initial :
print(x, nraphson(sinc,Dsinc,x,1e-6))
为学校项目编写的代码。不能使用 Numpy,所有微分和牛顿方法都必须编码,而不是使用 numpy 函数。
有谁知道我为什么会得到:
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' 在第 16 行
r1 = f(x + dx) - f(x)
谢谢
【问题讨论】:
-
在
sync中有if x == 0: return,这意味着当x是0时,返回值将是None,这似乎是在您的程序中发生的。跨度>
标签: python python-3.x typeerror