Esempio n. 1
0
def go():
    xlf, vlf = 1.5, 0.0
    xe, ve = 1.5, 0.0
    xrk2, vrk2 = 1.5, 0.0

    t, h = 0.0, 0.1
    tt = []
    xlf_array, vlf_array = [], []
    xe_array, ve_array = [], []
    xrk2_array, vrk2_array = [], []
    energy_lf, energy_e, energy_rk2 = [], [], []

    while t < 4 * np.pi:
        xlf_array.append(xlf)
        vlf_array.append(vlf)
        energy_lf.append(0.5 * m * vlf**2 + 0.5 * m * w**2 * xlf**2)
        xlf, vlf = ode.leapfrog(oscillator, xlf, vlf, t, h)

        xe_array.append(xe)
        ve_array.append(ve)
        energy_e.append(0.5 * m * ve**2 + 0.5 * m * w**2 * xe**2)
        xe, ve = ode.Euler(oscillator_rk, [xe, ve], t, h)

        xrk2_array.append(xrk2)
        vrk2_array.append(vrk2)
        energy_rk2.append(0.5 * m * vrk2**2 + 0.5 * m * w**2 * xrk2**2)
        xrk2, vrk2 = ode.rk2(oscillator_rk, [xrk2, vrk2], t, h)

        tt.append(t)
        t += h

    plt.figure(1)
    plt.plot(xlf_array, vlf_array, '--', label='Leapfrog')
    plt.plot(xe_array, ve_array, label='Euler')
    plt.plot(xrk2_array, vrk2_array, '.', label='RK2')
    plt.xlabel('x (m)')
    plt.ylabel('v (m/s)')
    plt.legend(loc='best')
    plt.show()

    plt.figure(2)
    plt.semilogy(tt, energy_lf, '--', label='Leapfrog')
    plt.semilogy(tt, energy_e, label='Euler')
    plt.semilogy(tt, energy_rk2, '.', label='RK2')
    plt.xlabel('Time (s)')
    plt.ylabel('Total Energy (J)')
    plt.legend(loc='best')
    plt.grid()
    plt.show()
Esempio n. 2
0
def go():
    xrk2, vrk2 = 5, -1

    t, h = 0.0, 0.05
    tt = []

    xrk2_array, vrk2_array = [], []

    while t < 10:

        xrk2_array.append(xrk2)
        vrk2_array.append(vrk2)
        xrk2, vrk2 = ode.rk2(electron_ode, [xrk2, vrk2], t, h)

        tt.append(t)
        t += h

    plt.figure(1)
    plt.plot(tt, xrk2_array, '.', label='RK2')
    plt.xlabel('Time (s)')
    plt.ylabel('x (m)')
    plt.legend(loc='best')
    plt.show()

    plt.figure(2)
    plt.plot(tt, vrk2_array, '.', label='RK2')
    plt.xlabel('Time (s)')
    plt.ylabel('v (m/s)')
    plt.legend(loc='best')
    plt.show()

    plt.figure(3)
    plt.plot(xrk2_array, vrk2_array, '.', label='RK2')
    plt.xlabel('x (m)')
    plt.ylabel('v (m/s)')
    plt.legend(loc='best')
    plt.show()
Esempio n. 3
0
    dydt[0] = y[2]
    dydt[1] = y[3]
    dydt[2] = 0
    dydt[3] = -g

    return dydt


ball = vp.sphere(pos=vp.vector(-20, 0, 0), radius=0.5, color=vp.color.yellow)
floor = vp.box(pos=vp.vector(0, -5, 0), length=50, height=0.2, width=4)

t = 0
h = 0.01
vx = 10 * np.cos(np.deg2rad(30))
vy = 10 * np.sin(np.deg2rad(30))
g = 9.8

while True:
    vp.rate(200)
    #y = ode.Euler(freefall, [ball.pos.y, v], t, h)
    y = ode.rk2(projectile, [ball.pos.x, ball.pos.y, vx, vy], t, h)

    ball.pos.x = y[0]
    ball.pos.y = y[1]
    vx = y[2]
    vy = y[3]

    if ball.pos.y > floor.pos.y + ball.radius:
        vy = vy - 9.8 * h
    else:
        vy = -vy
Esempio n. 4
0
def exact_sol(t):
    return 1000 * np.exp(-lamb * t)


t = 0
h = 0.5
y_euler, y_rk2, y_rk4, y_rk45n = 1000, 1000, 1000, 1000

tt, exsol = [], []
nuclear_euler, nuclear_rk2, nuclear_rk4, nuclear_rk45n = [], [], [], []
abserr_euler, abserr_rk2, abserr_rk4, abserr_rk45n = [], [], [], []
clf_euler, clf_rk2, clf_rk4, clf_rk45n = [], [], [], []

while t <= 8:
    yy_euler = ode.Euler(decay, [y_euler], t, h)
    yy_rk2 = ode.rk2(decay, [y_rk2], t, h)
    yy_rk4 = ode.rk4(decay, [y_rk4], t, h)
    yy_rk45n = ode.RK45n(decay, [y_rk45n], t, h)

    exs = exact_sol(t)

    tt.append(t)
    exsol.append(exs)

    nuclear_euler.append(yy_euler[0])
    nuclear_rk2.append(yy_rk2[0])
    nuclear_rk4.append(yy_rk4[0])
    nuclear_rk45n.append(yy_rk45n[0])
    abserr_euler.append(abs(yy_euler[0] - exs))
    abserr_rk2.append(abs(yy_rk2[0] - exs))
    abserr_rk4.append(abs(yy_rk4[0] - exs))
Esempio n. 5
0
    return dydt


t = 0
h = 0.01
x = 0
y = 0
vx = 10 * np.cos(np.deg2rad(30))
vy = 10 * np.sin(np.deg2rad(30))
g = 9.8

tt, posx, posy, velx, vely = [], [], [], [], []

while t <= 0.5:
    #y = ode.Euler(freefall, [ball.pos.y, v], t, h)
    yy = ode.rk2(projectile, [x, y, vx, vy], t, h)

    tt.append(t)
    posx.append(x)
    posy.append(y)
    velx.append(vx)
    vely.append(vy)

    x = yy[0]
    y = yy[1]
    vx = yy[2]
    vy = yy[3]
    t += h

    if y > -5.5:
        vy = vy - 9.8 * h
Esempio n. 6
0
floor = vp.box(pos = vp.vector(0, -5, 0), length = 8, height = 0.2, width = 4)

m = 1
t = 0
h = 0.01
v = 0.0
g = 9.8
pe = []
ke = []
tt = []


while (t <= 1):
    vp.rate(400)
    #y = ode.Euler(freefall, [ball.pos.y, v], t, h)
    y = ode.rk2(freefall, [ball.pos.y, v], t, h)
    t = t + h/2

    ball.pos.y = y[0]
    v = y[1]
    tt.append(t)
    pe.append(m*g*ball.pos.y)
    ke.append(0.5*m*v**2)

    if ball.pos.y > floor.pos.y + ball.radius:
        v = v - 9.8*h
    else:
        v = -v
    
    plt.figure()
    plt.plot(tt, pe, label = 'Potential Energy')