Week 5

Notes

Continued Fractions

Consider the following sequence of floats: $$ x_0 = 1; \quad x_n = \frac{1}{1+x_{n-1}}. $$ What do you predict will happen to this sequence as $ n\rightarrow \infty $?

Exercise:

Write a function in Python that will take a natural number $ N $ as input, and returns the list $$ \left[x_0,x_1,x_2,\dotsc,x_N\right]. $$

In [1]:
def cont_frac(N):
    sequence = [1]
    x = 1
    for i in range(N):
        x = 1/(1+x)
        sequence.append(x)
    return sequence
In [4]:
cont_frac(10)
Out[4]:
[1,
 0.5,
 0.6666666666666666,
 0.6000000000000001,
 0.625,
 0.6153846153846154,
 0.6190476190476191,
 0.6176470588235294,
 0.6181818181818182,
 0.6179775280898876,
 0.6180555555555556]

Plot the sequence. What do you observe? Does this match your prediction?

In [5]:
import matplotlib.pyplot as plt
In [6]:
inputs = [i for i in range(101)]
outputs = cont_frac(100)
plt.plot(inputs,outputs)
plt.show()

Changing parameters

Consider the same sequence, except with a different starting value. $$ x_0 = \;? $$ What happens to the behavior of the sequence if we change the starting value?

Exercise:

Modify your function to make the starting value of the sequence as additional input. Then plot the sequence for a variety of different starting values.

In [7]:
def cont_frac(x,N):
    sequence = [x]
    for i in range(N):
        x = 1/(1+x)
        sequence.append(x)
    return sequence
In [24]:
cont_frac(-3,10)
Out[24]:
[-3,
 -0.5,
 2.0,
 0.3333333333333333,
 0.75,
 0.5714285714285714,
 0.6363636363636364,
 0.6111111111111112,
 0.6206896551724138,
 0.6170212765957447,
 0.6184210526315789]

Describe the effect of changing the starting value on the behavior of the sequence.

To get a better understanding of the effect of changing the starting value, consider each term in the sequence to be a function of the starting value. $$ \left[x_0(x_0),x_1(x_0),x_2(x_0),\dotsc,x_N(x_0)\right] $$ A graph of $ x_N(x_0) $ for $ x_0 $ in the interval [0,5] would show us how the last term in the sequence changes as we make incremental changes to the initial value.

Exercise:

Plot the function $ x_{N}(x_0) $ on the interval [0,5] and describe what you see.

In [11]:
import matplotlib.pyplot as plt
In [21]:
inputs = [i/5 for i in range(251)]  # Values of x_0 between 0 and 5
outputs = [cont_frac(x_0,7)[-1] for x_0 in inputs] # values of x_10 corresponding to the values of x_0 between 0 and 5
plt.plot(inputs,outputs)
plt.xlabel('Initial value of the sequence')
plt.ylabel('Nth term in the sequence')
plt.show()

New parameters and bifurcation

Bifurcation is a mathematical term used to describe a change in behavior. It is uaually used to describe how a mathematical model's behavior splits into two or more divergent patterns as one of the model's parameters is changed.

To explore this concept, let's introduce some new parameters to our sequence of continued fractions. $$ x_0 = \;?, \quad x_n = \frac{a}{b+x_{n-1}} $$ I would like to explore the effects of these new parameters on the behavior of the sequence.

Exercise:

Update your function once again to include the new parameters as inputs.

In [25]:
def cont_frac(a,b,x,N):
    sequence = [x]
    for i in range(N):
        x = a/(b+x)
        sequence.append(x)
    return sequence
In [34]:
cont_frac(-3,1,1,20)
Out[34]:
[1,
 -1.5,
 6.0,
 -0.42857142857142855,
 -5.25,
 0.7058823529411765,
 -1.7586206896551722,
 3.954545454545456,
 -0.6055045871559631,
 -7.604651162790694,
 0.4542253521126763,
 -2.0629539951573848,
 2.822323462414579,
 -0.7848629320619784,
 -13.944598337950131,
 0.23175690134817048,
 -2.435545517720639,
 2.0897978942272784,
 -0.9709372919196271,
 -103.22506738544459,
 0.029347009268170538]

Now plot $ x_N(a) $ for a reasonable range of values for $ a $.

In [48]:
inputs = [i/10 for i in range(101)]  # Values for the parameter a between 0 and 10
outputs = [cont_frac(a,5,1,5)[-1] for a in inputs] # values of x_N corresponding to the values of x_0 between 0 and 5
plt.plot(inputs,outputs)
plt.show()

Let's make this graph a little more interesting. Pick a handful of values for $ b $, say 6 different values, and plot the functions $ x_N(a,b_1),x_N(a,b_2),\dotsc,x_N(a,b_6) $ all on the same pair of axes. This will give a glimpse at how the sequence behaves as both $ a $ and $ b $ change.

In [53]:
inputs = [i/10 for i in range(1,101)]  # Values for the parameter a between 0 and 10
for b in range(6):
    outputs = [cont_frac(a,b,1,10)[-1] for a in inputs] # values of x_N corresponding to the values of x_0 between 0 and 5
    plt.plot(inputs,outputs)
plt.legend(['$b={}$'.format(i) for i in range(6)])
plt.show()

Now repeat the above steps for the other parameter $ b $.

In [ ]:
 
In [ ]: