main content

differentiate symbolic expression or function -凯发k8网页登录

differentiate symbolic expression or function

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 nth 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 nth 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 mvar of type .

examples

find the derivative of the function 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 this expression.

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 defined 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 4th, 5th, and 6th derivatives of t6.

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 this expression with respect to the variable y.

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

compute the second derivative of the expression x*y. if you do not specify the differentiation variable, diff uses the variable determined by symvar. for this expression, symvar(x*y,1) returns x. therefore, 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, differentiate 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.

thus, 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 this expression 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 providing all differentiation variables.

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)2dfdx 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 2nd derivative of the function y=f(x)2dfdx 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)2dfdx with respect to f(x) and dfdx.

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=ddtl(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

to evaluate derivatives with respect to vectors, you can use symbolic matrix variables. for example, find the derivatives α/x and α/y for the expression α=ytax, 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

to evaluate a derivative with respect to a matrix, you can use symbolic matrix variables. for example, find the derivative y/a for the expression y=xtax, 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 between xt and x, which is a 3-by-3 matrix.

size(d)
ans = 1×2
     3     3

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

find the derivative of the function t(x)=asin(bx), where a is a 1-by-3 matrix, b is a 3-by-2 matrix, and x is a 2-by-1 matrix. create a, b, and x as symbolic matrix variables and t(x) as a symbolic matrix function.

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 using diff.

dt = diff(t,x)
dt(x) = acos(bx)b

input arguments

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

  • a symbolic expression

  • a symbolic function

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

  • a symbolic matrix variable

  • a symbolic matrix function

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: single | double | sym | symfun | symmatrix | symfunmatrix

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:

  • 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: single | double | sym | symfun

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

data types: single | double | sym | symfun

differentiation parameter, specified as a symbolic matrix variable.

when using a symbolic matrix variable as the differentiation parameter, f must be a differentiable scalar function, 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 example, see differentiate with respect to vectors and differentiate with respect to matrix.

data types: symmatrix

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 will error.

tips

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

  • to improve performance, diff assumes that all mixed derivatives commute. for example,

    xyf(x,y)=yxf(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

see also

| | | | | | | |

external websites

    网站地图