#-----------------------------------------------------------
# Lecture 10 -- Debugging
#-----------------------------------------------------------

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve

# Dr. Tree will solve this problem in class.
#
# Use the fixed point method to find the roots of:
#
# x**3 - 2*x**2 + pi*x + 1 = 0

############################################################
# I purposefully didn't practice this, so I would show     #
# you how I make mistakes and debug when I am programming  #
############################################################




# ---- answer ----
# (I did need to make sure there was an answer though.)

x = np.linspace(-1, 5, 101)

def f(x):
    return x**3 - 5*x**2 + np.pi*x + 1

root1 = fsolve(f, -0.5)[0] # Don't worry about this step.
root2 = fsolve(f, 1.0)[0]  # We will learn fsolve in a few lectures.
root3 = fsolve(f, 4.5)[0]

plt.figure()
plt.plot(x, f(x), 'k-')
plt.plot(root1, f(root1), 'bo')
plt.plot(root2, f(root2), 'ro')
plt.plot(root3, f(root3), 'go')
plt.show()

print('roots: ', np.array([root1, root2, root3]))

# ----------------