Differentiation - MATLAB & Simulink (2024)

Open Live Script

This examples shows how to find derivatives using Symbolic Math Toolbox™.

First create a symbolic expression.

syms xf = sin(5*x);

Differentiate the expression f with respect to x.

Df = diff(f)
Df =5cos(5x)

As another example, specify another expression that uses exp(x) to represent ex.

g = exp(x)*cos(x);

Differentiate the expression g.

y = diff(g)
y =excos(x)-exsin(x)

To find the derivative of g for a given value of x, substitute x for the value using subs and return a numerical value using vpa. Find the derivative of g at x = 2.

y_eval = vpa(subs(y,x,2))
y_eval =-9.7937820180676088383807818261614

To take the second derivative of g, use diff(g,2).

D2g = diff(g,2)
D2g =-2exsin(x)

You can get the same result by taking the derivative twice.

D2g =-2exsin(x)

In this example, Symbolic Math Toolbox returns a simplified answer. However, in some cases, the answer is not simplified, in which case you can use the simplify command. The next section of this example will discuss the cases of such simplification.

Note that to take the derivative of a constant, you must first define the constant as a symbolic expression.

c = sym("5");Dc = diff(c)
Dc =0

If you use diff directly on a constant number, such as 5, the result is an empty array because the number is not a symbolic object but a double numeric type.

Dc = diff(5)
Dc = []

Derivatives of Expressions with Several Variables

To differentiate an expression that contains more than one symbolic variable, specify the variable that you want to differentiate with respect to. The diff command then calculates the partial derivative of the expression with respect to that variable. For example, specify a symbolic expression with two variables.

syms s tf = sin(s*t);

Find the partial derivative f/t by using diff and specifying the variable to differentiate as t.

Df_t = diff(f,t)
Df_t =scos(st)

To differentiate f with respect to the variable s, specify the variable to differentiate as s.

Df_s = diff(f,s)
Df_s =tcos(st)

If you do not specify a variable to differentiate with respect to, diff uses the default variable. Basically, the default variable is the letter closest to x in the alphabet. See the complete set of rules in Find Symbolic Variables in Expressions, Functions, and Matrices. In the preceding example, diff(f) takes the derivative of f with respect to t because the letter t is closer to x in the alphabet than the letter s is. To determine the default variable that MATLAB differentiates with respect to, use symvar.

fvar = symvar(f,1)
fvar =t

Calculate the second derivative of f with respect to t.

D2f =-s2sin(st)

Note that diff(f,2) returns the same answer because t is the default variable.

More Examples About Derivatives

To further illustrate the diff function on other expressions, define the symbolic variables a, b, x, n, t, and theta.

syms a b x n t theta

This table illustrates the results of using diff on several other expressions.

f

diff(f)

>> syms x n

>> f = x^n;

>> Df = diff(f)

Df =

n*x^(n - 1)

>> syms a b t

>> f = sin(a*t + b);

>> Df = diff(f)

Df =

a*cos(b + a*t)

>> syms theta

>> f = exp(i*theta);

>> Df = diff(f)

Df =

exp(theta*1i)*1i

You can differentiate the Bessel function of the first kind, besselj(nu,z), with respect to z.

syms nu zb = besselj(nu,z);Db = diff(b)
Db =

νJbesseljν(z)z-Jbesseljν+1(z)

The diff function can also take a symbolic matrix as its input. In this case, the differentiation is done element-by-element.

syms a xA = [cos(a*x),sin(a*x);-sin(a*x),cos(a*x)]
A =

(cos(ax)sin(ax)-sin(ax)cos(ax))

Find the derivative of A with respect to x.

DA = diff(A)
DA =

(-asin(ax)acos(ax)-acos(ax)-asin(ax))

You can also perform differentiation of a vector function with respect to a vector argument. Consider the transformation from Cartesian coordinates (x,y,z) to spherical coordinates (r,λ,φ) as given by

x=rcosλcosφ,

y=rcosλsinφ,

z=rsinλ.

Here, λ corresponds to elevation or latitude, while φ denotes azimuth or longitude.

Differentiation- MATLAB & Simulink (1)

To calculate the Jacobian matrix, J, of this transformation, use the jacobian function. The mathematical notation for J is

J=(x,y,z)(r,λ,φ).

For the purposes of computing the Jacobian, use l to represent λ and f to represent φ. Find the Jacobian.

syms r l fx = r*cos(l)*cos(f);y = r*cos(l)*sin(f);z = r*sin(l);J = jacobian([x; y; z], [r l f])
J =

(cos(f)cos(l)-rcos(f)sin(l)-rcos(l)sin(f)cos(l)sin(f)-rsin(f)sin(l)rcos(f)cos(l)sin(l)rcos(l)0)

Find the determinant of this Jacobian and simplify the result.

detJ = simplify(det(J))
detJ =-r2cos(l)

The arguments of the jacobian function can be column or row vectors. Moreover, since the determinant of the Jacobian is a rather complicated trigonometric expression, you can use simplify to make trigonometric substitutions and reductions (simplifications).

A table summarizing diff and jacobian follows.

Mathematical Operator

Command Using Symbolic Math Toolbox

dfdx

diff(f) or diff(f,x)

dfda

diff(f,a)

d2fdb2

diff(f,b,2)

J=(r,t)(u,v)

J = jacobian([r; t],[u; v])

See Also

diff | int | jacobian | gradient | curl | laplacian | functionalDerivative

External Websites

  • Calculus Derivatives (MathWorks Teaching Resources)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Differentiation- MATLAB & Simulink (2)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Differentiation
- MATLAB & Simulink (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5873

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.