简体   繁体   中英

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.

You want matlabFunction :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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