Conversion to function_handle from double is not possible.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
darova
2019-9-14
Maybe it is becase your function handle is not in the function body? Why it is outside?
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!