---
title: "Newton's Method Can Fail"
author: "Luke Tierney"
output:
  html_document
---

The function
$$
f(x) = \frac{1}{2} \log(x^2+1) + x  \arctan(x)
$$

is convex with a minimum at $x = 0$:

```{r}
f <- function(x) 1/2 * log(1 + x^2) + x * atan(x)
x <- seq(-5, 5, len = 101)
plot(x, f(x), type = "l")
```

The first derivative is
$$
f'(x) = d_1(x) = \arctan(x)
$$

```{r}
d1 <- atan
plot(x, d1(x), type = "l")
```

The second derivative is
$$
f''(x) = d_2(x) = \frac{1}{1 + x^2}
$$

```{r}
d2 <- function(x) 1 / (1 + x^2)
plot(x, d2(x), type = "l")
```

A single Newton step is taken by

```{r}
ns <- function(x) x - d1(x) / d2(x)
```

A set of `n` steps is returned by

```{r}
run <- function(x, step, n = 10) {
    v <- matrix(0, nrow = n, ncol = length(x))
    for (i in seq_len(n)) {
        x <- step(x)
        v[i, ] <- x
    }
    v
}
```

For a starting point close enough to zero convergence is very fast:

```{r}
run(1.3, ns)
```

Start a little too far away:

```{r}
run(1.4, ns)
```

This plot shows the range of convergence:
```{r}
plot(x, ns(x), type = "l", xlim = c(-2, 2), ylim = c(-5, 5))
lines(x, -x, lty = 2)
```
