trying to use a feval by calling a function

1 visualización (últimos 30 días)
Brooke
Brooke el 3 de Mzo. de 2011
Im trying to evaluate an objective function (f), its gradient (gf), and Hessian (H) by using feval. however, I keep getting an error saying that "x" is undefined. below is the function i have created to be passed to feval.
function [f,gf,H] = func(x); f = x(1)-x(2)+2.*x(1).*x(2)+2.*x(1).^2+x(2).^2; gf = [1+2.*x(2)+4.*x(1);-1+2.*x(1)+2.*x(2)]; H = [4 2;2 2]; end
Then, the function I have that calls this and has the feval is as follows
function myopt(func,x); % % evaluate function 'func' and its gradient and hessian [f,gf,H] = feval(func,x); end
From the command line I call myopt(func,[1 2]) and receive the error. Can someone help me? I'm sure it's a simple fix...

Respuestas (1)

Matt Fig
Matt Fig el 3 de Mzo. de 2011
Try:
myopt('func',[1 2])
.
.
.
By the way, it is generally preferrable to use function handles instead of strings and FEVAL. For example, you could change MYOPT to this:
function myopt(func,x);
% % evaluate function 'func' and its gradient and hessian
[f,gf,H] = func(x)
end
Then call it like this:
myopt(@func,[1 2])
  2 comentarios
Brooke
Brooke el 3 de Mzo. de 2011
wow, thank you. that was stupid that i couldn't get that. thanks again!
Jan
Jan el 3 de Mzo. de 2011
Does this mean, that you accept the answer? Then it would be helpful for others to enable the corresponding button.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by