<jupyter_start><jupyter_text>Optimization Exercise 1  Imports<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
from scipy.optimize import minimize<jupyter_output><empty_output><jupyter_text>Hat potential 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:<jupyter_code>def hat(x,a,b):
    return -a*x**2 + b*x**4
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<jupyter_output><empty_output><jupyter_text>Plot this function over the range $x\in\left[-3,3\right]$ with $b=1.0$ and $a=5.0$:<jupyter_code>a = 5.0
b = 1.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');
plt.tick_params(axis='x',top='off',direction='out');
plt.tick_params(axis='y',right='off',direction='out');
assert True # leave this to grade the plot<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>#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');
assert True # leave this for grading the plot<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17229585292860197, 'fraction_numerical': 0.026507054296707994, 'mean_word_length': 6.267080745341615, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '313821', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 882, 'n_words': 234, 'path': 'Optimization/OptimizationEx01.ipynb', 'repo_name': 'JAmarel/Phys202', 'size': '2339'}