我在第 4 行使用了 np.Zeros 并且它没有给出错误为什么它在第 17 行给出错误 AttributeError: 'module' object has no attribute 'Zeros' 而在第 27 行它给出错误.. 错误y= newtonRaphson(残差,startSoln(x),1.0e-5)
import numpy as np
from newtonRaphson import *
def residual(y):
r=np.Zeros((m+1))
r[0]=y[0]
r[m]=y[m]-1.0
for i in range(1,m):
r[i]=y[i-1]
return r
def F(x,y,yPrime):
F=-3.0*y*yPrime
return F
def startSoln(x):
y=np.Zeros((m+1))
for i in range(m+1):
y[i]=0.5*x[i]
return y
xStart=0.0
xStop=2.0
m=10
h=(xStop-xStart)/m
x=np.arange(xStart,xStop+h,h)
y= newtonRaphson(residual,startSoln(x),1.0e-5)
print '\n x y'
for i in range(m+1):
print x[i],' '.y[i]
residual(0.0005)
这是 newtonRaphson 模块
def newtonRaphson(f,df,a,d,tol=1.09e-9):
import error
fa= f(a)
if fa == 0.0 :return a
fb = f(b)
if fb ==0.0: return b
if fa*fb >0.0: error.err
x= 0.5*(a+b)
for i in range(30):
if abs(fx)<tol :return x
if fa*fx <0.0:
b=x
else:
a=x ;fa=fx
dfx=df(X)
try:dx= -fx/dfx
except ZeroDivisionError: dx=b-a
x=x+dx
if (b-x)*(x-a)<0.0:
dx=0.5*(b-a)
x=a+dx
if abs(dx)< tol*max(abs(b),1.0): return x
print 'Too many iterations in Newton-Raphson'
最佳答案
是因为the function is called zeros
,而不是 Zeros
。将 np.Zeros
更改为 np.zeros
。
关于python - 属性错误 : 'module' object has no attribute 'Zeros' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261763/