I want to generate two graphs that show a solution of a simple harmonic oscillator. I'm running it in the latest version of Spyder.
I have two figures in this code which I have set-up to run in separate pop-up windows. The windows pop-up but the figures are not displaying.
What is the problem?
It seems like figure two also stops responding after I run so is it an issue with my computer instead of the code?
Thanks
#!/usr/bin/env python3
# Verifying a solution of the simple harmonic oscillator equation by
# establishing convergence to an exact solution.
import matplotlib.pyplot as plt
import numpy
k = 1.0 # Spring constant
m = 1.0 # Mass
cycles = 2.0 # No. of periods to integrate over
x0 = 1.0 # Initial displacement
v0 = 0.0 # Initial velocity
def leapfrog( steps ):
"""Solve the simple harmonic motion equations for several oscillation cycles,
assuming that the mass (m) and spring constant (k) are defined in the
global space.
"""
omega = (k/m)**0.5
delta = 2.0*cycles*numpy.pi/omega/steps
x = numpy.empty( steps+1 )
v = numpy.empty( steps+1 )
t = numpy.empty( steps+1 )
t[0] = 0.0
x[0] = x0
v[0] = v0 + 0.5*delta*x[0]*omega**2.0
for i in range(steps):
t[i+1] = t[i] + delta
v[i+1] = v[i] - omega**2*delta*x[i]
x[i+1] = x[i] + delta*v[i+1]
return t, x, v
def l2_error_norm( t , x ):
"""Calculate the L2 relative error norm."""
steps = len( x ) - 1
omega = (k/m)**0.5
l2_err = 0.0
l2 = 0.0
for i in range(steps):
x_exact = x0*numpy.cos( omega*t[i] )
l2_err += (x[i] - x_exact)**2.0
l2 += x[i]**2.0
return (l2_err/l2)**0.5
plt.switch_backend( 'TkAgg' )
# This loop integrates the SHM equations repeatedly using an increasing
# number of steps (doubling at each loop iteration).
n = 14
steps = 8
l2_error = numpy.empty( n )
delta = numpy.empty( n )
for i in range(0,n):
t, x, v = leapfrog( steps )
delta[i] = (k/m)**0.5*(t[1]-t[0])
l2_error[i] = l2_error_norm( t , x )
plt.plot( t , x )
#plt.show( block=False )
steps *= 2
plt.figure()
plt.loglog( delta , l2_error , 'o' )
plt.loglog( delta , l2_error[0]*(delta/delta[0])**1.0 )
plt.loglog( delta , l2_error[0]*(delta/delta[0])**2.0 )
plt.loglog( delta , l2_error[0]*(delta/delta[0])**3.0 )
plt.show()