MATLAB Answers

0

Conversion to function_handle from double is not possible.

Asked by Nicholas Eyerman on 14 Sep 2019
Latest activity Answered by John D'Errico
on 14 Sep 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 Comments

Maybe it is becase your function handle is not in the function body? Why it is outside?
f is passed in as an argument. That is not the issue.

Sign in to comment.

1 Answer

Answer by John D'Errico
on 14 Sep 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.

  0 Comments

Sign in to comment.