Newtons Method

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

08/12/2022 15:06 NewtonsMethod

Newton's method
Solving equations with calculus
It's good to understand that there are many equations (even very simple equations) that cannot be easily solved in closed form. As
two seemingly simple examples, let's take a look at

cos(x) = x , and
x
3
− 3x − 1 = 0 .

It's easy to see that these equations have solutions by simply looking at some graphs. We'll use a couple of Python packages to do
this. Note that you can try the code out yourself at http://try.jupyter.org/! (http://try.jupyter.org/!)

In [1]:

%matplotlib inline
from sympy import *
from sympy.plotting import plot
x = symbols('x')
import warnings
warnings.filterwarnings('ignore')

Here's the graph of the first equation.

In [2]:

plot(cos(x), x, (x,-0.2,1.2))

Out[2]:

<sympy.plotting.plot.Plot at 0x106584748>

That point of intersection shows that there is a solution. Let's ask the computer for a symbolic representation of the solution.

https://marksmath.org/classes/Spring2016CalcI/demos/NewtonsMethod.html 1/3
08/12/2022 15:06 NewtonsMethod

In [3]:

solve(cos(x)-x)

---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-3-7a8e53eb2234> in <module>()
----> 1 solve(cos(x)-x)

/Users/mcmcclur/anaconda3/lib/python3.5/site-packages/sympy/solvers/solvers.py in solve(f, *
symbols, **flags)
907 ###########################################################################
908 if bare_f:
--> 909 solution = _solve(f[0], *symbols, **flags)
910 else:
911 solution = _solve_system(f, symbols, **flags)

/Users/mcmcclur/anaconda3/lib/python3.5/site-packages/sympy/solvers/solvers.py in _solve(f,
*symbols, **flags)
1412 if result is False:
1413 raise NotImplementedError(msg +
-> 1414 "\nNo algorithms are implemented to solve equation %s" % f)
1415
1416 if flags.get('simplify', True):

NotImplementedError: multiple generators [x, cos(x)]


No algorithms are implemented to solve equation -x + cos(x)

Well, that's discouraging! How about a simple cubic polynomial?

In [4]:

plot(x**3-3*x-1, (x,-2,2))

Out[4]:

<sympy.plotting.plot.Plot at 0x1095004e0>

Looks like there are three roots. Let's find them!

https://marksmath.org/classes/Spring2016CalcI/demos/NewtonsMethod.html 2/3
08/12/2022 15:06 NewtonsMethod

In [5]:

init_printing(pretty_print=True)
solve(x**3-3*x-1)

Out[5]:

⎡ −−−−−−−−
– –
1 √3i 1 √3i 1 1 1
⎢(− −
3
)√ + + , + (−
⎢ −−−−−−− −−−−−−−
2 2 2 2 1 √ 3i 3 1 √ 3i 1 √ 3i 3 1 √ 3i 2
⎣ (− − )√ + (− + )√ +
2 2 2 2 2 2 2 2

−− −−−−−−⎤

1 1 √3i 3
+ √ + ⎥
−−−−−− − ⎥
3 1 √ 3i 2 2
√ + ⎦
2 2

Crazy!

Newton's method
Newton'd method tells us that we can approximate the root of a function by iterating the related Newton's method iteration function:
f (x)
n(x) = x − .

f (x)

We should start the iteration near the root we want to find. For example, the previous graph shows that f (x) = x 3 − 3x − 1 has
a root slightly less than 2. If we set x 1 = 2 , x 2 = n(x 1 ), and (generally) x i = f (x i−1 ) we should generate a sequence that
converges to the root. Let's try it!

In [6]:

init_printing(pretty_print=False)
def f(x): return x**3 - 3*x - 1
n = lambdify(x, x-f(x)/diff(f(x),x))
xi = 2.0
cnt = 0
while abs(f(xi)) > 10.0**(-10):
cnt = cnt+1
xi = n(xi)
(xi,cnt)

Out[6]:

(1.8793852415718169, 4)

This tells us that in 4 iterations, Newton's method found an estimate to the solution x i so that |f (x i )|
1
< 1/10 0 - i.e., a residual
accurate to within 10 digits. Not bad!

We can apply this to cos(x) = x by first writing the equation as cos(x) − x = 0 and finding a root of f (x) = cos(x) − x .

In [8]:

def f(x): return cos(x)-x


n = lambdify(x, x-f(x)/diff(f(x),x))
xi = 2.0
cnt = 0
while abs(f(xi)) > 10.0**(-10):
cnt = cnt+1
xi = n(xi)
(xi,cnt)

Out[8]:

(0.7390851332198146, 3)

https://marksmath.org/classes/Spring2016CalcI/demos/NewtonsMethod.html 3/3

You might also like