trying to use a feval by calling a function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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
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 Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!