Googlebot
Googlebot

Reputation: 15673

Calculating derivative by SciPy

Consider this python script

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,1,100)

def y(x,z):
    return x**2 + z**-1 - 1

for z in np.arange(1,50,1):
    plt.plot(x, y(x,z))

At a fixed x, how can I draw

plt.plot(z, y(x,z))
plt.plot(z, y(x,z).derivative) #dy/dz

Upvotes: 0

Views: 135

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

You must use the derivative function:

import numpy as np
import matplotlib.pyplot as plt

from scipy.misc import derivative

x = np.linspace(0,1,100)

def y(z, x):
    return x**2 + z**(-1) - 1

z = np.arange(1.0, 50.0)

dydz = [derivative(y, zi, args=(x, )) for zi in z]
plt.plot(z, dydz)
plt.show()

Screenshot:

enter image description here

Upvotes: 1

Related Questions