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 [5]:
X=np.linspace(-3,3,1000)
f=plt.figure(figsize=(15,10))
plt.plot(X,hat(X,a,b))


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

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 [21]:
x1=opt.minimize(hat,3.0,args=(a,b))
x2=opt.minimize(hat,-3.0,args=(a,b))
print(x1.x, "\n")
print(x2.x)
f=plt.figure(figsize=(15,10))
plt.plot(X,hat(X,a,b))
plt.plot(x1.x,hat(x1.x,a,b), 'ro')
plt.plot(x2.x,hat(x2.x,a,b), 'ro')


[-1.58113883] 

[ 1.58113882]
Out[21]:
[<matplotlib.lines.Line2D at 0x7f790585b208>]

In [22]:
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)=-ax^2+bx^4$$$$\frac{dV}{dx}=-2ax+4bx^3$$$$0=-2ax+4bx^3$$$$0=x(-2a+4bx^2)$$$$x=0, x=\pm\sqrt{\frac{a}{2b}}$$$$a=5.0, b=1.0$$$$x=0, x=\pm\sqrt{\frac{5.0}{2.0}}$$$$x=0, x=\pm 1.5811388$$

In [ ]: