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):
    v = -a*x**2 + b*x**4
    return v

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 [5]:
x1 = np.arange(-3,3,0.1)
plt.plot(x1, hat(x1, 5,1))


Out[5]:
[<matplotlib.lines.Line2D at 0x7f20ec0a04e0>]

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 [10]:
def hat(x):
    b = 1
    a = 5
    v = -a*x**2 + b*x**4
    return v

xmin1 = opt.minimize(hat,-1.5)['x'][0]
xmin2 = opt.minimize(hat,1.5)['x'][0]
xmins = np.array([xmin1,xmin2])

print(xmin1)
print(xmin2)

x1 = np.arange(-3,3,0.1)
plt.plot(x1, hat(x1))
plt.scatter(xmins,hat(xmins), c = 'r',marker = 'o')
plt.grid(True)
plt.title('Hat Potential')
plt.xlabel('Range')
plt.ylabel('Potential')


-1.5811388245
1.58113880951
Out[10]:
<matplotlib.text.Text at 0x7f20e58c3ef0>

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.

$$ V(x) = -a x^2 + b x^4 \\ V'(x) = -2ax + 4bx^3 \\ V'(x) = x (-2a + 4bx^2)\\ 4x^2 - 10 = 2(2x^2 - 5) \\ 2(2x^2 - 5) = - \sqrt{10}-2x , \sqrt{10}+2 x \\ $$

The minimums or maximums are at $$ x = \frac{\sqrt{10}}{2}, x = \frac{-\sqrt{10}}{2}, x = 0\\$$ Checking to see if they are a minimum or a maximum:\ $$ V''(x) = -10 + 12x^2\\ V''(0) = -10 + 12(0)^2 = -10\\ $$ x = 0 is a maxima. $$ V''(\frac{\sqrt{10}}{2}) = -10 + 12(\frac{\sqrt{10}}{2})^2 = 350 \\ $$ The x above is a minima. $$ V''(\frac{-\sqrt{10}}{2}) = -10 + 12(\frac{-\sqrt{10}}{2})^2 = 350\\ $$ The x above is a minima.


In [ ]: