Undefined function 'diff' for input arguments of type 'function_handle'.
8 views (last 30 days)
Show older comments
I am trying to produce a function that receives an inital input x0 (for example, x0 = 1) and the result should be the function e^2sin(x) - x at x0 and it's derivative's function at x0. I keep getting the error "Undefined function 'diff' for input arguments of type 'function_handle'." and I'm not sure why. Any advice would be appreciated!
function [Val1, Val2] = nonlinear1(x0)
syms x;
f = @(x) exp(2*sin(x)) - x;
Val1 = f(x0);
g = diff(f);
Val2 = g(x0);
end
0 Comments
Answers (2)
KSSV
on 23 Jan 2020
function [Val1, Val2] = nonlinear1(x0)
syms x;
f = exp(2*sin(x)) - x;
Val1 = f(x0);
g = diff(f);
Val2 = g(x0);
end
Don't use @(x)....as it is symbolic, straight away use the expression.
0 Comments
Walter Roberson
on 23 Jan 2020
There are two major diff() functions in MATLAB. One of them is the numeric difference operator, roughly
x(:, 2:end) - x(:, 1:end-1)
This is applicable only to numeric arrays. A function handle is not a numeric array so this kind of diff() does not apply to it.
The other major diff() function is part of the Symbolic Toolbox. The first parameter to it must be a symbolic array, or a symbolic function. The operation performed is calculus differentiation. A function handle is not a symbolic array and is not a symbolic function, so this kind of diff() does not apply to it.
There is no diff() function defined to operate on function handles.
What you need is
g(x) = diff(f(x), x);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!