#!/usr/bin/env python
# coding: utf-8

# # Rates of Convergence
# 
# Copyright (C) 2020 Andreas Kloeckner
# 
# <details>
# <summary>MIT License</summary>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# </details>

# In[1]:


import numpy as np


# In[2]:


C = 1/2
e0 = 0.1*np.random.rand()

rate = 1


# In[3]:


e = e0
for i in range(20):
    print(e)
    e = C*e**rate


# * What do you observe about the number of iterations it takes to decrease the error by a factor of 10 for `rate=1,2,3`?
# * Is there a point to faster than cubic convergence?

# ------------------
# Now let's see if we can figure out the convergence order from the data.
# 
# Here's a function that spits out some fake errors of a process that converges to $q$th order:

# In[4]:


def make_rate_q_errors(q, e0, n=10, C=0.7):
    errors = []
    e = e0
    for i in range(n):
        errors.append(e)
        e = C*e**q
        
    return errors


# In[5]:


errors = make_rate_q_errors(1, e0)


# In[6]:


for e in errors:
    print(e)


# In[7]:


for i in range(len(errors)-1):
    print(errors[i+1]/errors[i])

