MATLAB Symbolic Toolbox 支持纯函数吗?

发布于 2024-11-08 23:19:18 字数 415 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心欲静而疯不止 2024-11-15 23:19:18

从你的问题来看:

这不起作用,因为 MATLAB 计算 diff(f,x)=diff(f,y)=0 (它不知道它是否是一个函数)。

这不是一种异常行为,而是预期的行为。当您将 f 初始化为符号变量时,没有与 f 关联的定义,因此 x 的导数应该 返回0,而导数本身应该返回1。 Mathematica 的行为完全相同:

MATLAB:

syms f x
diff(f,x)
 
ans =
 
0

diff(f,f)
 
ans =
 
1

Mathematica

In[1]:= D[f, x]
        D[f, f]

Out[1]= 0
Out[2]= 1

对于纯函数,定义独立于实际函数,如果您插入任何参数,它应该对其求值。例如,mathematica 中关于 x 的导数的纯函数定义,D[#, x] &

In[3]:= D[#, x] &[a x^2 + b x + c]
Out[3]= 2 a x

In[4]:= D[#, x] &[a x^3 + b f]
Out[4]= 3 a x^2

在 MATLAB 中最接近的等效项称为 匿名函数。上述函数的定义是

syms f x a b c
y=@(f)diff(f,x);

y(a*x^2+b*x+c)

ans =
 
b + 2*a*x

现在来到您想要做的事情,在 Mathematica 中可以保留某些未计算的表达式并对其进行处理以获得您想要的最终输出形式。但是,我不知道 MATLAB 使用符号工具箱具有这种功能。

From your question:

This doesn't work, because MATLAB evaluates diff(f,x)=diff(f,y)=0 (it doesn't know if it is a function).

This is not an abnormal behaviour, rather the expected one. When you initialize f to be a symbolic variable, there is no definition associated with f and hence a derivative w.r.t x should return 0 and a derivative w.r.t. itself should return 1. Mathematica behaves exactly the same:

MATLAB:

syms f x
diff(f,x)
 
ans =
 
0

diff(f,f)
 
ans =
 
1

Mathematica

In[1]:= D[f, x]
        D[f, f]

Out[1]= 0
Out[2]= 1

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] &

In[3]:= D[#, x] &[a x^2 + b x + c]
Out[3]= 2 a x

In[4]:= D[#, x] &[a x^3 + b f]
Out[4]= 3 a x^2

The closest equivalent of this in MATLAB is called an anonymous function. The definition for the above function is

syms f x a b c
y=@(f)diff(f,x);

y(a*x^2+b*x+c)

ans =
 
b + 2*a*x

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文