Use derivative of function directly as an anonymous function

664 views Asked by At

I would like to ask, if it is possible to create a anonymous function directly from a return value after using the diff function? Without copy the text from the console and add it manually to a anonymous function.

Eg.

xy @(x)=diff(x^2,x);

and using afterwards as: xy(3) and so on.

1

There are 1 answers

0
horchler On

You can use a symfun symbolic function:

syms x
f(x) = x^2;    % Equivalent to: f = symfun(x^2,x);
df = diff(f,x) % Since f is a symfun, df will be
df(3)

which returns

df(x) =

2*x


ans =

6