首页 > 解决方案 > 符号分化

问题描述

好吧,我知道,对于正常情况,如果我定义

syms x,y
K = f(x,y)

作为 and 的一个显式表达式xy我们可以做diff(K, x)ordiff(K, y)来获得我们想要的东西。

但是现在,如果我有另一个功能

J = g(K)

我想做

diff(J, K)

然后错误发生为:

'第二个参数必须是一个变量或一个指定微分数量的非负整数。

那么,简而言之,如何解决这种“链式表达分化”?(对不起,这个模棱两可的描述。)

标签: matlabsymbolic-math

解决方案


根据 Matlab 中的 diff 函数,

第一个参数应该是您要微分的函数,其余参数必须是符号变量或表示微分次数的非负数。

所以,错误。

The second argument must be a variable or a non negative integer specifying the number of differentiations.

在代码diff(J, K)中说 K 是 Matlab 的符号变量,但在实际情况下,K 是 x 和 y 的表达式。因此,这就是 Matlab 抛出该错误的原因。

因此,如果您想区分带有变量 x, y 的链式函数,那么每当您想要区分该表达式时,您需要在 diff() 函数中明确提及每个符号变量。执行此操作的代码如下。

% define the symbolic functions
% as f(x,y) and g(K)
syms f(x,y) g(K)

% create the functions
K = f(x,y)
J = g(K)

% differentiate the functions with
% respect to x and y respectively.
% ------------------------
% differentiate w.r.t x
diff_K_x = diff(K, x)
% differentiate w.r.t y
diff_K_y = diff(K, y)
% -----------------------

% differentiate the function J with respect to x and y both
diff_K_xy = diff(J,x,y)

推荐阅读