Conversion to function_handle from double is not possible.

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

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.

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2019년 9월 14일
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.

카테고리

도움말 센터 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품

질문:

2019년 9월 14일

답변:

2019년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by