Matlab

How to Take Partial Derivative of a Function in MATLAB?

MATLAB is a useful programming language mostly used by mathematicians and engineers for simulation and solving complex mathematical problems. It solves matrices and can solve polynomials, trigonometric functions, and many other algebraic problems. One among these mathematical problems is finding the partial derivative of the multivariate function.

This blog will teach us how to find the function’s partial derivatives in MATLAB.

How to Find the Function’s Partial Derivative in MATLAB?

We can calculate the function’s partial derivatives in MATLAB using the built-in gradient() function. This function is used for calculating the partial derivative of the multivariate function (multiple inputs and single output) with respect to the specified variable. This function accepts the multivariate function as an input and returns its calculated partial derivative with respect to the given variable.

Syntax

The gradient() follows a simple syntax that is given below:

gradient(F, x)

gradient(F, [x, y])

gradient(F, [x1, x2, x3,…,xN])

Here:

The function gradient(F, x) calculates the given function F’s one-dimensional partial derivative with respect to x.

The function gradient(F, [x, y]) computes the given function F’s partial derivatives with respect to x and y.

The function gradient(F, [x1, x2, x3,…,xN) finds the n-dimensional partial derivative of the function F with respect to the N components.

Examples

Follow the given examples to demonstrate the working of MATLAB’s gradient() function to compute the partial derivatives of the given function F.

Example 1: How to Calculate the Partial Derivatives of the Function with Single Input Using the Function gradient(F, x)?

In this example, we calculate the given function F’s partial derivatives with respect to x using the gradient() function.

syms x y;

F = sin(x) + cos(y);

gradient(F, x)

Example 2: How to Calculate the Partial Derivatives of the Function with Two Inputs Using the Function gradient(F, [x, y])?

The given MATLAB code computes the function F’s partial derivatives with respect to x and y using the gradient() function.

syms x y;

F = sin(x) + cos(y);

gradient(F,[x,y])

Example 3: How to Calculate the Partial Derivatives of the Function with Three Inputs Using the Function gradient(F, [x, y, z])?

This example computes the given function F’s partial derivatives with respect to x, y, and z using the gradient() function.

syms x y z;

F = sin(x) + cos(y)- exp(z);

gradient(F,[x,y, z])

Conclusion

MATLAB is a helpful programming tool that allows us to solve mathematical problems using built-in functions. We can take the partial derivative of the multivariate function in MATLAB using the built-in gradient() function with respect to one, two, or N variables. This guide has presented an easy guide with simple examples to take the partial derivative of a function in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.