MATLAB: Undefined function ‘matlabFunction’ for input arguments of type ‘double’.

errormatlab function

I can't seem to figure out why I am getting the error for the code
function [r,n]= Problem25b(f,x1,tol,N)
if nargin<4 || isempty(N), N=20; end
if nargin<3 || isempty(tol), tol=1e-6; end
fp=matlabFunction(diff(f));
f2p=MatLabFunction(diff(f,2));
x=zeros(1,N+1);
x(1)=x1;
for n=1:N
x(n+1)=x(n)-(f(x(n))*fp(x(n)))/(fp(x(n)))^(2)-f(x(n))*f2p(x(n));
if abs(x(n+1)-x(n))<tol
r=x(n+1);
return
end
end
when I run it I get the error:
[r,n]=Problem25b('x^(3)+2x^(2)+x+2',-3,1e-6,20)
Undefined function 'matlabFunction' for input arguments of type 'double'.
Error in Problem25b (line 5)
fp=matlabFunction(diff(f));

Best Answer

'x^(3)+2x^(2)+x+2'
is a character vector. diff() applied to a character vector gives numeric values:
ans =
-26 -54 11 -10 2 7 70 -26 -54 10 -9 2 77 -77 7
There is no matlabFunction() for numeric values.
You need to construct a symbolic expression out of the character vector that is passed in.
In older MATLAB releases, you could use sym() to convert character vectors into symbolic expressions. In newer MATLAB releases, you need to use str2sym()
Related Question