开发者

Are pure functions supported by MATLAB Symbolic Toolbox?

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 d开发者_如何转开发erivative 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜