【问题标题】:find the domain of a function, given the root of it给定函数的根,找到函数的域
【发布时间】: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


    【解决方案1】:

    你需要找到 g(x) = f(x)-y 的零点:

    def source(f,y):
        def g(x):
             return f(x)-y
        x = NR(g, diff_param(g))
        return x
    

    这会返回一个x,但可能还有其他的。要找到它们,您需要尝试其他初始值x0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多