There are two ways this can be done:
Using symbolic maths, which are basically like encoding an algebraic expression and then letting the computer figure out an analytical expression of the derivative; these can be quite powerful but are restricted to certain types of problems:
https://www.sympy.org/en/index.html
Using finite differencing. This is a very common and powerful concept founded on the idea of using the definition of the derivative with delta_x being small and finite instead of tending towards zero:
https://en.wikipedia.org/wiki/Finite_difference
For your particular problem, this could be done, for instance:
import numpy as np
import matplotlib.pyplot as plt
def func(x):
return x**2
def finiteDifference(f, limits, deltaX):
# create values of the function at specific locations
x = np.arange(limits[0], limits[1]+deltaX, deltaX)
y = np.zeros(len(x))
for i in range(len(x)):
y[i] = f(x[i])
# construct the derivative
dydx = np.zeros(len(x))
# first order at the ends
dydx[0] = (y[1]-y[0]) / deltaX
dydx[-1] = (y[-1]-y[-2]) / deltaX
# central 2nd order elsewhere
for i in range(1, len(x)-1):
dydx[i] = (y[i+1] - y[i-1]) / (2.*deltaX)
return x, y, dydx
x, y, dydx = finiteDifference(func, (-2, 2), 0.1)
plt.plot(x, y, "r-", x, dydx, "b-", lw=2)
# compare with analytical f'=2x
plt.plot(x, 2.*x, "gx", ms=5, mew=2)
plt.show()