%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
#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
StaticInteract(plot, Ex=RangeWidget(0., 1., 1.), Ey=RangeWidget(0., 1., 1.))
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
plt.plot(xposn,yposn)
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
#plt.hold(True)
#plt.hold()
plt.subplot(121)
plt.plot(xposn,yposn)
plt.subplot(122)
plt.plot(xposn1,yposn1)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(xposn,yposn)
ax2.plot(xposn1,yposn1)
plt.show()
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