matlab - 符号分化
问题描述
好吧,我知道,对于正常情况,如果我定义
syms x,y
K = f(x,y)
作为 and 的一个显式表达式x
,y
我们可以做diff(K, x)
ordiff(K, y)
来获得我们想要的东西。
但是现在,如果我有另一个功能
J = g(K)
我想做
diff(J, K)
然后错误发生为:
'第二个参数必须是一个变量或一个指定微分数量的非负整数。
那么,简而言之,如何解决这种“链式表达分化”?(对不起,这个模棱两可的描述。)
解决方案
根据 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)
推荐阅读
- python-3.x - 定义 __slots__ 的 Python 元类使 __slots__ 只读
- c++ - 字符串的实现相关输出
- python - 在 PyQt5 中,为什么 Signal 无法连接外部类中的 Slot?
- wordpress - 将本地计算机上的 Wordpress 站点转换/导出为静态内容,以从 s3 存储桶托管站点
- angular - Ag-grid 自定义过滤器
- google-cloud-platform - 谷歌云存储:新文件上传时缓存无效
- scala - 如何测试 akka-http 路由来处理异常?
- python - 你可以重新训练 RAKE 吗?
- javascript - 我无法停止执行功能
- emacs - 如果所有子 TODO 都已完成,如何归档组织模式 TODO 条目?