Optimization Exercise 1

Imports


In [1]:
%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 [2]:
def hat(x,a,b):
    return -a*x**2+b*x**4

In [3]:
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 [4]:
a = 5.0
b = 1.0

In [12]:
x=np.linspace(-3,3,100)
plt.figure(figsize=(9,6))
plt.xlabel('Range'), plt.ylabel('V(x)'), plt.title('Hat Potential')
plt.plot(x, hat(x,a,b))
plt.box(False)
plt.grid(True)
plt.tick_params(axis='x', top='off', direction='out')
plt.tick_params(axis='y', right='off', direction='out');



In [6]:
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 [9]:
res1 = opt.minimize_scalar(hat, bounds=(-3,0), args=(a,b), method='bounded')
res2 = opt.minimize_scalar(hat, bounds=(0,3), args=(a,b), method='bounded')
print('Local minima: %f, %f' % (res1.x, res2.x))
plt.figure(figsize=(9,6))
plt.xlabel('Range'), plt.ylabel('V(x)')
plt.plot(x, hat(x,a,b), label="Potential")
plt.scatter(res1.x, res1.fun, marker="o", color="r")
plt.scatter(res2.x, res2.fun, marker="o", color="r")
plt.title('Finding Local Minima of Hat Potential')
plt.box(False), plt.grid(True), plt.xlim(-2.5,2.5), plt.ylim(-8,4)
plt.tick_params(axis='x', top='off', direction='out')
plt.tick_params(axis='y', right='off', direction='out')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);


Local minima: -1.581140, 1.581140

In [8]:
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 local minima of the hat potential analytically, I needed to take the first derivative with respect to $x$ and set that equal to zero.

$$ V(x) = -ax^2 + bx^4 $$$$ \frac{dV}{dx} = -2ax + 4bx^3 = 0 $$

A solution we will not use is the $x=0$ because that corresponds to a maximum.

Add $-2ax$ to the other side and cancel out an $x$ to get:

$$ 4bx^2 = 2a $$

Divide by $4b$ and reduce the fraction:

$$ x^2 = \frac{a}{2b} $$

Take the square root:

$$ x = \pm \sqrt{\frac{a}{2b}} $$

Plugging $a=5.0$ and $b=1.0$, we get:

$$ x = -\sqrt{\frac{5}{2}} \: or \: \sqrt{\frac{5}{2}} $$

Or

$$ x = -1.581140 \: or \: 1.581140 $$