In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
In [10]:
#Ex = 0
#Ey = 5

m = 1
q = 1

dt = 0.1

def main(Ex,Ey):
    def force():
        return [Ex,Ey]

#    def euler_step(x,Vx,y,Vy):
#        array = force()
#        Fy = Vy
#        fy = array[1]
#        Fx = Vx
#        fx = array[0]
#        return [fx*dt, Fx*dt, fy*dt, Fy*dt]

    def rk2_step(x,dx,y,dy):
#        array = force()
#        F1y = Vy
#        f1y = array[1]
#        F2y = Vy
#        fy = array[1]
#        F1x = Vx
#        f1x = array[0]
        F1x = dy
        f1x = fn(k,m,y)
        F2x = dy +f1*h
        f2x = fn(k,m,(y+F1*h))
        return [(f1+f2)*h/2., (F1+F2)*h/2.] # [dy, y] update

    i = 0
    
    x = 0
    y = 0
    Vx = 1
    Vy = 1

    xposn = []
    yposn = []

    while i<100:
        array = euler_step(x,Vx,y,Vy)
    #        array = rk2_step(k,m,y,dy)
        Vx += array[0]
        x += array[1]
        Vy += array[2]
        y += array[3]
        #print y,dy
        xposn.append(x)
        yposn.append(y)
        i +=1
    return [xposn,yposn]
    
def plot(Ex,Ey):
    fig, ax = plt.subplots(figsize=(4, 3),
                           subplot_kw={'axisbg':'#EEEEEE',
                                       'axisbelow':True})
    ax.grid(color='w', linewidth=2, linestyle='solid')
    alist = main(Ex,Ey)
    xlist = alist[0]
    ylist = alist[1]
    ax.plot(xlist, ylist, lw=5, alpha=0.4)#, label = Ex/Ey)
    ax.legend(loc='upper right')
    #ax.set_xlim(0,2*np.pi)
    #ax.set_ylim(-1, 1)
    return fig
In [15]:
StaticInteract(plot, Ex=RangeWidget(0., 1., 1.), Ey=RangeWidget(0., 1., 1.))
Out[15]:
Ex:
Ey:

testing

In [2]:
Bz = 1

m = 1
q = 1

x = 0
y = 0
Vx = 1
Vy = 0

dt = 0.1

xposn = []
yposn = []

def force():
    return [Vy*Bz,-Vx*Bz]

def euler_step(x,Vx,y,Vy):
    array = force()
    Fx = Vx
    fx = array[0]
    Fy = Vy
    fy = array[1]
    return [fx*dt, Fx*dt, fy*dt, Fy*dt]

i = 0
while i<100:
    array = euler_step(x,Vx,y,Vy)
#        array = rk2_step(k,m,y,dy)
    Vx += array[0]
    x += array[1]
    Vy += array[2]
    y += array[3]
    #print y,dy
    xposn.append(x)
    yposn.append(y)
    i +=1
In [3]:
plt.plot(xposn,yposn)
Out[3]:
[<matplotlib.lines.Line2D at 0xb13715ac>]
In []:
 
In [7]:
Bz = 1

m = 1
q = 1

x = 0
y = 0
Vx = 1
Vy = 0

dt = 0.1

xposn = []
yposn = []

def force():
    return [Vy*Bz,-Vx*Bz]

#def euler_step(x,Vx,y,Vy):
#    array = force()
#    Fx = Vx
#    fx = array[0]
#    Fy = Vy
#    fy = array[1]
#    return [fx*dt, Fx*dt, fy*dt, Fy*dt]

def rk2_step(k,m,y,dy):
    array = force()
#    
    F1x = Vx
    f1x = array[0]
    F2x = Vx +f1x*dt
    f2x = array[0]
#    
    F1y = Vy
    f1y = array[1]
    F2y = Vy +f1y*dt
    f2y = array[1]
#    
    return [(f1x+f2x)*dt/2., (F1x+F2x)*dt/2., (f1y+f2y)*dt/2., (F1y+F2y)*dt/2.] # [dy, y] update
#    return [fx*dt, Fx*dt, fy*dt, Fy*dt]

i = 0
while i<100:
    array = rk2_step(x,Vx,y,Vy)
#        array = rk2_step(k,m,y,dy)
    Vx += array[0]
    x += array[1]
    Vy += array[2]
    y += array[3]
    #print y,dy
    xposn.append(x)
    yposn.append(y)
    i +=1
In [18]:
#plt.hold(True)
#plt.hold()
plt.subplot(121)
plt.plot(xposn,yposn)
plt.subplot(122)
plt.plot(xposn1,yposn1)
Out[18]:
[<matplotlib.lines.Line2D at 0xb0f932ec>]
In [22]:
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(xposn,yposn)
ax2.plot(xposn1,yposn1)
plt.show()
In []:
 
In [17]:
Bz = 1

m = 1
q = 4

x = 0
y = 0
Vx = 1
Vy = 0

dt = 0.1

xposn1 = []
yposn1 = []

def force():
    return [(q/m)*Vy*Bz,-(q/m)*Vx*Bz]

#def euler_step(x,Vx,y,Vy):
#    array = force()
#    Fx = Vx
#    fx = array[0]
#    Fy = Vy
#    fy = array[1]
#    return [fx*dt, Fx*dt, fy*dt, Fy*dt]

def rk2_step(k,m,y,dy):
    array = force()
#    
    F1x = Vx
    f1x = array[0]
    F2x = Vx +f1x*dt
    f2x = array[0]
#    
    F1y = Vy
    f1y = array[1]
    F2y = Vy +f1y*dt
    f2y = array[1]
#    
    return [(f1x+f2x)*dt/2., (F1x+F2x)*dt/2., (f1y+f2y)*dt/2., (F1y+F2y)*dt/2.] # [dy, y] update
#    return [fx*dt, Fx*dt, fy*dt, Fy*dt]

i = 0
while i<10:
    array = rk2_step(x,Vx,y,Vy)
#        array = rk2_step(k,m,y,dy)
    Vx += array[0]
    x += array[1]
    Vy += array[2]
    y += array[3]
    #print y,dy
    xposn1.append(x)
    yposn1.append(y)
    i +=1
In []:
 
In []: