Using fminunc()
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Sargondjani
el 20 de Mayo de 2012
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 comentario
Sargondjani
el 20 de Mayo de 2012
o and the function your are trying to optimize should return 1 value as output (not 2 as you tried)
Más respuestas (3)
Sargondjani
el 20 de Mayo de 2012
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 comentarios
Walter Roberson
el 21 de Mayo de 2012
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 comentarios
Ver también
Categorías
Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!