trying to use a feval by calling a function

조회 수: 1 (최근 30일)
Brooke
Brooke 2011년 3월 3일
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...

답변 (1개)

Matt Fig
Matt Fig 2011년 3월 3일
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
Brooke
Brooke 2011년 3월 3일
wow, thank you. that was stupid that i couldn't get that. thanks again!
Jan
Jan 2011년 3월 3일
Does this mean, that you accept the answer? Then it would be helpful for others to enable the corresponding button.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by