简体   繁体   English

多个匿名函数MATLAB

[英]Multiple anonymous function MATLAB

My goal is to compute the derivative of a function and use this result as another function. 我的目标是计算函数的导数并将此结果用作另一个函数。 Clearly, it means : 显然,这意味着:

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

How can I do such a thing ? 我该怎么办? I tried syms x but it doesn't help. 我尝试了syms x,但它没有帮助。

You want matlabFunction : 你想要matlabFunction

g = matlabFunction(f) converts the symbolic expression or function f to a MATLAB function with handle g . g = matlabFunction(f)将符号表达式或函数f转换为带句柄g的MATLAB函数。

In your example: 在你的例子中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM