MATLAB: Nonscalar arrays of function handles are not allowed; use cell arrays instead.

MATLABnewton

How can I solve this error?
(ydy.m)
function [y,dy]=ydy(x)
y=@(x) 2.02*x^(5)-1.28*x^(4)+3.06*x^(3)-2.92*x^(2)-5.66*x+6.08; % f(x)
dy=@(x)10.1*x^(4)-5.12*x^(3)+9.18*x^(2)-5.84*x-5.66; % df(x)/dx
end
(parts of main script)
xinf = -1.5; % Abcissa inferior do intervalo de onde se encontra o zero da função
xsup = -1; % Abcissa superior do intervalo de onde se encontra o zero da função
function=2.02*x^(5)-1.28*x^(4)+3.06*x^(3)-2.92*x^(2)-5.66*x+6.08
% Newton Method
fprintf('\tM. de Newton\n\n')
[y,dy]=newton('ydy',2,0.5*10^-6,0.5*10^-6,100);
(command window)
(...)
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
Error in newton (line 18)
[y(k),dy(k)]=feval(ydy,x(k));
Error in PBL2_racunho (line 88)
[x,y]=newton('ydy',2,0.5*10^-6,0.5*10^-6,100);

Best Answer

You get the error in newton because the feval of your ydy-function returns function handles and you try to assign them to regular arrays (which matlab doesn't allow, there's reasons related ot confusion between indexing or function-evaluation to a regular array of function handles, for example a 1-by-1 array of function handles fcn(x) or fcn(1)(x)...), To solve this you would have to store the function-handles in cell-arrays. Something like this:
[y{k},dy{k}]=feval(ydy,x(k)); % in the newton.m function.
However, it seems like you treat the outputs as the actual values of the function y and its derivative dy later so perhap what you want is to return the value of the polynomial and its derivative at the point x you call it with and not the handles to these functions.
HTH
Related Question