【发布时间】:2015-01-28 03:26:39
【问题描述】:
我需要帮助编写一个接收函数的方法,以及一些数字 y 并返回 x 这样f(x) = y。该函数可使用牛顿法微分:
from random import *
def diff_param(f,h=0.001):
return (lambda x: (f(x+h)-f(x))/h)
def NR(func, deriv, epsilon=10**(-8), n=100, x0=None):
""" returns a number such that f(number) == 0"""
if x0 is None:
x0 = uniform(-100.,100.)
x=x0; y=func(x)
for i in range(n):
if abs(y)<epsilon:
#print (x,y,"convergence in",i, "iterations")
return x
elif abs(deriv(x))<epsilon:
#print ("zero derivative, x0=",x0," i=",i, " xi=", x)
return None
else:
#print(x,y)
x = x- func(x)/deriv(x)
y = func(x)
#print("no convergence, x0=",x0," i=",i, " xi=", x)
return None
我需要编写一个方法source(f,y),它返回x,这样f(x) = y。
def source(f,y):
【问题讨论】:
标签: python math newtons-method