HW 2

We are given $f(x)=\sin(1/x)$ and we're asked to find the largest fixed point $x_0$ using several techniques.

Problem 1

Sketch a graph of $f$ together with the line $y=x$ to get a rough idea as to the value of $x_0$.

Answer: This is a pretty basic problem for matplotlib. Note that I'm starting the plot domain at $x$ slightly larger than zero to avoid a division eror and that I'm using a ridiculous number of points in my linspace command due to the ridiculous number of times that the function oscillates.

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

def f(x): return np.sin(1/x)
x = np.linspace(0.001,1,10000)
y = f(x)
plt.plot(x,y)
plt.plot(x,x)
Out[1]:
[<matplotlib.lines.Line2D at 0x10cfc2588>]

It looks like the largest fixed point is between $0.8$ and $1$.

Problem 2

Find $x_0$ using fixed point iteration.

Solution: Emulating the code we have on our web page for finding roots, we get something like so:

In [2]:
x = 1
cnt = 0
while np.abs(f(x)-x)>10**-15 and cnt < 100:
    x = f(x)
    cnt = cnt+1
x,cnt
Out[2]:
(0.89753946128048678, 55)

It looks like we found the fixed point to be approximately $0.8975$ in $55$ iterates.

Comment: Let's push this a bit further and think about why it took so many iterates. The derivative of $f$ is

$$f'(x) = -\frac{\cos(1/x)}{x^2}$$

so

$$f'(0.89) \approx -0.5459.$$

Now, we oughtta be able to get a rough idea as to how many iterates to expect by solving the inequality

$$0.5459^n < 10^{-15},$$

which yields $n>57$.

Problem 3

How many iterates would we expect the bisection method to take?

Solution: Like the comment in the problem before, we simply solve an inequality, namely,

$$0.5^n < 10^{-15}$$

to get $n>49.8$ or approximately $n>50$.

One weakness of the bisection method is that this is completely independent of the function so there's no obvious way to improve it.

Problem 4

I guess this is just a matter of following the Newton's method code in our root finding notebook.

In [3]:
def f(x): return np.sin(1/x)-x
def n(x): return x + (-x + np.sin(1/x))/(1 + np.cos(1/x)/x**2)
x = 1
cnt = 0
while np.abs(f(x))>10**-10 and cnt<10:
    x = n(x)
    cnt = cnt+1
x,cnt
Out[3]:
(0.89753946128048723, 3)

Problem 5

I guess we gotta just redo with one extra character.

In [4]:
x = 0.1
cnt = 0
while np.abs(f(x))>10**-10 and cnt<10:
    x = n(x)
    cnt = cnt+1
x,cnt
Out[4]:
(0.10732788726105187, 4)