trying to use a feval by calling a function
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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...
0 comentarios
Respuestas (1)
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
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.
Ver también
Categorías
Más información sobre Performance and Memory 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!