Road to AI

Derivative Function with Multiple Inputs

Download notebook (.ipynb)
# Now we get slighly more complex from the previous one

# We are adding 3 scalar inputs
a = 2.0
b = -3.0
c = 10.0

#Single ouput
d = a*b + c
print(d)
Output
4.0

Now we are gonna look at the derivative of d wrt to a,b & c

# We'll set the value of h
h = 0.0001
#inputs
a = 2.0
b = -3.0
c = 10.0

d1 = a*b + c

(i) wrt a

a += h

d2 = a*b + c
print('d1', d1)
print('d2', d2)

# Here (d2 - d1) is the amount of bump that was actually increased, after we had bump the value of the specific value that we are interested in by a tiny amount
# And that is normalised by dividing that value by h, to get the slope
print('Slope: ', (d2 - d1)/h)
Output
d1 4.0
d2 3.999699999999999
Slope:  -3.000000000010772

Now here with the ouput d2, it becomes slightly less than 4 after we had added a small value to a

As the negative value formed after the product is bigger

Now, since we know the overall value had reduced, the sign of the slope should also naturally be negative (as seen)

Even Mathematically, if we take the derivative of the expression d2 wrt a
then only b is left
which is a negative value, hence the negative sign

(ii) wrt b

b += h

d2 = a*b + c

print('d1', d1)
print('d2', d2)

print('Slope: ', (d2 - d1)/h)
Output
d1 4.0
d2 4.0002
Slope:  2.0000000000042206

Similar explanation here

the value of b becomes slightly less after adding the small increament
Therefore increasing the value of the final expression d2

Here, since its the derivative of d2 wrt b
only a remains, therefore a positive slope value

(iii) wrt c

c += h

d2 = a*b + c

print('d1', d1)
print('d2', d2)

print('Slope: ', (d2 - d1)/h)
Output
d1 4.0
d2 4.0001
Slope:  0.9999999999976694

Here, we add a slightly bit higher value to c

For this derivative, a and b are not considered.
The function or d2 becomes slightly bit higher because of c
It becomes slightly bit higher by the exact amount that was added to c

Therefore cancelling out and the value of the slope is hence 1

Therefore, this will be the derivative at which 'd' will increase, as we scale 'c'