• Syntax
  • Description
  • Examples
  • Input Arguments
  • Limitations
  • Tips
  • Version History
  • See Also
  • diff

    Differentiate symbolic expression or function

    collapse all in page

    Description

    example

    Df = diff( f ) differentiates f with respect to the symbolic scalar variable determined by symvar(f,1) .

    example

    Df = diff( f , n ) computes the n th derivative of f with respect to the symbolic scalar variable determined by symvar .

    example

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

    example

    Df = diff( f , var , n ) computes the n th derivative of f with respect to var .

    example

    Df = diff( f , var1,...,varN ) differentiates f with respect to the parameters var1,...,varN .

    example

    Df = diff( f , mvar ) differentiates f with respect to the symbolic matrix variable or symbolic matrix function.

    Examples

    collapse all

    Find the derivative of the function f(x) = sin(x^2) .

    syms f(x)
    f(x) = sin(x^2);
    Df = diff(f,x)
    Df(x) = 2xcos(x2)

    Find the value of the derivative at x = 2 . Convert the value to double .

    Df2 = Df(2)
    Df2 = 4cos(4)
    double(Df2)
    ans = -2.6146
    

    Find the first derivative of the expression sin(x*t^2) .

    syms x t
    Df = diff(sin(x*t^2))
    Df = t2cos(t2x)

    Because you did not specify the differentiation variable, diff uses the default variable determined by symvar . For this expression, the default variable is x .

    var = symvar(sin(x*t^2),1)
    var = x

    Now, find the derivative of this expression with respect to the variable t .

    Df = diff(sin(x*t^2),t)
    Df = 2txcos(t2x)

    Find the fourth, fifth, and sixth derivatives of t 6 .

    syms t
    D4 = diff(t^6,4)
    D4 = 360t2
    D5 = diff(t^6,5)
    D5 = 720t
    D6 = diff(t^6,6)
    D6 = 720

    Find the second derivative of the expression x*cos(x*y) with respect to the variable y .

    syms x y
    Df = diff(x*cos(x*y),y,2)
    Df = -x3cos(xy)

    Find the second derivative of the expression x*y . If you do not specify the differentiation variable, diff uses the variable determined by symvar . Because symvar(x*y,1) returns x , diff computes the second derivative of x*y with respect to x .

    syms x y
    Df = diff(x*y,2)
    Df = 0

    If you use nested diff calls and do not specify the differentiation variable, diff determines the differentiation variable for each call. For example, find the second derivative of the expression x*y by calling the diff function twice.

    Df = diff(diff(x*y))
    Df = 1

    In the first call, diff differentiates x*y with respect to x and returns y . In the second call, diff differentiates y with respect to y and returns 1 .

    So, diff(x*y,2) is equivalent to diff(x*y,x,x) , and diff(diff(x*y)) is equivalent to diff(x*y,x,y) .

    Differentiate the expression x*sin(x*y) with respect to the variables x and y .

    syms x y
    Df = diff(x*sin(x*y),x,y)
    Df = 2xcos(xy)-x2ysin(xy)

    You also can compute mixed higher-order derivatives by specifying all differentiation variables. Find the mixed fourth derivative of the expression with respect to the variables x , x , x , and then y .

    syms x y
    Df = diff(x*sin(x*y),x,x,x,y)
    Df = x2y3sin(xy)-6xy2cos(xy)-6ysin(xy)

    Find the derivative of the function y = f ( x ) 2 d f d x with respect to f ( x ) .

    syms f(x) y
    y = f(x)^2*diff(f(x),x);
    Dy = diff(y,f(x))
    Dy = 
    

    2f(x)x f(x)

    Find the second derivative of the function y = f ( x ) 2 d f d x with respect to f ( x ) .

    Dy2 = diff(y,f(x),2)
    Dy2 = 
    

    2x f(x)

    Find the mixed derivative of the function y = f ( x ) 2 d f d x with respect to f ( x ) and d f d x .

    Dy3 = diff(y,f(x),diff(f(x)))
    Dy3 = 2f(x)

    Find the Euler–Lagrange equation that describes the motion of a mass-spring system. Define the kinetic and potential energy of the system.

    syms x(t) m k
    T = m/2*diff(x(t),t)^2;
    V = k/2*x(t)^2;

    Define the Lagrangian.

    L = T - V
    L = 
    

    mt x(t)22-kx(t)22

    The Euler–Lagrange equation is given by

    0 = d d t L ( t , x , x ˙ ) x ˙ - L ( t , x , x ˙ ) x .

    Evaluate the term L / x ˙ .

    D1 = diff(L,diff(x(t),t))
    D1 = 
    

    mt x(t)

    Evaluate the second term L / x .

    D2 = diff(L,x)
    D2(t) = -kx(t)

    Find the Euler–Lagrange equation of motion of the mass-spring system.

    diff(D1,t) - D2 == 0
    ans(t) = 
    

    m2t2 x(t)+kx(t)=0

    Since R2021a

    To evaluate derivatives with respect to vectors, you can use symbolic matrix variables. For example, find the derivatives α / x and α / y for the expression α = y T A x , where y is a 3-by-1 vector, A is a 3-by-4 matrix, and x is a 4-by-1 vector.

    Create three symbolic matrix variables x , y , and A , of the appropriate sizes, and use them to define alpha .

    syms x [4 1] matrix
    syms y [3 1] matrix
    syms A [3 4] matrix
    alpha = y.'*A*x
    alpha = yTAx

    Find the derivative of alpha with respect to the vectors x and y .

    Dx = diff(alpha,x)
    Dx = yTA
    Dy = diff(alpha,y)
    Dy = xTAT

    Since R2021a

    To evaluate derivatives with respect to matrices, you can use symbolic matrix variables. For example, find the derivative Y / A for the expression Y = X T A X , where X is a 3-by-1 vector, and A is a 3-by-3 matrix. Here, Y is a scalar that is a function of the vector X and the matrix A .

    Create two symbolic matrix variables to represent X and A . Define Y .

    syms X [3 1] matrix
    syms A [3 3] matrix
    Y = X.'*A*X
    Y = XTAX

    Find the derivative of Y with respect to the matrix A .

    D = diff(Y,A)
    D = XTX

    The result is a Kronecker tensor product of X T and X , which is a 3-by-3 matrix.

    size(D)
    ans = 1×2
         3     3
    

    Since R2022a

    Differentiate a symbolic matrix function with respect to its matrix argument.

    Find the derivative of the function t ( X ) = A sin ( B X ) , where A is a 1-by-3 matrix, B is a 3-by-2 matrix, and X is a 2-by-1 matrix. Create symbolic matrix variables to represent A , B , and X , and create a symbolic matrix function to represent t ( X ) .

    syms A [1 3] matrix
    syms B [3 2] matrix
    syms X [2 1] matrix
    syms t(X) [1 1] matrix keepargs
    t(X) = A*sin(B*X)
    t(X) = Asin(BX)

    Differentiate the function with respect to X .

    Dt = diff(t,X)
    Dt(X) = Acos(BX)B

    Since R2023b

    To find the gradient of a scalar expression with respect to a vector, you can use a symbolic matrix variable as the differentiation parameter.

    Create a symbolic matrix variable X to represent a vector with three components. To see how these components are stored in Symbolic Math Toolbox™, use symmatrix2sym to display the elements of the symbolic matrix variable.

    syms X [1 3] matrix
    symmatrix2sym(X)
    ans = (X1,1X1,2X1,3)

    The components of the symbolic matrix variable are X1_1 , X1_2 , and X1_3 . Create three symbolic scalar variables for these components. Create a scalar symbolic expression expr using these scalar variables.

    syms X1_1 X1_2 X1_3
    expr = 2*X1_2*sin(X1_1) + 3*sin(X1_3)*cos(X1_2);

    Find the gradient of the scalar expression expr with respect to X . The diff function finds the first partial derivatives of expr with respect to each component of X .

    g = diff(expr,X)
    g = 
    

    Σ1where  Σ1=(2X1,2cos(X1,1)2sin(X1,1)-3sin(X1,2)sin(X1,3)3cos(X1,2)cos(X1,3))

    Input Arguments

    collapse all

    Expression or function to differentiate, specified as one of these values:

    • Symbolic expression

    • Symbolic function

    • Symbolic vector or symbolic matrix (a vector or a matrix of symbolic expressions or functions)

    • Symbolic matrix variable

    • Symbolic matrix function

    • Numeric expression

    If f is a symbolic vector or matrix, diff differentiates each element of f and returns a vector or a matrix of the same size as f .

    Data Types: sym | symfun | symmatrix | symfunmatrix | double | single

    Order of derivative, specified as a nonnegative integer.

    Differentiation parameter, specified as a symbolic scalar variable, symbolic function, or a derivative function created using the diff function.

    If you specify differentiation with respect to the symbolic function var = f(x) or the derivative function var = diff(f(x),x) , then the first argument f must not contain any of these values:

    • Integral transforms, such as fourier , ifourier , laplace , ilaplace , htrans , ihtrans , ztrans , and iztrans

    • Unevaluated symbolic expressions that include limit or int

    • Symbolic functions evaluated at a specific point, such as f(3) or g(0)

    Data Types: sym | symfun

    Differentiation parameters, specified as symbolic scalar variables, symbolic functions, or derivative functions created using the diff function.

    Data Types: sym | symfun

    Differentiation parameter in the form of a matrix, specified as a symbolic matrix variable or symbolic matrix function.

    When using a symbolic matrix variable or function as the differentiation parameter, f must be a differentiable scalar function or expression, where mvar can represent a scalar, vector, or matrix. The derivative of f cannot be a tensor or a matrix in terms of tensors. For examples, see Differentiate with Respect to Vectors and Differentiate with Respect to Matrix .

    When differentiating a scalar function f with respect to a vector or matrix mvar , diff uses the convention of returning an output size that is the transpose of the input size mvar . For example, if f is a 1-by-1 scalar and mvar is a 1-by-3 row vector, then diff(f,mvar) finds the derivative of f with respect to each element of the transposed mvar and returns the result as a 3-by-1 column vector.

    Data Types: symmatrix | symfunmatrix

    Limitations

    • The diff function does not support tensor derivatives when using a symbolic matrix variable as the differentiation parameter. If the derivative is a tensor, or the derivative is a matrix in terms of tensors, then the diff function generates an error.

    Tips

    • When computing mixed higher-order derivatives with more than one variable, do not use n to specify the order of the derivative. Instead, specify all differentiation variables explicitly.

    • To improve performance, diff assumes that all mixed derivatives commute. For example,

      x y f ( x , y ) = y x f ( x , y )

      This assumption suffices for most engineering and scientific problems.

    • If you differentiate a multivariate expression or function f without specifying the differentiation variable, then a nested call to diff and diff(f,n) can return different results. The reason is that in a nested call, each differentiation step determines and uses its own differentiation variable. In calls like diff(f,n) , the differentiation variable is determined once by symvar(f,1) and used for all differentiation steps.

    • If you differentiate an expression or function containing abs or sign , the arguments must be real values. For complex arguments of abs and sign , the diff function formally computes the derivative, but this result is not generally valid because abs and sign are not differentiable over complex numbers.

    Version History

    Introduced before R2006a

    expand all

    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.