Nonscalar arrays of function handles are not allowed; use cell arrays instead.
5 views (last 30 days)
Show older comments
Lucas Pinto
on 5 Jan 2021
Commented: Lucas Pinto
on 6 Jan 2021
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);
4 Comments
Accepted Answer
Bjorn Gustavsson
on 5 Jan 2021
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
2 Comments
Steven Lord
on 6 Jan 2021
I agree, most likely ydy.m should return the values of the functions rather than function handles that people can use to evaluate the function.
[y, dy] = ydy(6)
function [y,dy]=ydy(x)
y= 2.02*x^(5)-1.28*x^(4)+3.06*x^(3)-2.92*x^(2)-5.66*x+6.08; % f(x)
dy= 10.1*x^(4)-5.12*x^(3)+9.18*x^(2)-5.84*x-5.66; % df(x)/dx
end
More Answers (0)
See Also
Categories
Find more on Configure Simulation Conditions 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!