# ## MIT License

# 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.
#

# In[1]:
import numpy as np
import matplotlib.pyplot as pt
# In[2]:
def f(x):
return np.exp(x) - 2
# In[3]:
xgrid = np.linspace(-2, 3, 1000)
pt.grid()
pt.plot(xgrid, f(xgrid))
# What's the true solution of $f(x)=0$?
# In[4]:
xtrue = np.log(2)
print(xtrue)
print(f(xtrue))
# Now let's run the secant method and keep track of the errors:
# In[5]:
errors = []
x = 2
xbefore = 3
# At each iteration, print the current guess and the error.
# In[17]:
slope = (f(x)-f(xbefore))/(x-xbefore)
xbefore = x
x = x - f(x)/slope
print(x)
errors.append(abs(x-xtrue))
print(errors[-1])
# In[18]:
for err in errors:
print(err)
# * Do you have a hypothesis about the order of convergence?
# In[19]:
# Does not quite double the number of digits each round--unclear.
# ------------
# Let's check:
# In[19]:
for i in range(len(errors)-1):
print(errors[i+1]/errors[i]**1.618)
# In[ ]: