The essence of calculus is the derivative. The derivative is the instantaneous rate of change of a function with respect to one of its variables. This is equivalent to finding the slope of the tangent line to the function at a point.

You already have learned some methods to calculate derivatives of functions and MATLAB can assist you to confirm your results or analyse the behaivour of the derivative graphically.

In this activity we will use the commands syms and diff to calculate partial derivatives symbolically.

Use the MATLAB Live Editor to edit and run this Live Script in your browser or your desktop.

- Read each section carefully.
- Run the code within a section before starting to read the next.
- To run the code from each section, position the cursor on the code with the mouse and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:

Remark: Run the code of each section from top to bottom, otherwise you may get an error.

- The end of a section is indicated with a thin line, like the next one -

To illustrate how to calculate derivatives of one variable functions in MATLAB, first we create a symbolic expression:

syms x % Define the symbolic variable x

f = 1/(1 + x^2); % Define the symbolic expression, i.e. f(x)

The command

diff(f)

differentiates "f" with respect to "x". Run this section to see the result.

If we wish to compute the partial derivatives of a multivariate function we must specify the variable. That is, diff(f, var). For example, consider the function:

In order to compute and we write the code:

syms x y

f = (x + y) * cos(x - y);

partialX = diff(f, x)

partialY = diff(f, y)

Run this section to see the output. Compute the partial derivatives in your notebook to confirm the results.

In MATLAB we can also easily compute second-order derivatives of multivariate functions with respect to a particular variable.

For example, consider the function

In order to compute the second order partial derivatives

, , and ,

we write the code:

syms x y

f = (x + 2*y)^3;

secPartialX = diff(f, x, 2)

secPartialY = diff(f, y, 2)

secPartialXY = diff(f, x, y)

secPartialYX = diff(f, y, x)

Note 1: The code diff(f,x,2) is equivalent to diff(f,x,x). You can also compute higher-order derivatives. To learn more about it see: Higher-order derivatives.

Compute all the second partial derivatives of the following functions:

Write your code here:

% Part 1:

% Part 2:

% Part 3:

% Part 4:

The van der Waals equation for n moles of a gas is

where P is the pressure, V is the volume, and T is the temperature of the gas. The constant R is the universal gas constant and a and b are positive constants that are characteristic of a particular gas. Calculate

and .

Write your code here:

% Partial of T with respect of P

% Partial of P with respect of V