"""
Homework problem 1
"""

import numpy as np
from matplotlib import pyplot as plt
from scipy import optimize as opt


def washboard(var, tilt):
    return 5*np.cos(var) + np.cos(2*var) - tilt*var


def part_a():
    x = np.linspace(-2, 0, 1000)
    F = 6
    x_max = opt.minimize_scalar(lambda var: -washboard(var, F), bounds=(-1.5, 0), method='bounded').x
    x_min = opt.minimize_scalar(lambda var: washboard(var, F), bounds=(-3, -1), method='bounded').x
    plt.figure()
    plt.grid()
    plt.plot(x, washboard(x, F), label='Washboard potential')
    plt.scatter(x_max, washboard(x_max, F), c='g', label=f'Max = {round(x_max, 7)}')
    plt.scatter(x_min, washboard(x_min, F), c='r', label=f'Min = {round(x_min, 7)}')
    plt.legend()
    plt.show()
    print(x_max)
    print(x_min)
    print(washboard(x_max, F) - washboard(x_min, F))


def part_b():
    def f(vec):
        var, tilt = vec
        return -5 * np.sin(var) - 2 * np.sin(2 * var) - tilt, -5 * np.cos(var) - 4 * np.cos(2 * var)
    rc, Fc = opt.root(f, x0=np.array([-1, 6])).x
    print(rc)
    print(Fc)


if __name__ == '__main__':
    print('COMMIT!!!!')
    part_b()
