trying to use fminbnd to minimize a function with resolution 0.01
2 次查看(过去 30 天)
显示 更早的评论
function [e,t,lf]=f2(q,w)
e=fminbnd(@fun,q,w)
t=(1000/e)-(0.25*pi*e) (%these are final values please ignore them)
lf=(50*pi*r)+(((2*r)+(2*l))*40); (%these are final values please ignore them)
function [c]=fun(r)
r=q:0.01:w;
l=(1000./r)-(0.25*pi.*r);
c=(50*pi.*r)+(((2.*r)+(2.*l))*40);(%trying to minimize this function but i couldnt using fminbnd )
end
end
but im getti
ng these errors
Error using fminbnd (line 220)
User supplied objective function must return a scalar value.
Error in f2 (line 2)
e=fminbnd(@fun,q,w)
trying to learn matlab bascis on my own
please help me in this regard
0 个评论
采纳的回答
Matt J
2020-6-5
编辑:Matt J
2020-6-5
You cannot use fminbnd to guarantee a distance of 0.01 (or any other distance) to the global minimum, but typically you will do much better than that with the default tolerances. To eliminate your fminbnd error message remove the line, r=q:0.01:w,
e=fminbnd(@fun,q,w)
function [c]=fun(r)
%r=q:0.01:w;
l=(1000./r)-(0.25*pi.*r);
c=(50*pi.*r)+(((2.*r)+(2.*l))*40);
end
You can however guarantee a distance of 0.01 if you use exhaustive search, instead of fminbnd. That would look like,
r=q:0.01:w;
[~,minloc]=min(fun(r));
e=r(minloc)
function [c]=fun(r)
l=(1000./r)-(0.25*pi.*r);
c=(50*pi.*r)+(((2.*r)+(2.*l))*40);
end
5 个评论
Matt J
2020-6-5
编辑:Matt J
2020-6-5
It is probably intended that you to set the TolX parameter to 0.01, as in the example below. But note that this is not the same as varying r in increements of 0.01.
q=5;w=10;
e=fminbnd(@fun,q,w,optimset('TolX',0.01))
function [c]=fun(r)
l=(1000./r)-(0.25*pi.*r);
c=(50*pi.*r)+(((2.*r)+(2.*l))*40);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!