# Multivariable Newton's method for-loop

I am struggling with an assignment concerning Newton's method. We are to approximate 3 intersection points using intersecting circle coordinates P1, P2 and P3, using Newton's method. And I can do that for say 1 point at a time with an initial guess. But I need this in a for-loop for all 3 points.

I am struggling with integrating a for-loop, where we for the first iteration use Matrix 1, with column vector 1 as initial guess, and for iteration 2 use our second matrix and second initial guess, and likewise for the third iteration.

You can see my matrices in the cell array A: these are the coordinates I want to use for a0, b0 etc. And the third column features the radii of the circles which form our equations.

And in the matrix G, there are my initial guesses I wish to use for each attempt at solving the system with the associated matrix 1, 2 and 3.

The systems which we solve with newton's method are f and df.

Tl;dr : Need a for-loop in our newton approximation, where iter 1 -> matrix 1, guess 1; and same for 2 and 3.

My code

%constants
n=100; %max iterations
tol = 10^-8;

%cell array matrix for all our circles
A = {
[175, 950, 60; 160, 1008, 45]...%P1 with r1/r2
[410, 2400, 75; 381, 2500, 88]...%P2 with r1/r2
[675, 1730, 42; 656, 1760, 57]...%P3 with r1/r2
};

% matrix for initial guesses
G = [180,950;
400,700;
350,500];

f = @(x) [(x(1)-a0)^2 + (x(2)-b0)^2 - r1^2;
(x(1)-a1)^2 + (x(2)-b1)^2 - r2^2]; % our vector function f

df = @(x) [2 * (x(1)-a0), 2 * (x(2)-b0); % our system's Jacobian
2 * (x(1)-a1), 2 * (x(2)-b1)];

function coord = multinewton(f,df,n,tol)

for i = 1:n
Dv = -df(v)\f(v); % increment Dv
v = v + Dv; % add on to get new guess

if norm(f(v)) < tol
fprintf('Convergence achieved after %d iterations.\n', i);
fprintf('Solution: v = [%f, %f]\n', v);
break; % break the loop if the norm of f(v) is below tolerance
end
end

end


Would really appreciate help on this. Thanks.

$$\begin{cases} \|p-c_1\|^2 = r_1^2 \\ \|p-c_2\|^2 = r_2^2 \\ \|p-c_3\|^2 = r_3^2\end{cases}$$
Setting $$f=(f_1, f_2, f_3)$$ where $$f_i(p) = \|p-c_i\|^2 - r_i^2$$ you may rewrite this system as $$f(p) = 0$$. Now Newton reads $$p_{k+1} = p_k + (J_f(p_k))^{+}f(p_k)$$, where $$M^{+}$$ is e.g. the Moore-Penrose pseudoinverse of $$M$$. If the columns of the Jacobian $$J_f(p)$$ are linearly independent then the Moore-Penrose pseudoinverse is $$(J_f^\top J_f)^{-1}J_f^\top$$ (I guess other generalized inverses should work too in case $$J_f^\top J_f$$ is singular).