Newton for nonlinear systems
clear all
format short
set(0, 'defaultaxesfontsize', 12)
set(0, 'defaultlinelinewidth', 1.5)
set(0, 'defaultFunctionLinelinewidth', 1.5)
set(0, 'defaultscattermarkerfacecolor', 'flat')
gcf;
set(gcf, 'Position', [0 0 600 350], 'Theme', 'light')
addpath ../FNC_matlab4.5. Newton for nonlinear systems ¶ The rootfinding problem becomes much more difficult when multiple variables and equations are involved.
Particular problems are often posed using scalar variables and equations.
The steady state of interactions between the population w ( t ) w(t) w ( t ) of a predator species and the population h ( t ) h(t) h ( t ) of a prey species might be modeled as
a h − b h w = 0 , − c w + d w h = 0 , \begin{split}
ah - b h w &= 0, \\
-cw + d w h &= 0,
\end{split} ah − bh w − c w + d w h = 0 , = 0 , for positive parameters a , b , c , d a,b,c,d a , b , c , d . To cast this in the form of (4.5.1) , we could define x = [ h , w ] \mathbf{x}=[h,w] x = [ h , w ] , f 1 ( x 1 , x 2 ) = a x 1 − b x 1 x 2 f_1(x_1,x_2) = ax_1 - bx_1x_2 f 1 ( x 1 , x 2 ) = a x 1 − b x 1 x 2 , and f 2 ( x 1 , x 2 ) = − c x 2 + d x 1 x 2 f_2(x_1,x_2)= -c x_2 + d x_1 x_2 f 2 ( x 1 , x 2 ) = − c x 2 + d x 1 x 2 .
While the equations of Example 4.5.1 are easy to solve by hand, in practice even establishing the existence and uniqueness of solutions for any particular system is typically quite difficult.
4.5.1 Linear model ¶ To extend rootfinding methods to systems, we will keep to the basic philosophy of constructing easily managed models of the exact function. As usual, the starting point is a linear model. We first need to define what it means to take a derivative of a vector-valued function of a vector variable.
A multidimensional Taylor series begins with the linear approximation
f ( x + h ) = f ( x ) + J ( x ) h + O ( ∥ h ∥ 2 ) , \mathbf{f}(\mathbf{x}+\mathbf{h}) = \mathbf{f}(\mathbf{x}) + \mathbf{J}(\mathbf{x})\mathbf{h} + O(\| \mathbf{h} \|^2), f ( x + h ) = f ( x ) + J ( x ) h + O ( ∥ h ∥ 2 ) , where J \mathbf{J} J is the Jacobian matrix of f \mathbf{f} f at x \mathbf{x} x , and h \mathbf{h} h is a small perturbation from x \mathbf{x} x .
The terms f ( x ) + J ( x ) h \mathbf{f}(\mathbf{x})+\mathbf{J}(\mathbf{x})\mathbf{h} f ( x ) + J ( x ) h in (4.5.7) represent the linear part of f \mathbf{f} f near x \mathbf{x} x , while the O ( ∥ h ∥ 2 ) O(\| \mathbf{h} \|^2) O ( ∥ h ∥ 2 ) term represents the omitted higher-order terms in the Taylor series. If f \mathbf{f} f is actually linear, i.e., f ( x ) = A x − b \mathbf{f}(\mathbf{x})=\mathbf{A}\mathbf{x}-\mathbf{b} f ( x ) = Ax − b , then the Jacobian matrix is the constant matrix A \mathbf{A} A and the higher-order terms in (4.5.7) disappear.
Because of the Jacobian’s role in (4.5.7) , we may write J ( x ) \mathbf{J}(\mathbf{x}) J ( x ) as f ′ ( x ) \mathbf{f}{\,}'(\mathbf{x}) f ′ ( x ) . Like any derivative, it is a function of the independent variable x \mathbf{x} x .
4.5.2 The multidimensional Newton iteration ¶ With a method in hand for constructing a linear model for the vector system f ( x ) \mathbf{f}(\mathbf{x}) f ( x ) , we can generalize Newton’s method. Specifically, at a root estimate x k \mathbf{x}_k x k , we set h = x − x k \mathbf{h} = \mathbf{x}-\mathbf{x}_k h = x − x k in (4.5.7) and get
f ( x ) ≈ q ( x ) = f ( x k ) + J ( x k ) ( x − x k ) . \mathbf{f}(\mathbf{x}) \approx \mathbf{q}(\mathbf{x}) = \mathbf{f}(\mathbf{x}_k) + \mathbf{J}(\mathbf{x}_k)(\mathbf{x}-\mathbf{x}_k). f ( x ) ≈ q ( x ) = f ( x k ) + J ( x k ) ( x − x k ) . We define the next iteration value x k + 1 \mathbf{x}_{k+1} x k + 1 by requiring q ( x k + 1 ) = 0 \mathbf{q}(\mathbf{x}_{k+1})=\boldsymbol{0} q ( x k + 1 ) = 0 ,
0 = f ( x k ) + J ( x k ) ( x k + 1 − x k ) , \begin{split}
\boldsymbol{0} &= \mathbf{f}(\mathbf{x}_k) + \mathbf{J}(\mathbf{x}_k)(\mathbf{x}_{k+1}-\mathbf{x}_k),\\
\end{split} 0 = f ( x k ) + J ( x k ) ( x k + 1 − x k ) , which can be rearranged into
x k + 1 = x k − [ J ( x k ) ] − 1 f ( x k ) . \mathbf{x}_{k+1} = \mathbf{x}_k - \bigl[\mathbf{J}(\mathbf{x}_k)\bigr]^{-1} \mathbf{f}(\mathbf{x}_k). x k + 1 = x k − [ J ( x k ) ] − 1 f ( x k ) . Note that J − 1 f \mathbf{J}^{-1}\mathbf{f} J − 1 f now plays the role that f / f ′ f/f' f / f ′ had in the scalar case; in fact, the two are the same in one dimension. In computational practice, however, we don’t compute matrix inverses.
Given f \mathbf{f} f and a starting value x 1 \mathbf{x}_1 x 1 , for each k = 1 , 2 , 3 , … k=1,2,3,\ldots k = 1 , 2 , 3 , …
Compute y k = f ( x k ) \mathbf{y}_k = \mathbf{f}(\mathbf{x}_k) y k = f ( x k ) and A k = f ′ ( x k ) = J ( x k ) \mathbf{A}_k=\mathbf{f\,}'(\mathbf{x}_k)= \mathbf{J}(\mathbf{x}_k) A k = f ′ ( x k ) = J ( x k ) .
Solve the linear system A k s k = − y k \mathbf{A}_k\mathbf{s}_k = -\mathbf{y}_k A k s k = − y k for the Newton step s k \mathbf{s}_k s k .
Let x k + 1 = x k + s k \mathbf{x}_{k+1} = \mathbf{x}_k + \mathbf{s}_k x k + 1 = x k + s k .
An extension of our series analysis of the scalar Newton’s method shows that the vector version is also quadratically convergent in any vector norm, under suitable circumstances and when the iteration converges at all.
4.5.3 Implementation ¶ An implementation of Newton’s method for systems is given in Function 4.5.2 . Other than computing the Newton step using backslash and taking vector magnitudes with norm, Function 4.5.2 is virtually identical to the scalar version Function 4.3.1 presented earlier.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function x = newtonsys(f,x1)
% NEWTONSYS Newton's method for a system of equations.
% Input:
% f function that computes residual and Jacobian matrix
% x1 initial root approximation (n-vector)
% Output
% x array of approximations (one per column, last is best)
% Stopping parameters.
funtol = 1000 * eps; % stop for small || f(x) ||
xtol = 1000 * eps; % stop for small || x change ||
maxiter = 40; % stop after this many iterations
x = x1(:);
[y, J] = f(x1);
dx = Inf;
k = 1;
while (norm(dx) > xtol) && (norm(y) > funtol) && (k < maxiter)
dx = -(J \ y); % Newton step
x(:, k+1) = x(:, k) + dx;
k = k+1;
[y, J] = f(x(:, k));
end
if k==maxiter
warning('Maximum number of iterations reached.')
end
endA system of nonlinear equations is defined by its residual and Jacobian.
This function needs to be defined within a script file or in a file of its own with the .m extension.
function [f, J] = nlsystem(x)
f = zeros(3, 1); % ensure a column vector output
f(1) = exp(x(2) - x(1)) - 2;
f(2) = x(1) * x(2) + x(3);
f(3) = x(2) * x(3) + x(1)^2 - x(2);
J(1, :) = [-exp(x(2) - x(1)), exp(x(2) - x(1)), 0];
J(2, :) = [x(2), x(1), 1];
J(3, :) = [2 * x(1), x(3)-1, x(2)];
endSince our system function is defined in an external file here, we need to use @ in order to reference it as a function argument.
nlsystem = @f45_nlsystem;
x1 = [0; 0; 0]; % column vector!
x = newtonsys(nlsystem, x1);
num_iter = size(x, 2)Let’s compute the residual of the last result in order to check the quality.
r = x(:, end)
back_err = norm(nlsystem(r))We take the sequence errors in the first component of the solution, applying the log so that we can look at the exponents.
log10( abs(x(1, 1:end-1) - r(1)) )'This sequence looks to be nearly doubling at each iteration, which is a good sign of quadratic convergence.
4.5.4 Exercises ¶ Two curves in the ( u , v ) (u,v) ( u , v ) plane are defined implicitly by the equations u log u + v log v = − 0.3 u\log u + v \log v = -0.3 u log u + v log v = − 0.3 and u 4 + v 2 = 1 u^4 + v^2 = 1 u 4 + v 2 = 1 .
(a) ✍ Write the intersection of these curves in the form f ( x ) = 0 \mathbf{f}(\mathbf{x}) = \boldsymbol{0} f ( x ) = 0 for two-dimensional f \mathbf{f} f and x \mathbf{x} x .
(b) ✍ Find the Jacobian matrix of f \mathbf{f} f .
(c) ⌨ Use Function 4.5.2 to find an intersection point starting from u = 1 u=1 u = 1 , v = 0.1 v=0.1 v = 0.1 .
(d) ⌨ Use Function 4.5.2 to find an intersection point starting from u = 0.1 u=0.1 u = 0.1 , v = 1 v=1 v = 1 .
Two elliptical orbits ( x 1 ( t ) , y 1 ( t ) ) (x_1(t),y_1(t)) ( x 1 ( t ) , y 1 ( t )) and ( x 2 ( t ) , y 2 ( t ) ) (x_2(t),y_2(t)) ( x 2 ( t ) , y 2 ( t )) are described by the equations
[ x 1 ( t ) y 1 ( t ) ] = [ − 5 + 10 cos ( t ) 6 sin ( t ) ] , [ x 2 ( t ) y 2 ( t ) ] = [ 8 cos ( t ) 3 + 12 sin ( t ) ] , \begin{bmatrix}
x_1(t) \\ y_1(t)
\end{bmatrix}
=
\begin{bmatrix}
-5+10\cos(t) \\ 6\sin(t)
\end{bmatrix}, \qquad
\begin{bmatrix}
x_2(t)\\y_2(t)
\end{bmatrix} =
\begin{bmatrix}
8\cos(t) \\ 3+12\sin(t)
\end{bmatrix}, [ x 1 ( t ) y 1 ( t ) ] = [ − 5 + 10 cos ( t ) 6 sin ( t ) ] , [ x 2 ( t ) y 2 ( t ) ] = [ 8 cos ( t ) 3 + 12 sin ( t ) ] , where t t t represents time.
(a) ⌨ Make a plot of the two orbits with the following code:
t = linspace(0, 2*pi, 500)';
x1 = -5 + 10*cos(t); y1 = 6*sin(t);
clf
plot(x1, y1); hold on;
x2 = 8*cos(t); y2 = 3 + 12*sin(t);
plot(x2, y2);
axis equal; grid on;(b) ✍ Write out a 2 × 2 2\times 2 2 × 2 nonlinear system of equations that describes an intersection of these orbits. (Note: An intersection is not the same as a collision—they don’t have to occupy the same point at the same time.)
(c) ✍ Write out the Jacobian matrix of this nonlinear system.
(d) ⌨ Use Function 4.5.2 to find all of the unique intersections. Add them to the plot from part (a).
⌨ Suppose one wants to find the points on the ellipsoid x 2 / 25 + y 2 / 16 + z 2 / 9 = 1 x^2/25 + y^2/16 + z^2/9 = 1 x 2 /25 + y 2 /16 + z 2 /9 = 1 that are closest to and farthest from the point ( 5 , 4 , 3 ) (5,4,3) ( 5 , 4 , 3 ) . The method of Lagrange multipliers implies that any such point satisfies
x − 5 = λ x 25 , y − 4 = λ y 16 , z − 3 = λ z 9 , 1 = 1 25 x 2 + 1 16 y 2 + 1 9 z 2 \begin{split}
x-5 &= \frac{\lambda x}{25}, \\[1mm]
y-4 &= \frac{\lambda y}{16}, \\[1mm]
z-3 &= \frac{\lambda z}{9}, \\[1mm]
1 &= \frac{1}{25}x^2 + \frac{1}{16}y^2 + \frac{1}{9}z^2
\end{split} x − 5 y − 4 z − 3 1 = 25 λ x , = 16 λ y , = 9 λ z , = 25 1 x 2 + 16 1 y 2 + 9 1 z 2 for an unknown value of λ \lambda λ .
(a) Write out this system in the form f ( u ) = 0 \mathbf{f}(\mathbf{u}) = \boldsymbol{0} f ( u ) = 0 . (Note that the system has four variables to go with the four equations.)
(b) Write out the Jacobian matrix of this system.
(c) Use Function 4.5.2 with different initial guesses to find the two roots of this system. Which is the closest point to ( 5 , 4 , 3 ) (5,4,3) ( 5 , 4 , 3 ) , and which is the farthest?
⌨ Any three noncollinear points in the plane determine a unique circle. Suppose the points are given as ( x i , y i ) (x_i,y_i) ( x i , y i ) for i = 1 , 2 , 3 i=1,2,3 i = 1 , 2 , 3 . We can define the circle in terms of its center ( a , b ) (a,b) ( a , b ) and radius r r r . Then
f i ( a , b , r ) = ( a − x i ) 2 + ( b − y i ) 2 − r 2 f_i(a,b,r) = (a-x_i)^2 + (b-y_i)^2 - r^2 f i ( a , b , r ) = ( a − x i ) 2 + ( b − y i ) 2 − r 2 should be made zero for all i = 1 , 2 , 3 i=1,2,3 i = 1 , 2 , 3 . This defines a nonlinear system f ( v ) = 0 \mathbf{f}(\mathbf{v})=\boldsymbol{0} f ( v ) = 0 for v = [ a , b , r ] \mathbf{v}=[a,b,r] v = [ a , b , r ] .
Use Function 4.5.2 on this system to find the circle passing through ( − 5 , 0 ) (-5,0) ( − 5 , 0 ) , ( 1 , − 3 ) (1,-3) ( 1 , − 3 ) , and ( 4 , 2 ) (4,2) ( 4 , 2 ) . Make a plot that shows you found the correct circle.