Differentiation - MATLAB & Simulink (2024)

Differentiation

To illustrate how to take derivatives using Symbolic Math Toolbox™ software, first create a symbolic expression:

syms xf = sin(5*x);

The command

diff(f)

differentiates f with respect to x:

ans =5*cos(5*x)

As another example, let

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

where exp(x) denotes ex, and differentiate g:

y = diff(g)
y =exp(x)*cos(x) - exp(x)*sin(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.

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

To take the second derivative of g, enter

diff(g,2)
ans =-2*exp(x)*sin(x)

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

diff(diff(g))

In this example, MATLAB® software automatically simplifies the answer. However, in some cases, MATLAB might not simplify an answer, in which case you can use the simplify command. For an example of such simplification, see More Examples.

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

c = sym('5');diff(c)

returns

ans =0

If you just enter

diff(5)

MATLAB returns

ans = []

because 5 is not a symbolic expression.

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, given the symbolic expression

syms s tf = sin(s*t);

the command

diff(f,t)

calculates the partial derivative f/t. The result is

ans = s*cos(s*t)

To differentiate f with respect to the variable s, enter

diff(f,s)

which returns:

ans =t*cos(s*t)

If you do not specify a variable to differentiate with respect to, MATLAB chooses a default variable. Basically, the default variable is the letter closest to x in the alphabet. See the complete set of rules in Find a Default Symbolic Variable. 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:

symvar(f,1)

Calculate the second derivative of f with respect to t:

diff(f,t,2)

This command returns

ans =-s^2*sin(s*t)

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

More Examples

To further illustrate the diff command, define a, b, x, n, t, and theta in the MATLAB workspace by entering

syms a b x n t theta

This table illustrates the results of entering diff(f).

f

diff(f)

syms x nf = x^n;
diff(f)
ans =n*x^(n - 1)
syms a b tf = sin(a*t + b);
diff(f)
ans =a*cos(b + a*t)
syms thetaf = exp(i*theta);
diff(f)
ans =exp(theta*1i)*1i

To differentiate the Bessel function of the first kind, besselj(nu,z), with respect to z, type

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

which returns

db =(nu*besselj(nu,z))/z - besselj(nu + 1,z)

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

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

which returns

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

The command

diff(A)

returns

ans =[ -a*sin(a*x), a*cos(a*x)][ -a*cos(a*x), -a*sin(a*x)]

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ϕ, and z=rsinλ. Note that λ 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 toolbox syntax, use l for λ and f for φ. The commands

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])

return the Jacobian

J =[ cos(f)*cos(l), -r*cos(f)*sin(l), -r*cos(l)*sin(f)][ cos(l)*sin(f), -r*sin(f)*sin(l), r*cos(f)*cos(l)][ sin(l), r*cos(l), 0]

and the command

detJ = simplify(det(J))

returns

detJ = -r^2*cos(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

MATLAB Command

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)

FAQs

How to differentiate with Matlab? ›

Df = diff( f , var ) differentiates f with respect to the differentiation parameter var . var can be a symbolic scalar variable, such as x , a symbolic function, such as f(x) , or a derivative function, such as diff(f(t),t) . Df = diff( f , var , n ) computes the n th derivative of f with respect to var .

How to write dy dx in Matlab? ›

In common, the differential operation is defined as "dy/dx" which means differentiate y with respect to x and in matlab it's defined by "diff()".

What is the difference function in Matlab? ›

Description. Y = diff( X ) calculates differences between adjacent elements of X along the first array dimension whose size does not equal 1: If X is a vector of length m , then Y = diff(X) returns a vector of length m-1 . The elements of Y are the differences between adjacent elements of X .

Does MATLAB have automatic differentiation? ›

To use automatic differentiation, you must call dlgradient inside a function and evaluate the function using dlfeval . Represent the point where you take a derivative as a dlarray object, which manages the data structures and enables tracing of evaluation.

Can MATLAB compute derivative? ›

For differentiation, you can differentiate an array of data using gradient , which uses a finite difference formula to calculate numerical derivatives. To calculate derivatives of functional expressions, you must use Symbolic Math Toolbox™.

How to solve a differential equation analytically in MATLAB? ›

Use diff and == to represent differential equations. For example, diff(y,x) == y represents the equation dy/dx = y. Solve a system of differential equations by specifying eqn as a vector of those equations. S = dsolve( eqn , cond ) solves eqn with the initial or boundary condition cond .

How to find derivative at a point in MATLAB? ›

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 . In this example, MATLAB® software automatically simplifies the answer.

What is the difference between MATLAB function and Simulink function? ›

Faster than MATLAB Function because code is not generated to update the diagram. Since, code generation is incremental, Simulink does not repeatedly update the block if the block and the signals connected to it have not changed.

What is the numerical diff in MATLAB? ›

MATLAB has special command diff that takes a list of numbers and calculates the difference between each adjacent number. For example: y = [1,3,8]; dy = diff(y) → [2,5]. When using yp=diff(y)./diff(x) it is necessary to map it to an associated x value.

How do you find the difference equation in MATLAB? ›

You can use the Z-transform to solve difference equations, such as the well-known "Rabbit Growth" problem. If a pair of rabbits matures in one year, and then produces another pair of rabbits every year, the rabbit population p ( n ) at year n is described by this difference equation.

How to differentiate signal in MATLAB? ›

You want to differentiate a signal without increasing the noise power. MATLAB®'s function diff amplifies the noise, and the resulting inaccuracy worsens for higher derivatives. To fix this problem, use a differentiator filter instead.

How do you compare two things in MATLAB? ›

Use the strcmp function to compare two character vectors, or strncmp to compare the first N characters. You also can use strcmpi and strncmpi for case-insensitive comparisons. Compare two character vectors with the strcmp function. chr1 and chr2 are not equal.

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5499

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.