In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
In [2]:
h = 0.1

#assume alpha = 1
def fn(k,m,a,y,dy):
    return -k*y/m -a*dy/m

def euler_step(k,m,a,y,dy):
    F1 = dy
    f1 = fn(k,m,a,y,dy)
    return [f1*h, F1*h] # [dy, y] update

def main(k,m,a):
    y = 0
    dy = 1
    i = 0
    ylist = []
    while i<100:
        array = euler_step(k,m,a,y,dy)
        dy += array[0]
        y += array[1]
        #print y,dy
        ylist.append(y)
        i +=1
    return ylist

def plot(k,m,a):
    fig, ax = plt.subplots(figsize=(4, 3),
                           subplot_kw={'axisbg':'#EEEEEE',
                                       'axisbelow':True})
    ax.grid(color='w', linewidth=2, linestyle='solid')
    alist = main(k,m,a)
    ax.plot(alist, lw=5, alpha=0.4, label = a*a-k*m)
    ax.legend(loc='upper right')
    #ax.set_xlim(0,2*np.pi)
    #ax.set_ylim(-1, 1)
    return fig
In [4]:
StaticInteract(plot, k=RangeWidget(1., 4., 1.), m=RangeWidget(1., 4., 1.), a=RangeWidget(1., 4., 1.))
Out[4]:
a:
k:
m:
In []: