# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] nbgrader={}
# # Optimization Exercise 1

# + [markdown] nbgrader={}
# ## Imports

# + nbgrader={}
# %matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
from scipy.optimize import minimize


# + [markdown] nbgrader={}
# ## Hat potential

# + [markdown] nbgrader={}
# The following potential is often used in Physics and other fields to describe symmetry breaking and is often known as the "hat potential":
#
# $$ V(x) = -a x^2 + b x^4 $$
#
# Write a function `hat(x,a,b)` that returns the value of this function:

# + deletable=false nbgrader={"checksum": "6cff4e8e53b15273846c3aecaea84a3d", "solution": true}
def hat(x,a,b):
    return -a*x**2 + b*x**4


# + deletable=false nbgrader={"checksum": "7204bd97cd003430289f171b6ba70d63", "grade": true, "grade_id": "optimizationex01a", "points": 2}
assert hat(0.0, 1.0, 1.0)==0.0
assert hat(0.0, 1.0, 1.0)==0.0
assert hat(1.0, 10.0, 1.0)==-9.0

# + [markdown] nbgrader={}
# Plot this function over the range $x\in\left[-3,3\right]$ with $b=1.0$ and $a=5.0$:

# + nbgrader={}
a = 5.0
b = 1.0

# + deletable=false nbgrader={"checksum": "6cff4e8e53b15273846c3aecaea84a3d", "solution": true}
x = np.linspace(-3,3,100);
plt.figure(figsize=(8,6))
plt.plot(x,hat(x,a,b));
plt.xlabel('x');
plt.ylabel('V(x)');
plt.title('Hat Potential');
plt.tick_params(axis='x',top='off',direction='out');
plt.tick_params(axis='y',right='off',direction='out');

# + deletable=false nbgrader={"checksum": "bd49ce2f030e3366ee640213f26fdaa6", "grade": true, "grade_id": "optimizationex01b", "points": 2}
assert True # leave this to grade the plot

# + [markdown] nbgrader={}
# Write code that finds the two local minima of this function for $b=1.0$ and $a=5.0$.
#
# * Use `scipy.optimize.minimize` to find the minima. You will have to think carefully about how to get this function to find both minima.
# * Print the x values of the minima.
# * Plot the function as a blue line.
# * On the same axes, show the minima as red circles.
# * Customize your visualization to make it beatiful and effective.

# + deletable=false nbgrader={"checksum": "6cff4e8e53b15273846c3aecaea84a3d", "solution": true}
#Finding the Left Minima
guess = (-2)
results = minimize(hat,guess,args=(a,b),method = 'SLSQP')
xL = results.x
print("Left Minima: x = " + str(xL[0]))
# -

#Finding the Right Minima
guess = (2)
results = minimize(hat,guess,args=(a,b),method = 'SLSQP')
xR = results.x
print("Right Minima: x = " + str(xR[0]))

x = np.linspace(-3,3,100);
plt.figure(figsize=(8,6))
plt.plot(x,hat(x,a,b));
plt.xlabel('x');
plt.ylabel('V(x)');
plt.title('Hat Potential with Minimums');
plt.tick_params(axis='x',top='off',direction='out');
plt.tick_params(axis='y',right='off',direction='out');
plt.plot(xL, hat(xL,a,b), marker='o', linestyle='',color='red');
plt.plot(xR, hat(xR,a,b), marker='o', linestyle='',color='red');

# + deletable=false nbgrader={"checksum": "235361d4c954cf9fd6a8ecef309b3a44", "grade": true, "grade_id": "optimizationex01c", "points": 4}
assert True # leave this for grading the plot

# + [markdown] nbgrader={}
# To check your numerical results, find the locations of the minima analytically. Show and describe the steps in your derivation using LaTeX equations. Evaluate the location of the minima using the above parameters.

# + [markdown] deletable=false nbgrader={"checksum": "d7d37614ffa0d469a42ff3fd121335f2", "grade": true, "grade_id": "optimizationex01d", "points": 2, "solution": true}
# $$ V(x) = -a x^2 + b x^4 $$
#
# To Find the minima we set $\frac{\partial V(x)}{\partial x}$ = 0
#
# $$ \frac{\partial V(x)}{\partial x} = -2ax + 4bx^3 = 0 $$
#
# Divide by x on both sides. We know x = 0 is not a minimum. It is a local maximum.
#
# $$ \frac{\partial V(x)}{\partial x} = -2a + 4bx^2 = 0 $$
#
# $$  2a = 4bx^2 $$
#
# $$ x^2 = \frac{2a}{4b} $$
#
# $$ x = \pm\sqrt{\frac{2a}{4b}} $$
#
# $$ x = \pm \sqrt{\frac{5}{2}} = \pm 1.58114 $$
# -



StarcoderdataJupyterScriptsDedupFiltered