using anonymous functions in nested functions returns error if function is constant and you want do build the derivate, why and how to handle?

1 view (last 30 days)
So i have this kind of nested function system using math toolbox
function difftest
x=sym('x')
start
function start
input='0' %input=0 or other number -> error
y=str2func(['@(x)' (input)])
z(x)=diff(y(x)) %error
p(x)=diff(z(x))
h(x)=diff(p(x))
end
end
and i need the first,second and third derivate of the function called from user input, when the user input='0' % or other constant value the anonymous function is correctly y(x)=0 but the derivate with diff(y(x)) returns an error! This is not the case if y(x)=x and all the derivates are correctly displayed. What am i doing wrong?
Thanks in advance, Phil

Answers (1)

Walter Roberson
Walter Roberson on 9 Dec 2015
What you are doing wrong is failing to supply the name of the variable to differentiate with respect to. When you do not supply a variable then symbolic diff() needs to use symvar() to find a variable to differentiate with respect to, and when your expression does not name any variables that is going to fail.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!