38 lines
788 B
Mathematica
38 lines
788 B
Mathematica
|
|
||
|
|
||
|
syms x y;
|
||
|
f = (1 - x)^2 + 100 * (y - x^2)^2;
|
||
|
|
||
|
[x, xs, gs] = newton(f, [0;0], 50000, 1e-6);
|
||
|
display(x);
|
||
|
display(xs);
|
||
|
display(gs);
|
||
|
plot(xs(1, :), xs(2, :), '-o');
|
||
|
|
||
|
function y = at(f, xk)
|
||
|
syms x y;
|
||
|
y = double(subs(f, [x, y], [xk(1), xk(2)]));
|
||
|
end
|
||
|
|
||
|
function [xk, xs, gs] = newton(f, x0, max_itr, tol)
|
||
|
syms x y;
|
||
|
xk = x0;
|
||
|
gf = gradient(f);
|
||
|
hf = hessian(f, [x, y]);
|
||
|
tnorm = norm(at(gf, xk), 2);
|
||
|
xs = zeros(size(x0, 1), max_itr + 1);
|
||
|
xs(:, 1) = x0;
|
||
|
gs = zeros(1, max_itr + 1);
|
||
|
gs(1) = tnorm;
|
||
|
k = 1;
|
||
|
|
||
|
while tnorm > tol && k <= max_itr + 1
|
||
|
xk = xk - (at(hf, xk) \ at(gf, xk));
|
||
|
tnorm = norm(at(gf, xk), 2);
|
||
|
k = k + 1;
|
||
|
xs(:, k) = xk;
|
||
|
gs(k) = tnorm;
|
||
|
end
|
||
|
xs = xs(:, 1:k);
|
||
|
gs = gs(:, 1:k);
|
||
|
end
|