MATLAB Answers


Differentiate an inline function

Asked by Jose
on 23 Nov 2012


I have an assignment in Matlab and I need to find a function's 8th and 9th derivate. This function is an inline function, as it is an input from the user.

I have been trying to use the function diff, but it tells me: «Function 'diff' is not supported for class 'inline'.»

Any ideas?


Is it required that you use an inline function? Could you use a symbolic function instead? Considering that you are using the symbolic diff() routine?

on 23 Nov 2012

I wish, but the function is an input from the user, and I don't know any way to convert it to a symbolic one.

But if there is an alternative to the diff function, it may work.

syms x
f = sym(string_from_input);


No products are associated with this question.

2 Answers

Answer by Matt Fig
on 23 Nov 2012
Edited by Matt Fig
on 23 Nov 2012
 Accepted answer

f = inline('x^8');  % Our inline function.
D8 = diff(sym(f),8) % Find the 8th derivative
D8 == prod(1:8)     % Check.  ans = 1 as expected.

  1 Comment

on 23 Nov 2012

Thanks you so much!

Answer by Matt J
on 23 Nov 2012
Edited by Matt J
on 23 Nov 2012

You cannot do symbolic differentiation on inline functions. If you don't have the Symbolic Toolbox, you will have to either consider a specific family of functions and pre-analyze their derivatives. Or, if you want to do numeric differentiation,

f = vectorize(inline(string_from_input));
result = diff(f(t),8)./( t(2)-t(1) )^8;


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!