Derivative Simple Function
In [1]:
Copied!
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
Copied!
# Now define a function, a scaler value function f(x)
def f(x):
return 3*x**2 - 4*x +5
# Now define a function, a scaler value function f(x)
def f(x):
return 3*x**2 - 4*x +5
In [4]:
Copied!
# Now we can just pass in some value to check
f(3.0)
# Now we can just pass in some value to check
f(3.0)
Out[4]:
20.0
In [7]:
Copied!
# 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
# 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
Out[7]:
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])
In [8]:
Copied!
# 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
# 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
Out[8]:
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])
In [9]:
Copied!
# And now we plot this using matplotlib
plt.plot(xs, ys)
# And now we plot this using matplotlib
plt.plot(xs, ys)
Out[9]:
[<matplotlib.lines.Line2D at 0x2274508baf0>]
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)
Now, we can check the derivative of the value by considering a very small value of h (almost close to zero)
In [10]:
Copied!
h = 0.001
x = 3.0
h = 0.001
x = 3.0
In [12]:
Copied!
# 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)
# 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)
Out[12]:
20.014003000000002
In [14]:
Copied!
# 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)
# 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)
Out[14]:
0.01400300000000243
In [15]:
Copied!
# 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
# 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
Out[15]:
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
In [18]:
Copied!
# 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
# 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
Out[18]:
-22.00000039920269
In [19]:
Copied!
# 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
# 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
Out[19]:
0.0