Optimization Exercise 1

Imports


In [33]:
%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)
plt.plot(x, hat(x,a,b))


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

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 [41]:
min1 = opt.minimize(hat, x0 =-1.7,args=(a,b))
min2=opt.minimize(hat, x0 =1.7, args=(a,b))
print(min1,min2)


   status: 0
     nfev: 18
      fun: -6.25
  message: 'Optimization terminated successfully.'
 hess_inv: array([[ 0.04999981]])
     njev: 6
  success: True
        x: array([-1.58113883])
      jac: array([  2.38418579e-07])    status: 0
     nfev: 18
      fun: -6.249999999999999
  message: 'Optimization terminated successfully.'
 hess_inv: array([[ 0.05000113]])
     njev: 6
  success: True
        x: array([ 1.58113882])
      jac: array([ -1.19209290e-07])

In [40]:
print('Our minimas are x=-1.58113883 and x=1.58113882')


Our minimas are x=-1.58113883 and x=1.58113882

In [39]:
plt.figure(figsize=(7,5))
plt.plot(x,hat(x,a,b), color = 'b',label='hat potential')
plt.box(False)
plt.title('Hat Potential')
plt.scatter(x=-1.58113883,y=hat(x=-1.58113883,a=5,b=1), color='r', label='min1')
plt.scatter(x=1.58113883,y=hat(x=-1.58113883,a=5,b=1), color='r',label='min2')
plt.legend()


Out[39]:
<matplotlib.legend.Legend at 0x7f6b5bddadd8>

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) = -5 x^2 + 1 x^4 $$

Take the derivative

$$ \frac{dV}{dx} = -10x + 4x^3 $$

set derivative to 0 and solve for x

$$ 0 = (-10+4x^2)x $$

critical points are $$x = 0 $$ and $$ x=\sqrt\frac{10}{4} $$ and $$ x=-\sqrt\frac{10}{4} $$

Check concavity by taking the second derivative

$$ \frac{d^2V}{dx^2} = - 10 + 12 x^2 $$

At x = 0, concavity is negative so local maxima is at x=0.

At x= $$ \sqrt\frac{10}{4} $$ concavity is positive, so they are the local minimas.


In [ ]: