Product Rule#
Suppose we want to differentiate \(y=x^2\sin(x)\), then we encounter a problem: How to differentiate a function that is the product of two other functions, in this case \(x^2\) and \(\sin(x)\).
To do this, we need the product rule:
If the function \(y = f(x)\) is written as the product of two functions, say \(u(x)\) and \(v(x)\) so
\(y = f(x) = u(x)v(x)\) then \(\frac{dy}{dx} = u\frac{dv}{dx} + v\frac{du}{dx}\).
So returning to our example, we have \(f(x) = x^2\sin(x)\) with \(u=x^2\) and \(v = \sin(x)\).
First, we find that \(\frac{du}{dx} = 2x\) and \(\frac{dv}{dx} = \cos(x)\), then we substitute into \(\frac{dy}{dx} = u\frac{dv}{dx} + v\frac{du}{dx}\).
Example
Differentiate using the product rule:
\(y = x\ln(x)\)
\(y = 6e^x \cos(x)\)
Solution:
Take \(u = x\) and \(v = \ln(x)\). We can now differentiate these to find that \(\frac{du}{dx} = 1\) and that \(\frac{dv}{dx} = \frac{1}{x}\).
Using the product rule:
Substitue in the quantities:
Expand and simplify:
For this, take \(u=6e^x\) and \(v=\cos(x)\). Now we differentiate those to find that \(\frac{du}{x} = 6e^x\) and \(\frac{dv}{dx} = -\sin(x)\).
By the product rule, we have:
Substitute in the quantities:
Expand the brackets:
Take a factor of \(6e^x\) out:
These can both be performed with sympy
.
from sympy import symbols, diff, log, cos, exp
x = symbols('x')
diff(x * log(x))
diff(6 * exp(x) * cos(x))
Example
Differentiate \(y = \frac{1}{3}(x^2 + 5x) \sin(x)\).
Solution: This is a product of two functions and we must use the product rule.
Set \(u=\frac{1}{3}(x^2 + 5x)\) and \(v = \sin(x)\).
We can differentiate to find that \(\frac{du}{dx} = \frac{1}{3}(2x+5)\) and \(\frac{dv}{dx} = \cos(x)\).
We now use the product rule:
Substitute in what we’ve worked out:
Simplify by taking a factor of \(\frac{1}{3}\) out
The end result looks complicated and ugly, but it is nicer than having all the brackets expanded.
We can check the result with sympy
.
from sympy import sin
diff((1 / 3) * (x ** 2 + 5 * x) * sin(x))
Again this is quite ugly but we can see that the answer is correct.