Road to AI

Derivative Simple Function

Download notebook (.ipynb)
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Now define a function, a scaler value function f(x)
def f(x):
    return 3*x**2 - 4*x +5
# Now we can just pass in some value to check
f(3.0)
Output
20.0
# The f(x) equation as you can see is a Quadratic equation, precisely a Parabola
# So now, we try to plot it

# Now, we'll just add like a range of values to feed in

# We'll start with x-axis values so, from -5 to 5 (Not including 5) in the steps of 0.25
# Therefore creating a numpy array
xs = np.arange(-5,5,0.25)
xs
Output
array([-5.  , -4.75, -4.5 , -4.25, -4.  , -3.75, -3.5 , -3.25, -3.  ,
       -2.75, -2.5 , -2.25, -2.  , -1.75, -1.5 , -1.25, -1.  , -0.75,
       -0.5 , -0.25,  0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,
        1.75,  2.  ,  2.25,  2.5 ,  2.75,  3.  ,  3.25,  3.5 ,  3.75,
        4.  ,  4.25,  4.5 ,  4.75])
# Now for the y-axis values, we call each of those elements in the numpy array to the function f(x)

# Therefore we create an another numpy array which containes the values after applying the function to each of the elements in xs
ys = f(xs)
ys
Output
array([100.    ,  91.6875,  83.75  ,  76.1875,  69.    ,  62.1875,
        55.75  ,  49.6875,  44.    ,  38.6875,  33.75  ,  29.1875,
        25.    ,  21.1875,  17.75  ,  14.6875,  12.    ,   9.6875,
         7.75  ,   6.1875,   5.    ,   4.1875,   3.75  ,   3.6875,
         4.    ,   4.6875,   5.75  ,   7.1875,   9.    ,  11.1875,
        13.75  ,  16.6875,  20.    ,  23.6875,  27.75  ,  32.1875,
        37.    ,  42.1875,  47.75  ,  53.6875])
# And now we plot this using matplotlib
plt.plot(xs, ys)
Output
[<matplotlib.lines.Line2D at 0x2274508baf0>]

Cell 5 output

Now we need to see what is the derivative of this function f(x) at any single input point x

So what is the derivative at different point in the x-axis to the function f(x)

image.png

Now, we can check the derivative of the value by considering a very small value of h (almost close to zero)

h = 0.001
x = 3.0
# Now, if we take the f(x) value directly, we know we get 20.0 (Already done in above cells)

# Lets say what if we add the value of h to it, so we are nudging it to a slighly more positive direction
# So the value must be slightly more than 20

f(x+h)
Output
20.014003000000002
# Now, by how much that above value has increased shows us the strength or the size of that slope

# Therefore, Next we see how much the function has responded

f(x+h) - f(x)
Output
0.01400300000000243
# Next we have to normalise it by adding the value rise of the run i.e. h, to get the value of the slope

(f(x+h) - f(x))/h
Output
14.00300000000243

Therefore, at 3 i.e. when x=3, the slope is 14.

You can see the same value if you calculate it manually using the derivative formula:
=> Derivative of 3x^2 - 4x +5
=> 6x-4
=> 6(3) - 4
=> 18 -4
=> 14

# Now what if we add a negative value for x?
# Then even the function will become negative. Therefore, we will be getting a negative sign slope

h = 0.00000001  # Cant make this too small, as unlike in theory, computer can handle only a finite amount. Therefore make it too small and it will directly return 0 :)
x = -3.0
(f(x+h) - f(x))/h
Output
-22.00000039920269
# Now at some point the slop must be 0, therefore nudging a small value either way from that point, it still remains 0
# In this case, for the function it is at around x = 2/3

h = 0.00000001
x = 2/3
(f(x+h) - f(x))/h
Output
0.0