我正在尝试为给定的数组生成近似推导。
我已经开发了一个数组,但不知道如何遍历每个值以获得派生
# display the approximation for each delta step in this cell
import numpy as np
delta = (
np.logspace(-1, -14, 14),
np.set_printoptions(formatter=dict(float="{:10.8e}".format)),
)
print(delta)
def my_derivative_approximation(f, x, d=10e-6):
return (f(x + d) - f(x)) / d
# Trying to apply approximation derivation to each delta array value
print(my_derivative_approximation(delta, 14))期待着学习这个概念。
发布于 2019-09-13 15:41:35
我想你误解了近似推导。在您的代码中,您尝试将numpy数组用作函数,但这有点胡说八道。如果你想近似推导,你可以简单地创建一个如下的函数:
def myFunction(x):
return x*2创建此函数后,您可以在增量数组中创建多个delta值:
delta = np.logspace(-1, -14,
14),np.set_printoptions(formatter=dict(float='{:10.8e}'.format))
# This is a tuple and you can obtain numpy array that includes delta values by #delta[0]之后,您可以通过将数组发送到近似函数来迭代您的numpy数组:
# display the approximation for each delta step in this cell
import numpy as np
def myFunction(x):
return x*2
delta = np.logspace(-1, -14,
14),np.set_printoptions(formatter=dict(float='{:10.8e}'.format))
def my_derivative_approximation(f, x, delta):
return (f(x + delta) - f(x)) / delta
#Trying to apply approximation derivation to each delta array value
print(my_derivative_approximation(myFunction,14,delta[0]))https://stackoverflow.com/questions/57918895
复制相似问题