簡體   English   中英

如何從給定范圍獲得導數逼近?

[英]How to get the derivative approximation from given range?

我正在嘗試為給定數組生成近似導數。

我已經開發了一個數組,但是不知道如何遍歷每個值來求導數

# 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))

期待學習這個概念。

我認為您誤解了近似推導。 在您的代碼中,您嘗試將numpy數組用作函數,但這有點廢話。 如果要近似推導,可以簡單地創建一個像這樣的函數:

def myFunction(x):
    return x*2

創建此函數后,可以在delta數組中創建許多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]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM