Mathematical Function as Matlab-Function Input

12 views (last 30 days)
Hello Everybody!
I have a probably pretty easy to answer question, but I am just not getting to it. How can I tell Matlab to use a mathematical function as an input argument?
To make clear what I mean a very simple example:
function [df] = derivate(y)
x=-20:20;
df=diff(y);
end
If I try to run this now with any kind of function like 3*x+5, Matlab tells me x wasn't defined. Can you tell me my mistake?
Thanks in advance!!!

Accepted Answer

Walter Roberson
Walter Roberson on 26 Oct 2011
You need the symbolic toolbox in order to do this, and you need to declare your free variable with either a sym() or syms() call.
Please also note that you cannot take the derivative of a mathematical function over a range.
If you are trying to do numeric derivatives then you should recode as something like:
function df = derivate(f)
x = -20:20;
df = diff(f(x));
end
and you would then call it with a function handle, such as calling
derivate(@(x) 3*x+5)
  1 Comment
Ninja Katja
Ninja Katja on 27 Oct 2011
Function Handle - that's the thing I was looking for! Thank you! :-)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!