Using fminunc()

8 次查看(过去 30 天)
Kim O
Kim O 2012-5-20
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 ;(

采纳的回答

Sargondjani
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
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 个)

Kim O
Kim O 2012-5-20
Thank you! This funtion was just an example not really my problem. I have a n dimensional algebraic equation f(x,t)=0 with x0 and t0 near a MINIMUM or MAXIMUM. I guess for my Problem fminunc is the wrong method.
Btw:
myfunction=@(x)x(1)+x(2)+sin(x3); Is not working because my function f is 20 dimensional, I can't write it down, this should go automatically, but I don't know how.

Sargondjani
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).

Walter Roberson
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()

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by