繁体   English   中英

多个匿名函数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)将符号表达式或函数f转换为带句柄g的MATLAB函数。

在你的例子中:

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