# Optimization Exercise 1

## Imports


```python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
```

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


```python
def hat(x,a,b):
    return -1*a*(x**2) + b*(x**4)
```


```python
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
```

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


```python
a = 5.0
b = 1.0
```


```python
x = np.linspace(-3,3,100)
v = hat(x,a,b)
graph = plt.plot(x,v)

```


```python
assert True # leave this to grade the plot
```

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.


```python
f = lambda g: hat(g,a,b)
x1 = float(opt.minimize(f,-2 ).x)
x2 = float(opt.minimize(f,2 ).x)
print(x1)
print(x2)
graph = plt.plot(x,v)
plt.plot([x1,x2],[f(x1),f(x2)],'ro')
```


```python
assert True # leave this for grading the plot
```

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.

\begin{align}
V(x) = -a x^2 + b x^4 \\
x(4bx^2-2a) = 0 \\
4bx^2=2a \\
x = \pm \sqrt{\frac{a}{2b}} \\
x = \pm \sqrt{\frac{5}{2(1)}} \approx \pm 1.5811388
\end{align}


5b6244707faf255771727f22e8b628c14aeefd3f
ipynb
Jupyter Notebook
assignments/assignment11/OptimizationEx01.ipynb
joshnsolomon/Josh-Solomon-PHYS-202-work
919bb26416af0e81ca9724d5991e041dbd79d164
MIT
assignments/assignment11/OptimizationEx01.ipynb
joshnsolomon/Josh-Solomon-PHYS-202-work
919bb26416af0e81ca9724d5991e041dbd79d164
MIT
assignments/assignment11/OptimizationEx01.ipynb
joshnsolomon/Josh-Solomon-PHYS-202-work
919bb26416af0e81ca9724d5991e041dbd79d164
MIT
Qwen/Qwen-72B
1. YES
2. YES