![]() |
腹黑的鼠标 · 《底线》的4个真实案件:辱母案江歌案北大弑母 ...· 1 年前 · |
![]() |
发怒的打火机 · 【文字解读】促进滨海旅游高品质发展,建设世界 ...· 1 年前 · |
![]() |
乐观的甘蔗 · 施图伦滴眼液- 商品搜索- 京东· 1 年前 · |
![]() |
重感情的椰子 · 华硕灵耀X凌锋首发评测:重量不到1kg,颜值 ...· 1 年前 · |
Differentiate symbolic expression or function
differentiates
Df
= diff(
f
)
f
with respect to the symbolic scalar
variable determined by
symvar(f,1)
.
differentiates
Df
= diff(
f
,
var1,...,varN
)
f
with respect to the parameters
var1,...,varN
.
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) =
Find the value of the derivative at
x = 2
. Convert the value to
double
.
Df2 = Df(2)
Df2 =
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 =
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 =
Now, find the derivative of this expression with respect to the variable
t
.
Df = diff(sin(x*t^2),t)
Df =
Find the fourth, fifth, and sixth derivatives of .
syms t
D4 = diff(t^6,4)
D4 =
D5 = diff(t^6,5)
D5 =
D6 = diff(t^6,6)
D6 =
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 =
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 =
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 =
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 =
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 =
Find the derivative of the function with respect to .
syms f(x) y y = f(x)^2*diff(f(x),x); Dy = diff(y,f(x))
Dy =
Find the second derivative of the function with respect to .
Dy2 = diff(y,f(x),2)
Dy2 =
Find the mixed derivative of the function with respect to and .
Dy3 = diff(y,f(x),diff(f(x)))
Dy3 =
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 =
The Euler–Lagrange equation is given by
.
Evaluate the term .
D1 = diff(L,diff(x(t),t))
D1 =
Evaluate the second term .
D2 = diff(L,x)
D2(t) =
Find the Euler–Lagrange equation of motion of the mass-spring system.
diff(D1,t) - D2 == 0
ans(t) =
Since R2021a
To evaluate derivatives with respect to vectors, you can use symbolic matrix variables. For example, find the derivatives and for the expression , where is a 3-by-1 vector, is a 3-by-4 matrix, and 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 =
Find the derivative of
alpha
with respect to the vectors
and
.
Dx = diff(alpha,x)
Dx =
Dy = diff(alpha,y)
Dy =
Since R2021a
To evaluate derivatives with respect to matrices, you can use symbolic matrix variables. For example, find the derivative for the expression , where is a 3-by-1 vector, and is a 3-by-3 matrix. Here, is a scalar that is a function of the vector and the matrix .
Create two symbolic matrix variables to represent and . Define .
syms X [3 1] matrix syms A [3 3] matrix Y = X.'*A*X
Y =
Find the derivative of with respect to the matrix .
D = diff(Y,A)
D =
The result is a Kronecker tensor product of and , 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 , where is a 1-by-3 matrix, is a 3-by-2 matrix, and is a 2-by-1 matrix. Create symbolic matrix variables to represent , , and , and create a symbolic matrix function to represent .
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) =
Differentiate the function with respect to .
Dt = diff(t,X)
Dt(X) =
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 =
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 =
f
—
Expression or function to differentiate
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
n
—
Order of derivative
Order of derivative, specified as a nonnegative integer.
var
—
Differentiation parameter
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
var1,...,varN
—
Differentiation parameters
Differentiation parameters, specified as symbolic scalar variables,
symbolic functions, or derivative functions created using the
diff
function.
Data Types:
sym
|
symfun
mvar
—
Differentiation parameter in the form of matrix
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
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.
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,
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.
The
diff
function can differentiate expressions or functions of
type
sym
,
symfun
,
symmatrix
,
symfunmatrix
,
double
, and
single
with respect to
differentiation parameters of type
symmatrix
and
symfunmatrix
. For an example, see
Find Gradient with Respect to Vector
.
The
diff
function accepts an input argument of type
symfunmatrix
. You can differentiate symbolic matrix functions
with respect to differentiation parameters of type
sym
,
symfun
,
symmatrix
,
symfunmatrix
,
double
, and
single
. For an example, see
Differentiate Symbolic Matrix Function
.
The
diff
function accepts an input argument of type
symmatrix
. You can differentiate symbolic matrix variables
with respect to differentiation parameters of type
sym
,
symfun
,
symmatrix
,
double
, and
single
. For examples, see
Differentiate with Respect to Vectors
and
Differentiate with Respect to Matrix
.
curl
|
divergence
|
functionalDerivative
|
gradient
|
hessian
|
int
|
jacobian
|
laplacian
|
symvar
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.
![]() |
乐观的甘蔗 · 施图伦滴眼液- 商品搜索- 京东 1 年前 |