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

4 visualizaciones (últimos 30 días)
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 comentarios
Lucas Pinto
Lucas Pinto el 5 de En. de 2021
My full code
func='2.02*x^(5)-1.28*x^(4)+3.06*x^(3)-2.92*x^(2)-5.66*x+6.08';
fprintf('\nFunção a estudar: %s \nZero ∈ [-1.5,-1]\n\n',func)
%% Método da Bissecção
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
xm = (xinf + xsup)/2; % Valor médio do intervalo
erro = (xsup - xinf)/2; % Erro
while erro > 0.5*10^-6 % Erro absoluto máximo
f = 2.02*(xinf^5)-1.28*(xinf^4)+3.06*(xinf^3)-2.92*(xinf^2)-5.66*xinf+6.08;
fxm = 2.02*(xm^5)-1.28*(xm^4)+3.06*(xm^3)-2.92*(xm^2)-5.66*xm+6.08;
if f*fxm > 0
xinf = xm;
elseif f*fxm < 0
xsup = xm;
elseif f*fxm == 0
result = xm;
end
xm = (xinf + xsup)/2; % Valor médio do intervalo
erro = (xsup - xinf)/2; % Erro
result = xm;
end
fprintf('Método da Bissecção\nO zero da função é: %f \n',result)
%plot()
% Condição de Convergência da Bissecção
if(xinf < xsup || (fxm(xinf) < 0 && fxm(xsup) > 0) || (f(xinf) > 0 && f(xsup) < 0))
fprintf('\n\tCondição Aprovada\n\n\n')
else
fprintf('\n\tCondição Chumbada\n\n\n')
end
%% Método da corda falsa
f=@(x) 2.02*x^(5)-1.28*x^(4)+3.06*x^(3)-2.92*x^(2)-5.66*x+6.08;
xinf=-1.5;
xsup=-1;
for i=1:10
x0=xinf; x1=xsup;
x2(i)=x0-(x1-x0)/(f(x1)-f(x0))*f(x0);
if f(x2(i))>0
xsup=x2(i);
else xinf=x2(i);
end
result=x2(i);
end
for i=1:10
error(i)=result-x2(i);
end
fprintf('Método da Corda Falsa\nO zero da função é: %f \n\n',result)
%% Rotinas
fprintf('Resultados obtidos pelas rotinas\n\n\n')
% Método da Bissecção
fprintf('\tM. da Bissecção\n\n')
bissec(f,xinf,xsup,erro,100)
% Condição de Convergência da Bissecção
if(xinf < xsup || (fxm(xinf) < 0 && fxm(xsup) > 0) || (f(xinf) > 0 && f(xsup) < 0))
fprintf('\n\tCondição Aprovada\n\n\n')
else
fprintf('\n\tCondição Chumbada\n\n\n')
end
% Método da Corda Falsa
fprintf('\tM. da Corda Falsa\n\n')
cordafalsa(f,xinf,xsup,erro,100)
% Método de Newton
fprintf('\tM. de Newton\n\n')
[x,y]=newton('ydy',2,0.5*10^-6,0.5*10^-6,100);
dpb
dpb el 5 de En. de 2021
What's ydy?
fevel tries to execute it; didn't find it defined.

Iniciar sesión para comentar.

Respuesta aceptada

Bjorn Gustavsson
Bjorn Gustavsson el 5 de En. de 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 comentarios
Steven Lord
Steven Lord el 6 de En. de 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)
y = 1.4577e+04
dy = 1.2273e+04
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
If you do this, you probably want to vectorize ydy.
Lucas Pinto
Lucas Pinto el 6 de En. de 2021
I had "@f(x)" in the ydy.m file and couldn´t, now the error is fixed.
Thank you all for the help given!
function [y,dy]=ydy(k)
y= 2.02*k^(5)-1.28*k^(4)+3.06*k^(3)-2.92*k^(2)-5.66*k+6.08; % f(x)
dy=10.1*k^(4)-5.12*k^(3)+9.18*k^(2)-5.84*k-5.66; % df(x)/dx
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Configure Simulation Conditions en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by