Optimization Exercise 1

Imports


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
from scipy.optimize import minimize

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,100);
plt.figure(figsize=(8,6))
plt.plot(x,hat(x,a,b));
plt.xlabel('x');
plt.ylabel('V(x)');
plt.title('Hat Potential');
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 [12]:
#Finding the Left Minima
guess = (-2)
results = minimize(hat,guess,args=(a,b),method = 'SLSQP')
xL = results.x
print("Left Minima: x = " + str(xL[0]))


Left Minima: x = -1.58113717494

In [13]:
#Finding the Right Minima
guess = (2)
results = minimize(hat,guess,args=(a,b),method = 'SLSQP')
xR = results.x
print("Right Minima: x = " + str(xR[0]))


Right Minima: x = 1.58113715467

In [18]:
x = np.linspace(-3,3,100);
plt.figure(figsize=(8,6))
plt.plot(x,hat(x,a,b));
plt.xlabel('x');
plt.ylabel('V(x)');
plt.title('Hat Potential with Minimums');
plt.tick_params(axis='x',top='off',direction='out');
plt.tick_params(axis='y',right='off',direction='out');
plt.plot(xL, hat(xL,a,b), marker='o', linestyle='',color='red');
plt.plot(xR, hat(xR,a,b), marker='o', linestyle='',color='red');



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.

$$ V(x) = -a x^2 + b x^4 $$

To Find the minima we set $\frac{\partial V(x)}{\partial x}$ = 0

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

Divide by x on both sides. We know x = 0 is not a minimum. It is a local maximum.

$$ \frac{\partial V(x)}{\partial x} = -2a + 4bx^2 = 0 $$$$ 2a = 4bx^2 $$$$ x^2 = \frac{2a}{4b} $$$$ x = \pm\sqrt{\frac{2a}{4b}} $$$$ x = \pm \sqrt{\frac{5}{2}} = \pm 1.58114 $$

In [ ]: