Optimization Exercise 1

Imports


In [86]:
%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:


In [116]:
# YOUR CODE HERE
def hat(x, a, b):
    V = -a*(x**2) + b*(x**4)
    return V

In [117]:
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$:


In [118]:
a = 5.0
b = 1.0

In [119]:
# YOUR CODE HERE
x = np.linspace(-3, 3, 25)

plt.figure(figsize=(7, 4))
plt.plot(x, hat(x, a, b))

plt.ylabel('$V(X)$')
plt.xlabel('x')
plt.title('Hat')
plt.grid()



In [120]:
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.

In [142]:
# YOUR CODE HERE
x0 = [-1.5, 1.5]
res1 = opt.minimize(hat, x0[0], args=(a, b))
res2 = opt.minimize(hat, x0[1], args=(a, b))

x1 = res1.x
x2 = res2.x

y1 = np.array(hat(x1, a, b))
y2 = np.array(hat(x2, a, b))


Out[142]:
array([-6.25])

In [153]:
x = np.linspace(-3, 3, 50)

plt.figure(figsize=(9, 5))
plt.plot(x, hat(x, a, b))

plt.ylabel('$V(X)$')
plt.xlabel('x')
plt.title('Hat')

plt.scatter(x1, y1, color='red')
plt.scatter(x2, y2, color='red')
plt.legend('Vm')

plt.grid()



In [ ]:
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.

YOUR ANSWER HERE: The original hat potential function: $$ V(x) = -a x^2 + b x^4 $$

with $ a= 5$ and $b=1$ gives us:

$$ V(x) = -5 x^2 + x^4 $$

Taking the drivative and setting it equal to 0 will give us all points where the slope is zero, thus giving us the local maxima and minima:

$$ V^\prime (x)= \frac{d V}{d x} = 0 = -10 x + 4 x^3$$

Solving for $x$ gives us values of:

$$ x = -\sqrt{\frac{5}{2}}, 0, \sqrt{\frac{5}{2}}$$

By taking the second derivative of $V(x)$ and pluggin in the values of $x$, we can determine which are minima or maxima:

$$ V^{\prime \prime} (x)=\frac{d^2 V}{d x^2} = -10 + 12x^2$$

For the given values of $x$ above, we get: $$ V^{\prime \prime} (0) = -10 $$ $$ V^{\prime \prime} \left(\sqrt{\frac{5}{2}}\right) = 20 $$ $$ V^{\prime \prime} \left(-\sqrt{\frac{5}{2}}\right) = 20 $$ Which implies concave down, up, and up, respectively.

Therfore, the minima are, $x_1 = \sqrt{\frac{5}{2}} \approx 1.581$, and $x_2 = -\sqrt{\frac{5}{2}} \approx -1.581$, which equals what opt.minimize gave us.