MATLAB 中的微分或导数
函数y = f(x) 的微分告诉我们 y 的值如何随 x 的变化而变化。它也可以称为函数的斜率。
衍生物的函数f(x)的WRT至x被表示为
MATLAB 允许用户使用 diff() 方法计算函数的导数。 diff() 方法的不同语法是:
- f' = diff(f)
- f' = diff(f, a)
- f' = diff(f, b, 2)
f' = diff(f)
它返回函数f(x) 与变量 x 的导数。
示例 1:
Matlab
% Create a symbolic expression in variable x
syms x
f = cos(x);
disp("f(x) :");
disp(f);
% Derivative of f(x)
d = diff(f);
disp("Derivative of f(x) :");
disp(d);
Matlab
% Create a symbolic expression in
# variable x
syms x
f = cos(x);
disp("f(x) :");
disp(f);
% Derivative of f(x)
d = diff(f);
val = subs(d,x,pi/2);
disp("Value of f'(x) at x = pi/2:");
disp(val);
Matlab
% Create a symbolic expression in variable x
syms x t;
f = sin(x*t);
disp("f(x) :");
disp(f);
% Derivative of f(x,t) wrt t
d = diff(f,t);
disp("Derivative of f(x,t) wrt t:");
disp(d);
Matlab
% Create a symbolic expression in
% variable x,n
syms x n;
f = x^n;
disp("f(x,n) :");
disp(f);
% Double Derivative of f(x,n) wrt x
d = diff(f,x,2);
disp("Double Derivative of f(x,n) wrt x:");
disp(d);
Matlab
% Create a symbolic expression in variable
% u and v
syms u v;
f = u^2;
g = sin(v)*(3*u);
disp("f(u,v) :");
disp(f);
disp("g(u,v) :");
disp(g);
% Jacobian matrix of function f(u,v) and
% g(u,v)
J = jacobian([f; g], [u v]);
disp("Jacobian matrix :");
disp(J);
% Determinant of Jacobian matrix
d = det(J);
disp("Determinant of Jacobian matrix:");
disp(d);
输出 :
示例 2:使用subs(y,x,k) 计算指定值处函数的导数。
- subs(y,x,k) ,它给出了函数y在x = k处的值。
MATLAB
% Create a symbolic expression in
# variable x
syms x
f = cos(x);
disp("f(x) :");
disp(f);
% Derivative of f(x)
d = diff(f);
val = subs(d,x,pi/2);
disp("Value of f'(x) at x = pi/2:");
disp(val);
输出 :
f' = diff(f, a)
- 它返回函数f关于变量a的导数。
MATLAB
% Create a symbolic expression in variable x
syms x t;
f = sin(x*t);
disp("f(x) :");
disp(f);
% Derivative of f(x,t) wrt t
d = diff(f,t);
disp("Derivative of f(x,t) wrt t:");
disp(d);
输出 :
f' = diff(f, b, 2)
它返回函数f关于变量b 的二重导数。
示例 1:
MATLAB
% Create a symbolic expression in
% variable x,n
syms x n;
f = x^n;
disp("f(x,n) :");
disp(f);
% Double Derivative of f(x,n) wrt x
d = diff(f,x,2);
disp("Double Derivative of f(x,n) wrt x:");
disp(d);
输出 :
同样,您还可以使用diff(f,x,k)计算函数f的k 阶导数。
示例 2:
计算偏导数 } 使用雅可比矩阵和行列式。
MATLAB
% Create a symbolic expression in variable
% u and v
syms u v;
f = u^2;
g = sin(v)*(3*u);
disp("f(u,v) :");
disp(f);
disp("g(u,v) :");
disp(g);
% Jacobian matrix of function f(u,v) and
% g(u,v)
J = jacobian([f; g], [u v]);
disp("Jacobian matrix :");
disp(J);
% Determinant of Jacobian matrix
d = det(J);
disp("Determinant of Jacobian matrix:");
disp(d);
输出 :