# 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
# YOUR CODE HERE
def hat(x, a, b):
    return (-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
x = np.linspace(-3, 3, 100)
plt.plot(x, hat(x, a, b))
```


```python
# YOUR CODE HERE
raise NotImplementedError()
```


```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
# YOUR CODE HERE
#specifies a number of divisions which the function will look for a minimum on each.
#more divisions = more accurate
def minima(divisions, function, a, b):
    critpoints = []
    for n in range(0, divisions):
        sectionx = np.linspace(n*(6/divisions) - 3, (n+1)*(6/divisions) - 3, 100)
        sectiony = function(np.linspace(n*(6/divisions) - 3, (n+1)*(6/divisions) - 3, 100), a, b)
        #make sure the minimum is not on the ends
        if np.amin(sectiony) != sectiony[0] and np.amin(sectiony) != sectiony[-1]:
            minpt = np.argmin(sectiony)
            critpoints.append(sectionx[minpt])
            
    return critpoints
        
```


```python
minpts = minima(100, hat, a, b)
```


```python
x = np.linspace(-3, 3, 100)
plt.plot(x, hat(x, a, b))
plt.scatter(minpts[0], hat(minpts[0], a, b), color = "r")
plt.scatter(minpts[1], hat(minpts[1], a, b), color = "r")
plt.xlabel("X")
plt.ylabel("V(x)")
print("Minimums: ", minpts)
```


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

To find the minima, we can take the derivative of the function and set it to zero, finding the critical points.

\begin{equation}
V'(x) = -2ax + 4bx^3 = 0
\end{equation}

Dividing both sides by x gives us a quadratic, and we can put in the given values of a and b:

\begin{equation}
-10 + 4x^2 = 0
\end{equation}

Solving this gives us the intercepts, where V'(x) = 0.

\begin{equation}
x = \pm \sqrt{\frac{5}{2}}
\end{equation}

Which approximates to:
\begin{equation}
x = \pm 1.5811
\end{equation}

And this agrees with the program's results.


```python

```

dad9934b36477e08755d1a8eda6358c7709e383c
ipynb
Jupyter Notebook
assignments/assignment11/OptimizationEx01.ipynb
SJSlavin/phys202-2015-work
60311479d6a27ca4c530b057036a326e87805b61
MIT
assignments/assignment11/OptimizationEx01.ipynb
SJSlavin/phys202-2015-work
60311479d6a27ca4c530b057036a326e87805b61
MIT
assignments/assignment11/OptimizationEx01.ipynb
SJSlavin/phys202-2015-work
60311479d6a27ca4c530b057036a326e87805b61
MIT
Qwen/Qwen-72B
1. YES
2. YES