Optimization Exercise 1

Imports


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

In [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

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


In [5]:
a = 5.0
b = 1.0

In [6]:
x = np.linspace(-3,3,61)
plt.plot(x, hat(x,a,b));



In [7]:
hat(x,a,b)


Out[7]:
array([ 36.    ,  28.6781,  22.2656,  16.6941,  11.8976,   7.8125,
         4.3776,   1.5341,  -0.7744,  -2.6019,  -4.    ,  -5.0179,
        -5.7024,  -6.0979,  -6.2464,  -6.1875,  -5.9584,  -5.5939,
        -5.1264,  -4.5859,  -4.    ,  -3.3939,  -2.7904,  -2.2099,
        -1.6704,  -1.1875,  -0.7744,  -0.4419,  -0.1984,  -0.0499,
         0.    ,  -0.0499,  -0.1984,  -0.4419,  -0.7744,  -1.1875,
        -1.6704,  -2.2099,  -2.7904,  -3.3939,  -4.    ,  -4.5859,
        -5.1264,  -5.5939,  -5.9584,  -6.1875,  -6.2464,  -6.0979,
        -5.7024,  -5.0179,  -4.    ,  -2.6019,  -0.7744,   1.5341,
         4.3776,   7.8125,  11.8976,  16.6941,  22.2656,  28.6781,  36.    ])

In [8]:
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 [10]:
x0 = [-1.5,1.5]
a = 5.0
b = 1.0
x1 = opt.minimize(lambda x,a,b: -a*x**2 + b*x**4, x0[0], args=(a,b)).x
x2 = opt.minimize(lambda x,a,b: -a*x**2 + b*x**4, x0[1], args=(a,b)).x
print(x1,x2)


[-1.58113882] [ 1.58113881]

In [11]:
plt.plot(x, hat(x,a,b))
plt.plot(x1, hat(x1,a,b), 'ro')
plt.plot(x2, hat(x2,a,b), 'ro');



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

Find derivative of hat potential, set that equal to zero, then solve for x. $$$$ $$V = -ax^2+bx^4$$ $$$$ $$\frac{dV}{dx} = -2ax+4bx^3$$ $$$$ $$\frac{dV}{dx} = -2x\left(a-2bx^2\right)$$ $$$$ $$-2x = 0,\: 2bx^2 = a$$ $$$$ $$x = 0,\: x = \sqrt{\frac{a}{2b}}$$ $$$$ For the plot above, we used $a=5.0$ and $b=1.0$. Plugging those in, we get: $$$$ $$x=\pm 1.58113883$$ $$$$ To determine which are minima, we can look at the the second derivative and see if it is positive at those locations. $$$$ $$\frac{d^2V}{dx^2} = -2a+12bx^2$$ $$$$ $$x=0:\: \frac{d^2V}{dx^2} = -2a = -10 < 0$$ $$$$ $$x=\pm 1.58113883:\: \frac{d^2V}{dx^2} = -2a+12b\left(\sqrt{\frac{a}{2b}}\right)^2 = -2a+6a = 20 > 0$$ $$$$ So $x= \pm 1.58113883$ are minima!