我正在尝试在 Matlab 中编写牛顿算法的实现。
当我使用公式调用我的函数时:
result = NewtonMethod(x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4, [0 0], 0.1, 10)
我收到一条错误消息:
??? Undefined function or method 'hessian' for input arguments of type 'double'.
Error in ==> NewtonMethod at 13
H = hessian(f, x0);
我不知道出了什么问题。也许更熟悉Matlab的人可以帮助我。
下面是我的代码:
function xnext = NewtonMethod(f, x0, eps, maxSteps)
% x0 - starting point (2 – dimensional vector)
% H - matrix of second derivatives (Hessian)
% eps - required tolerance of calculations
% maxSteps - length of step
x = x0;
for n=1:maxSteps
% determine the hessian H at the starting point x0,
H = hessian(f, x0);
% determine the gradient of the goal function gradf at the point x,
gradF = gradient(f, x);
% determine next point
xnext = x - inv(H) * x * gradF;
if abs(xnext - x) < eps
return %found
else
x = xnext; %update
end
end
这是我第一次接触 Matlab。
更新:
现在我有一个错误:
??? Error using ==> mupadmex
Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1381
B = mupadmex('symobj::subsref',A.s,inds{:});
我输入:
syms x
result = NewtonMethod(x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4, [0 0], 0.1, 10)