Derivatives, along with integrals, are one of the cornerstone topics in Calculus.
The Wolfram language has a surprisingly powerful system for differentiation.
To compute the derivative of f(x),
you use the "D" function.
The first argument is the function,
and the second is the variable.
The output is the derivative.
If you want to compute the derivative at a specific point,
say x=1.5,
then you can use the ReplaceAll shortcut and replace x with 1.5.
To compute the second derivative of a function,
you pass in a list as the second argument
with the variable and the number of times to differentiate.
In this case, 2.
Similarly, for the third derivative,
you pass in the function, then a list containing x and 3.
Mathematica supports a wide variety of ways to enter your derivative command.
You can, of course, use the D function like we did previously.
But you can also use the "prime" notation for a derivative.
When you use the "prime" notation,
you can use whatever you like as your variable.
You can use the "prime" notation for higher order derivatives, too.
By the way, the Together function is helpful for combining terms into a single fraction.
The Wolfram Language can compute partial derivatives, too.
To compute the second order partial derivative of g with respect to y then x,
you use the D function once more.
This time, you pass in 3 arguments:
first the function,
then y,
and then x,
The "D" function will take the derivative with respect to the variables in the order you provide them.
Notice we did not put y and x together inside a list.
The reason is that the Wolfram Language is carefully crafted to be consistent and intuitive.
Earlier we used a list to specify the variable and the order of the derivative.
This still applies.
So if you want to compute this third order partial derivative,
you again use the D function with the function,
a list with y and the order 2,
and lastly a list with x and the order 1.
When the order is 1, you don't need to specify it in a list,
so here's another way to ask for the same derivative.
You can also do away with lists entirely
and ask for the partial derivative with respect to y
then y,
then x.
These are 3 different ways to describe the same computation.
Mathematica can also perform sophisticated symbolic differentiation.
Let me illustrate.
Suppose f is sin(πx).
What if we want to take the n-th derivative.
Before we execute this command,
keep in mind Mathematica does not know the value of n.
Since Mathematica assumes that n is a non-negative integer,
it's able to compute an expression for this derivative.
And look what you get if you ask for the n-th derivative of x^2.
The returned value involves a function that may be new to you.
To show that this expression is indeed correct,
let's build a table of the first 10 derivatives.
As expected,
the first derivative is 2x,
the second derivative is 2,
and all higher order derivatives are 0.
Be sure to consult the excellent Wolfram documentation for more details on the FactorialPower function.
To highlight how deep the derivative function goes,
let's ask Mathematica to compute the n-th derivative of h(x),
a function we've not even defined.
The returned expression is the proper symbolic representation for the n-th derivative of a function.
If you were to define h(x) at a later time,
you could revisit the output to see the updated value.
To illustrate how the Wolfram Language uses all the standard rules of differentiation,
let's first clear any previous definitions of F and G.
The derivative of a sum of two functions is the sum of the derivatives.
Mathematica computes the derivative of a product with the product formula.
For the quotient of two functions, it uses the quotient rule.
This computation is more generally displayed as a single fraction.
And for function composition, the Wolfram Language uses the Chain Rule.
So all common differentiation rules are available.
The Wolfram Language differentiation algorithm is extremely powerful.
As you might expect,
being able to take the fifth derivative of tangent(3x)
is a straightforward computation.
But look at the result when you want to compute the n-th derivative of tangent(ax).
Mathematica is able to return an answer,
even though the expression involves complex numbers,
the Kronecker delta,
and a sum involving Stirling numbers of the second kind.
Next,
look what is returned if we compute the derivative of the absolute value function.
The reason for this output is that the function is non-differentiable at x=0.
To handle functions that are piecewise differentiable,
you can use the PiecewiseExpand function.
As the name suggests, it expands a function into a piecewise function.
The Wolfram Language can now properly differentiate this piecewise function.
Our examples today focused on derivatives of functions of a single variable
and partial derivatives.
The Wolfram Language supports many other ways to measure rates of change.
You can find the derivative using implicit differentiation.
You can compute the gradient...
The Laplacian...
Total derivatives...
Even fractional derivatives.
And never forget that the Wolfram Help is just a click away
whenever you encounter something unfamiliar.
If you enjoy our videos on Mathematica and the Wolfram Language,
you might want to join our Mathematica mailing list.
This way you'll be notified when we release new lessons,
sample notebooks,
and our upcoming professional course.