Conversion to function_handle from double is not possible.

4 visualizaciones (últimos 30 días)
Nicholas Eyerman
Nicholas Eyerman el 14 de Sept. de 2019
Respondida: John D'Errico el 14 de Sept. de 2019
f=@(x) ((1/3)*(x-2)^2)-3
function [r,k] = RegulaFalsi_Mod(f,a,b,kmax,tol)
if nargin <5
isempty(tol), tol = 1e-4;
end
if nargin<4
isempty(kmax), kmax = 20;
end
c=zeros(1,kmax);
if f(a)*f(b)>0
r = 'failure';
return
end
disp (' k a b')
for k = 1:kmax
c(k) = (a*f(b) - b*f(a))/(f(b)-f(a));
if f(c(k)) == 0
return
end
for k = 1:3
if f(3) == f(k)
f(a) = (1/k)*f(a);
elseif f(3) ~= f(k)
continue
for k=1:6
if c(6) == c(k)
f(a) = (1/(2*k))*f(a);
elseif c(6)~=c(k)
continue
end
end
end
end
end
fprintf('%2i, %11.6f, %11.6f\n',k,a,b)
if f(b)*f(c(k))>0
b=c(k);
else a = c(k);
end
c(k+1) = (a*f(b)-b*f(a))/(f(b)-f(a));
if abs(c(k+1)-c(k)) < tol
r = c(k+1);
return
end
end
Error Message
Conversion to function_handle from double is not possible.
Error in RegulaFalsi_Mod (line 21)
f(a) = (1/k)*f(a);
  2 comentarios
darova
darova el 14 de Sept. de 2019
Maybe it is becase your function handle is not in the function body? Why it is outside?
John D'Errico
John D'Errico el 14 de Sept. de 2019
f is passed in as an argument. That is not the issue.

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 14 de Sept. de 2019
LOOK AT THE ERROR MESSAGE. Think about what it tells you.
Error in RegulaFalsi_Mod (line 21)
f(a) = (1/k)*f(a);
What re you trying to do there? f is a function handle! The right hand side of that expression is a double precision number.
You cannot assign something to an existing function handle as you tried to do.
Essentially, you cannot use f as both a function handle (which it is) and a variable where you will store information. And worse, even if you could do that, you cannot use a floating point number as an index into a vector or any variable. You CANNOT assign something to f(a).
And that is the gist of your problem.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by