Newton’s method
Newton’s method is technique to approximate solutions to equations that are difficult to solve numerically.
A simple applied problem
Here’s a simple applied problem, that doesn’t necessarily have a simple solution.
Statement
The basic set up, which is not particularly hard, is shown in Figure 1. It’s pretty easy to see that we need to find the maximum value of \[ f(x) = x\sin(x) \] over the interval \([0,\pi]\). The image indicates that the maximum occurs a little bigger than \(x\approx2\), maybe? Can we find that exactly, though? If not, can we find at least find a more precise estimate?
Aside on accuracy vs precision
Accuracy and precision are two related but distinct concepts in computational mathematics.
- An estimate is accurate if its actual error is small relative to the needs of a problem.
- An estimate is precise if its implied uncertainty is small.
As an example,
- \(\pi \approx 3.1\) is an accurate estimate of \(\pi\) with low precision,
- \(\pi \approx 3.4567891011\) is a precise estimate of \(\pi\) that’s not very accurate.
Generally, both are desirable but we should clear about which is which.
Start of a solution
It’s pretty easy to see that, as a function of \(x\), the area of the rectangle is \[ A(x) = x\sin(x). \] To use calculus to find the maximum possible area, we try to solve \(A'(x)=0\). That is \[ A'(x) = \sin(x) + x\cos(x) \stackrel{?}{=} 0. \]
You can have a go at that, if you’d like!
Solving the equation on a computer
These days, I guess the first computational tool you might think to try could be a chatbot. Here’s a transcription of Chat GPT’s response to my query on this question:
ChatGPT’s solution
You said:
ChatGPT said:
We want to solve
Step 1: Rearrange the equation
Move one term to the other side:
If , divide both sides by :
Therefore, we need to solve
Step 2: Find the solutions
Notice that is a solution because
To determine whether there are other solutions, observe that the original equation is equivalent to
For , this can be written as
The equation is transcendental, so its solutions generally cannot be expressed in closed form using elementary functions.
Also, the original equation is odd, meaning that if is a solution, then is a solution.
The nonzero solutions can be found numerically. They satisfy
For example, the first positive nonzero solution is approximately
By symmetry, another solution is .
Final answer
The solutions include
These are numerical solutions; there are infinitely many real roots.
You said:
ChatGPT said:
Good question! Unfortunately, there isn't a known exact answer in the form of simple numbers like , , or for the nonzero solutions.
We have
Dividing by , when , gives
Why can't we solve it exactly?
For example, if we try , then , but , so it doesn't work.
If we try , we'd need
which isn't true.
The difficulty is that appears both inside a trigonometric function and outside it. This makes the equation transcendental, so standard algebraic techniques generally can't isolate .
What is the exact answer, then?
We can write the exact solution set implicitly as
The nonzero roots must be approximated numerically, for example,
One important distinction: not having a simple exact form doesn't prove that a root cannot be expressed using some more advanced mathematical function. It means there is no standard elementary closed-form solution.
Using SageMath
It’s not hard and worth understanding how to do this sort of thing with a real computer algebra system, though. There are lots of tools for this, including Mathematica, Matlab, Python, Sympy, and SageMath. I’ve used all of these over the years.
One nice aspect of SageMath is that it can easily be embedded into a webpage. Here’s how to use SageMath to approach this problem, both algebraically, and numerically:
It turns out that SageMath gives us 16 digits of precision. Generally, I would expect almost all of those to be accurate.
Newton’s algorithm
Newton’s method is an algorithm to find numerical approximations to roots of functions; it is the theoretical foundation on which numerical tools like Sage’s find_root works. Given an initial guesss \(x_1\), Newton’s method improves this guess by applying the function \[N(x) = x - \frac{f(x)}{f'(x)}.\] This produces \(x_2 = N(x_1)\). We then plug that back in to get \(x_3\) and continue. More generally, we produce a sequence \((x_k)\) via \(x_k=N(x_{k-1})\).
Example
Let \(f(x) = x^3-x-1\). It’s evident from a graph that there’s one root a little larger than 1.
Newton’s method works by riding the tangent line from an initial guess. If we note that \(x_1=2\) is pretty close to the root, we compute \(x_2 = N(x_2)\), where \[N(x) = x - \frac{f(x)}{f'(x)} = x - \frac{x^3-x-1}{3x^2-1}.\] Thus, \(x_2 = N(2) = 2-5/11 \approx 1.54545\). Geometrically, this point is obtained by riding the tangent line to the \(x\)-axis:
If we do that again, we end up even closer to the root:
That’s why we iterate!
Performing Newton’s method with Sage
Here’s how we can apply Newton’s method to the previous example using Sage:
Performing Newton’s method by hand
A classic problem is to use Newton’s method to approximate \(\sqrt{2}\) by hand. If we stick with fractions, we will generate good rational approximations to \(\sqrt{2}\). The result is equivalent to an algorithm known by the Babylonians.
To derive the technique using Newton’s method, define \(f\) by \[ f(x) = x^2 - 2. \] Clearly, the positive root of \(f\) is exactly \(\sqrt{2}\). Now, \[ N(x) = x - \frac{f(x)}{f'(x)} = x - \frac{x^2-2}{2x} = \frac{x}{2} + \frac{1}{x}. \] Now, let’s take two Newton steps from \(x_0=1\): \[ \begin{aligned} x_1 &= \frac{x_0}{2} + \frac{1}{x_0} = \frac{1}{2} + \frac{1}{1} = \frac{3}{2} \\ x_2 &= \frac{x_1}{2} + \frac{1}{x_1} = \frac{3/2}{2} + \frac{1}{3/2} = \frac{3}{4} + \frac{2}{3} = \frac{17}{12}. \\ \end{aligned} \] Note that \[ \left(\frac{17}{12}\right)^2 = \frac{289}{144} = \frac{288}{144} + \frac{1}{144} = 2 + \frac{1}{144} \] is really close to two! So, \(17/12\) must be really close to \(\sqrt{2}\).
Exercises
- Use a numerical tool to solve the following equations. Be sure to find all solutions
- \(x^5-x-1 = 0\)
- \(x^5-2x-1 = 0\)
- \(\sin(3x) = x/2\)
- For each of the following functons, take three Newton steps from the given initial point
- \(f(x) = x^2 - 2\) from \(x_1 = 2.0\)
- \(f(x) = \sin(x)\) from \(x_1 = 3.0\)
- \(f(x) = x^5-x-1\) from \(x_1 = 1.0\)
- The equation \(\sin(x)=x/9\) has 7 solutions. We want to find an approximation to the largest solution. Use a graph to find a good initial approximation for your \(x_1\) and apply Newton’s method from that point obtain the approximation.