Description


The RNotebook Newtons Method f: R^2 -> R^1 illustrates how to use the custom package newtonsmethod for finding a zero (if it exists) of a function that maps points from \(\mathbb{R}^{2}\) to points in \(\mathbb{R}^{1}\).

Copyright (C) 2018 Crista Moreno

Newtons Method f: R^2 -> R^1 is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program.  
If not, see <https://www.gnu.org/licenses/>.
library("rgl")
library("knitr")
library("plot3D")
knit_hooks$set(webgl = hook_webgl)

Download the newtonsmethod package and record the path to its directory for installation.

In this example, the newtonsmethod package is located in a directory called Packages.

devtools::install("../Packages/newtonsmethod")

Load the newtonsmethod library.

library("newtonsmethod")

The Derivative


Newton’s Method uses the derivative of a function to find a zero of the function.

\[f: \mathbb{R}^{2} \rightarrow \mathbb{R}\]

\[Df:\mathbb{R}^{2} \rightarrow L(\mathbb{R}^{2}, \mathbb{R})\]

\[\text{Let } \mathbb{x} \in \mathbb{R}^{2}, \mathbb{x} = (x_{1}, x_{2}) \]

\[Df(\mathbb{x}):\mathbb{R}^{2} \rightarrow \mathbb{R}\]

Example


\[f: \mathbb{R}^{2} \rightarrow \mathbb{R}\]

\[f(\mathbb{x}) = \sin(x_{1}) + \cos(x_{2})\]

f <- function(x) {
  sin(x[1]) + cos(x[2])
}

\[Df:\mathbb{R}^{2} \rightarrow L(\mathbb{R}^{2}, \mathbb{R})\]

\[Df(\mathbb{x}) = \left[\cos(x_{1}), -\sin(x_{2})\right]\]

Df <- function(x) {
  matrix(c(cos(x[1]), -sin(x[2])), nrow = 1, ncol=2)
}

Visualization of \(g\)


g <- function(x, y) {
  cos(x) + sin(y)
}

zero_plane <- function(x, y) {
  x*0 + y*0
}

x <- seq(0, 2*pi, 0.1)
y <- seq(0, 2*pi, 0.1)

V2 <- outer(x, y, FUN = g)

col <- rainbow(10)[cut(V2, breaks = 10)]
P_0 <- outer(x, y, FUN = zero_plane)
#open3d()
surface3d(x, y, P_0, col = "pink", alpha=0.5)
function_surface <- surface3d(x, y, V2, col = col)
rglwidget(elementId = "functiong")

The transparent pink plane is the zero plane, and where the surface intersects the plane is where the zeros of the function are. The function \(f\) has infinitely many zeroes, the illustration above shows four lines of zeros that touch six zeroes on the edges of the surface. Two of those lines intersect at a zero on the middle of the surface.

Results


Application of Newton’s Method with the newtonsmethod library and the function newtons_method for the function \(f\) at the initial point \(\mathbb{x}_{0} = (0, 0)\).

x_0 <- c(0,0)
p <- newtons_method(x_0, f, Df)
p
## [1] -1.569722  0.000000

To check if the point \(p\) returned by newtonsmethod gives a zero of the function, we can compute \(f(p)\).

format(f(p), scientific = FALSE)
## [1] "0.0000005770331"

The result is not zero, but very close to zero. This is because the newtonsmethod package has an absolute error of 1e-6, and the algorithm will come to a halt as soon as this level of error is reached.