MATLAB Symbolic Toolbox 支持纯函数吗?
Mathematica 的纯函数是否在 MATLAB Symbolic Toolbox 中以某种方式实现?我想计算嵌套微分。
一个简单的例子: 我想获得二维函数的方向导数,即最大变化的方向。
syms f x y w
w = [ diff(f,x); diff(f,y) ] / sqrt(diff(f,x) + diff(f,y));
d = w * [ diff(f,x), diff(f,y) ];
我想得到答案:
d = sqrt(diff(f,x)^2 + diff(f,y)^2);
这不起作用,因为 MATLAB 计算 diff(f,x)=diff(f,y)=0 (它不知道它是否是一个函数)。 MATLAB 符号工具箱是否能够实现我想要实现的目标?
Are pure functions of Mathematica implemented somehow in MATLAB Symbolic Toolbox? I would like to calculate nested differentiations.
A simple example:
I would like to get the directional derivative of a 2D function int the direction of the largest change.
syms f x y w
w = [ diff(f,x); diff(f,y) ] / sqrt(diff(f,x) + diff(f,y));
d = w * [ diff(f,x), diff(f,y) ];
I would like to get the answer:
d = sqrt(diff(f,x)^2 + diff(f,y)^2);
This doesn't work, because MATLAB evaluates diff(f,x)=diff(f,y)=0 (it doesn't know if it is a function). Is MATLAB symbolic toolbox capable of something like I would like to achieve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从你的问题来看:
这不是一种异常行为,而是预期的行为。当您将
f
初始化为符号变量时,没有与f
关联的定义,因此x
的导数应该 返回0
,而导数本身应该返回1
。 Mathematica 的行为完全相同:MATLAB:
Mathematica
对于纯函数,定义独立于实际函数,如果您插入任何参数,它应该对其求值。例如,mathematica 中关于
x
的导数的纯函数定义,D[#, x] &
在 MATLAB 中最接近的等效项称为 匿名函数。上述函数的定义是
现在来到您想要做的事情,在 Mathematica 中可以保留某些未计算的表达式并对其进行处理以获得您想要的最终输出形式。但是,我不知道 MATLAB 使用符号工具箱具有这种功能。
From your question:
This is not an abnormal behaviour, rather the expected one. When you initialize
f
to be a symbolic variable, there is no definition associated withf
and hence a derivative w.r.tx
should return0
and a derivative w.r.t. itself should return1
. Mathematica behaves exactly the same:MATLAB:
Mathematica
With pure functions, the definition is independent of the actual function and if you chuck in any argument, it should evaluate it. For e.g., the pure function definition of a derivative w.r.t.
x
in mathematica,D[#, x] &
The closest equivalent of this in MATLAB is called an anonymous function. The definition for the above function is
Now coming to what you wanted to do, it is possible in Mathematica to hold certain expressions unevaluated and massage it to get it in the final output form that you want. However, I'm not aware of such a capability in MATLAB using the symbolic toolbox.