trying to use fminbnd to minimize a function with resolution 0.01

1 回表示 (過去 30 日間)
AJAY CHANDRA DORAGARI
AJAY CHANDRA DORAGARI 2020 年 6 月 5 日
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

採用された回答

Matt J
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
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
AJAY CHANDRA DORAGARI
AJAY CHANDRA DORAGARI 2020 年 6 月 5 日
yes sir, it might be asking to find with a tolerence of 0.01.
thank you sir
thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by