我已經在第4行使用了np.Zeros,並且它沒有給出錯誤,爲什麼它在第17行給出錯誤AttributeError:'module'object has no attribute'Zeros'並在第27行它給誤差..誤差在Y = newtonRaphson(殘留,startSoln(X),1.0E-5)AttributeError:'模塊'對象沒有屬性'零'
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'
在newtonRaphson中有'np'嗎? –
@BurhanKhalid我進口numpy的爲NP代碼 – user3036166
@BurhanKhalid修正Z到Z後。現在的誤差僅在27行,這是newtonRaphson需要ATLEAST 4場參數和我只給3 .. – user3036166