Mayur Potdar
Mayur Potdar

Reputation: 451

How to get the derivative approximation from given range?

I'm trying to generate the approximation derivation for a given array.

I have developed an array but don't know how to loop over each value to get derivation

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

Looking forward to learning this concept.

Upvotes: 0

Views: 121

Answers (1)

mustafamuratcoskun
mustafamuratcoskun

Reputation: 36

I think you misunderstood the approximation derivation. In your code, you try to use your numpy array as a function but it is a little bit nonsense. If you want to approximate derivation, you can simply create a function like this :

def myFunction(x):
    return x*2

After creating this function, you can create many delta values in delta array :

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]

After that, you can iterate over your numpy array by sending your array to a approximation function:

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

Upvotes: 1

Related Questions