General rule:

f(x)g(x) = f'(x)g(x) + f(x)g'(x)

So if you have tow functions, first take derivative of the first multiplied by the second function then add the function of the first times the derivative of the second.

Just like in your file is done.