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!