How can I get my bisection method function to work?
显示 更早的评论
I'm trying to write a bisection method algorithm to evaluate the minimum of a one variable function. I keep getting this error when i enter the command 'Fmin=bisection(4,6,10.^-3,F)':
"Undefined function 'bisection' for input arguments of type 'function_handle'. "
Can someone please help me figure out what I'm doing wrong?
My code is:
if true
% code
end
function Fmin = bisection(a,b,e,F)
%BISECTION METHOD Input endpoints of starting and function to optimise over %four intervals and Fmin will output as local minimum.
xa=a;xb=b;xm=(a+b)./2;
while abs(xb-xa)>e
F=feval(F,x);
Fa=F(xa),Fb=F(xb),Fm=F(xm);
xl=(xa+xm)./2,xr=(xm+xb)./2;
Fl=F(xl),Fr=F(xr);
y=[Fa,Fb,Fm,Fl,Fr];
Fmin=min(y);
if Fmin==Fa,
xb=xm,xm=xl,Fb=Fm,Fm=Fl;
elseif Fmin==Fl,
xb=xm,xm=xl,Fb=Fm,Fm=Fl;
elseif Fmin==Fb,
xa=xm,xm=xr,Fa=Fm,Fm=Fr;
elseif Fmin==Fr,
xa=xm,xm=xr,Fa=Fm,Fm=Fr;
elseif Fmin==Fm,
xa=xl,xb=xr,Fa=Fl,Fb=Fr;
end
end
fprintf(Fmin)
end
I'm using this for function F:
if true
% code
end
function F=f(x)
F=((x-1).^2).*((x-3).^2).*((x-5).^2)-5.*x;
end
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!