matlab - fminbnd doesn't give the minimum value -
i'm trying built-in functions in matlab. declared function this:
function y = myfunction(x) y = cos(4*x) .* sin(10*x) .* exp(-abs(x)); end
then use fminbnd
find minimum value:
fminbnd(@myfunction,-pi,pi)
this gives me result:
ans = 0.7768
however, when plot 'myfunction' in [-pi,pi]
, got following figure code used:
>> x = -pi:0.01:pi; >> y = myfunction(x); >> plot(x,y)
it can seen min value -0.77, not result given fminbnd
. what's wrong here? i'm new matlab , don't know i'm wrong.
first things first, fminbnd
returns x
-coordinate of minimum location of function. such, actual minimum located @ myfunction(0.7768)
. x=0.7768
location of minimum is.
now, tried running code more verbose information. specifically, wanted see how minimum changes @ each iteration. overrode default settings of fminbnd
can see what's happening @ each iteration.
this get:
>> y = @(x) cos(4*x).*sin(10*x).*exp(-abs(x)); %// no need function declaration >> options = optimset('display', 'iter'); >> [x,fval,exitflag] = fminbnd(y, -pi, pi, options) func-count x f(x) procedure 1 -0.741629 0.42484 initial 2 0.741629 -0.42484 golden 3 1.65833 -0.137356 golden 4 0.775457 -0.457857 parabolic 5 1.09264 0.112139 parabolic 6 0.896609 -0.163049 golden 7 0.780727 -0.457493 parabolic 8 0.7768 -0.457905 parabolic 9 0.776766 -0.457905 parabolic 10 0.776833 -0.457905 parabolic optimization terminated: current x satisfies termination criteria using options.tolx of 1.000000e-04 x = 0.776799595407872 fval = -0.457905463395071 exitflag = 1
x
location of minimum, fval
y
value of minimum , exitflag=1
means algorithm converged properly.
this not equal desired minimum. if can reference documentation of fminbnd
, says this:
fminbnd may give local solutions.
going that, reason why aren't getting right answer because have a lot of local minima in function. specifically, if zoom in x=0.7784
local minimum:
since algorithm managed find local minimum here, decides stop.
managed true minimum if restrict search boundaries of function around true minimum is. instead of [-pi,pi]
... try [-1,1]
instead:
>> [x,fval,exitflag] = fminbnd(y, -1, 1, options) func-count x f(x) procedure 1 -0.236068 -0.325949 initial 2 0.236068 0.325949 golden 3 -0.527864 -0.256217 golden 4 -0.32561 0.0218758 parabolic 5 -0.0557281 -0.487837 golden 6 0.0557281 0.487837 golden 7 -0.124612 -0.734908 golden 8 -0.134743 -0.731415 parabolic 9 -0.126213 -0.735006 parabolic 10 -0.126055 -0.735007 parabolic 11 -0.126022 -0.735007 parabolic 12 -0.126089 -0.735007 parabolic optimization terminated: current x satisfies termination criteria using options.tolx of 1.000000e-04 x = -0.126055418940111 fval = -0.735007134768142 exitflag = 1
when did this, managed right minimum location , minimum itself.
Comments
Post a Comment