多匿名函数 MATLAB

Multiple anonymous function MATLAB

我的目标是计算一个函数的导数,然后使用这个结果作为另一个 功能。显然,这意味着:

f = @(x) (x-1)*(x-2);      %A simple function
derivative = jacobian(f,x) %MATLAB output : "2*x - 3"
df = @(x) derivative       %= @(x) 2*x - 3
df(2)                      %= "2*x -3" instead of 2*2 - 3

我怎么能做这样的事?我试过 syms x 但没有用。

你想要matlabFunction:

g = matlabFunction(f) converts the symbolic expression or function f to a MATLAB function with handle g.

在你的例子中:

>> syms x
>> f = @(x) (x-1)*(x-2);
>> derivative = jacobian(f,x);
>> df = matlabFunction(derivative)
df =
  function_handle with value:
    @(x)x.*2.0-3.0
>> df(2)
ans =
     1