Using fminunc()
8 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I want to use x = fminunc(fun,x0);
For a litle test I choosed following structure:
fun=[x1+x2 ; sin(x3)];
fun==> 2x1 sym
x0 ==> 3x1 double
ERROR:
Error using optimfcnchk (line 288) If FUN is a MATLAB object, it must have an feval method.
Error in fminunc (line 239) funfcn = optimfcnchk(FUN,'fminunc',length(varargin),funValCheck,gradflag,hessflag); Thanks!
Btw: I know, x0 is near the solution BUT I don't know if the solution is a Minimum or Maximum ;(
0 件のコメント
採用された回答
Sargondjani
2012 年 5 月 20 日
what do you want? what is the function you want to optimize for? fminunc searches for a minimum, but for instance x1+x2 is unbounded below (solution: x1=-inf, x2=-inf) and for sin(x3) has infinitely many minima...
anyway the format for fminunc (and fmincon) is the following:
myfunction=@(x)x(1)+x(2)+sin(x3);
x0=[1, 4, 10];
[x,y]=fminunc(myfunction,x0);
note that the my example does not have a solution...
1 件のコメント
Sargondjani
2012 年 5 月 20 日
o and the function your are trying to optimize should return 1 value as output (not 2 as you tried)
その他の回答 (3 件)
Sargondjani
2012 年 5 月 20 日
what do you mean, you can't write it down?
you can create a function file if the function is hard to capture in one line: format is:
function [y]=my_function(x1,x2,...........)
y=.....%put calculations here
end
and store that as a .m-file, where matlab can call it (current directory for instance)
you can then call it with an anonymous function (this way you can also pass parameters):
ano_fun=@(x)my_function(x(1),x(2),....)
fminunc(ano_fun,x0);
if you want to find a local minimum or maximum you can do this with fminunc, you would just have to change the sign, of course (it would be helpful if you new before hand if it is min or max, hehe).
0 件のコメント
Walter Roberson
2012 年 5 月 21 日
fminunc() cannot be applied to symbolic expressions (that is, expressions created by the Symbolic Toolbox.)
You can convert a symbolic expression to a function handle by using matlabFunction()
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Symbolic Math Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!