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!