def plot_pvt(): astate = analytic_spring_ode(times, state[-1]) dstate = list(state) for t in times[1:]: dstate.append(rk4(discrete_spring_ode, t, dt, dstate[-1])) dstate = array(dstate) agreement = astate[:,0] - dstate[:,0] fig, ax = subplots() agmark, = ax.plot(times, agreement, 'k--', lw=1, alpha=.7) amark, = ax.plot(times, astate[:,0], 'b-', lw=6) dmark, = ax.plot(times, dstate[:,0], 'r-', lw=2) ax.legend( [amark, dmark, agmark], [r'Analytic $\left(\mathcal{A}_t\right)$', r'Discrete $\left(\mathcal{D}_t\right)$', r'Agreement $\left(\mathcal{A}_t-\mathcal{D}_t\right)$'], numpoints=1, loc="lower right") configure( ax=ax, title=r"$k=1.0$ $\frac{1}{seconds^2}$; $m=1.0$ $gram(s)$; $v_0=0.0$ $\frac{meter(s)}{second}$", xlabel="Time (seconds)\n$\\Delta t=" + str(dt) + "$", ylabel="Spatial Displacement (meters)\n$x_0="+str(x0)+"$", xbounds=(t0, tf), ybounds=(-1.,1.)) fig.suptitle("Simple Harmonic Oscillator", size=30) fig.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.08)
def plot_samples_and_models(times, pos, vel, acc): F1 = [array([pos[0], vel[0]])] F2 = [array([pos[0], vel[0]])] for t,dt,p,v in zip(times,dts,pos,vel)[1:]: F1.append(rk4(falld1, t, dt, F1[-1])) F2.append(rk4(falld2, t, dt, F2[-1])) F1 = array(F1) F2 = array(F2) fig, axes = subplots(2) pmark, = axes[0].plot(times, pos, 'b--o') plmark, = axes[0].plot(times[:-2], F1[:,0], 'g--o') pqmark, = axes[0].plot(times[:-2], F2[:,0], 'r--o') axes[0].legend( [pmark, plmark, pqmark], [r'$\left(t,p\right)$', r'Linear', r'Quadratic'], numpoints=1, loc="upper right") configure( ax=axes[0], title=r'Position vs Time $\left(v_t='+ str(vT)+ r'\right)$', xlabel=r"Time $\left(seconds\right)$", ylabel=r"Position $\left(meters\right)$", xbounds=None, ybounds=(0,.5)) vmark, = axes[1].plot(times[:-2], vel, 'b--o') vlmark, = axes[1].plot(times[:-2], F1[:,1], 'g--o') vqmark, = axes[1].plot(times[:-2], F2[:,1], 'r--o') axes[1].legend( [vmark, vlmark, vqmark], [r'$\left(t,v\right)$', r'Linear', r'Quadratic'], numpoints=1, loc="upper right") configure( ax=axes[1], title=r'Velocity vs Time $\left(v_t='+str(vT)+r'\right)$', xlabel=r"Time $\left(seconds\right)$", ylabel=r"Velocity $\left(\frac{meters}{second}\right)$", xbounds=None, ybounds=None) fig.suptitle("Falling Coffee Filter", size=30) fig.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.08) show()
def plot_evt(dt, integral, suptitle='Simple Harmonic Oscillator'): # Do this once. a_state = analytic_spring_ode(times, state[-1]) a_energy = spring_energy(times, a_state) d_state = list(state) if integral != predict_correct: for t in times[1:]: d_state.append(integral(discrete_spring_ode, t, dt, d_state[-1])) else: d_times, d_state = predict_correct(discrete_spring_ode, list(times), dt, d_state) d_state = array(d_state) d_energy = spring_energy(times, d_state) fig, ax = subplots() amark, = ax.plot(times, a_energy, 'b-', lw=6) if integral == predict_correct: print len(times); print len(d_energy) dmark, = ax.plot(d_times, d_energy[:-2], 'r-', lw=2) else: print len(times); print len(d_energy) dmark, = ax.plot(times, d_energy, 'r-', lw=2) ax.legend( [amark, dmark], [r'Analytic $\left(\mathcal{A}_t\right)$', r'Discrete $\left(\mathcal{D}_t\right)$'], numpoints=1, loc="lower right") configure( ax=ax, title=r"$k=1.0$ $\frac{1}{seconds^2}$; $m=1.0$ $gram(s)$; $v_0=0.0$ $\frac{meter(s)}{second}$", xlabel="Time (seconds)\n$\\Delta t=" + str(dt) + "$", ylabel="Energy (joules)\n$E_0="+str(E0)+"$", xbounds=None, ybounds=(0.49,0.51)) error = abs(E0 - d_energy[-1]) / E0 print error fig.suptitle(suptitle, size=30) fig.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.08)
def plot_pc(): a_state = analytic_spring_ode(times, state[-1]) pc_times, pc_state = predict_correct(discrete_spring_ode,times,dt,state) fig, ax = subplots() amark, = ax.plot(times, a_state[:,0], 'b-', lw=6) pcmark, = ax.plot(pc_times, pc_state[1:-1,0], 'r-', lw=2) ax.legend( [amark, pcmark], [r'Analytic $\left(\mathcal{A}_t\right)$', r'Predictor-Corrector $\left(\mathcal{D}_t\right)$'], numpoints=1, loc="lower right") configure( ax=ax, title=r"$k=1.0$ $\frac{1}{seconds^2}$; $m=1.0$ $gram(s)$; $v_0=0.0$ $\frac{meter(s)}{second}$", xlabel="Time (seconds)\n$\\Delta t=" + str(dt) + "$", ylabel="Position (meters)\n$x_0="+str(x0)+"$", xbounds=(t0,tf), ybounds=None) fig.suptitle("Predictor Corrector", size=30) fig.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.08)
def plot_acc_vs_vel(vel,acc): fig, ax = plt.subplots() avmark, = ax.plot(vel, acc, 'b--o') start_avmark, = ax.plot(vel[0], acc[0], 'go', ms=10) end_avmark, = ax.plot(vel[-1], acc[-1], 'ro', ms=10) ax.legend( [avmark, start_avmark, end_avmark], [r'$\left(v,a\right)$', r'First', r'Last'], numpoints=1, loc="lower right") configure( ax=ax, title="Acceleration vs Velocity", xlabel=r"Velocity $\left(\frac{meters}{second}\right)$", ylabel=r"Acceleration $\left(\frac{meters}{seconds^2}\right)$", xbounds=None, ybounds=(-10,10)) fig.suptitle("Falling Coffee Filter", size=30) fig.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.08) show()
def plot_samples(times, pos, vel, acc): fig, axes = subplots(2, 2) ptmark, = axes[0,0].plot(times, pos, 'b--o') start_ptmark, = axes[0,0].plot(times[0], pos[0], 'go', ms=10) end_ptmark, = axes[0,0].plot(times[-1], pos[-1], 'ro', ms=10) axes[0,0].legend( [ptmark, start_ptmark, end_ptmark], [r'$\left(t,p\right)$', r'First', r'Last'], numpoints=1, loc="upper right") configure( ax=axes[0,0], title="Position vs Time", xlabel=r"Time $\left(seconds\right)$", ylabel=r"Position $\left(meters\right)$", xbounds=None, ybounds=None) vtmark, = axes[1,0].plot(times[1:-1], vel, 'b--o') start_vtmark, = axes[1,0].plot(times[1], vel[0], 'go', ms=10) end_vtmark, = axes[1,0].plot(times[-2], vel[-1], 'ro', ms=10) axes[1,0].legend( [vtmark, start_vtmark, end_vtmark], [r'$\left(t,v\right)$', r'First', r'Last'], numpoints=1, loc="upper right") configure( ax=axes[1,0], title="Velocity vs Time", xlabel=r"Time $\left(seconds\right)$", ylabel=r"Velocity $\left(\frac{meters}{second}\right)$", xbounds=None, ybounds=None) atmark, = axes[0,1].plot(times[1:-1], acc, 'b--o') start_atmark, = axes[0,1].plot(times[1], acc[0], 'go', ms=10) end_atmark, = axes[0,1].plot(times[-2], acc[-1], 'ro', ms=10) axes[0,1].legend( [atmark, start_atmark, end_atmark], [r'$\left(t,a\right)$', r'First', r'Last'], numpoints=1, loc="lower right") configure( ax=axes[0,1], title="Acceleration vs Time", xlabel=r"Time $\left(seconds\right)$", ylabel=r"Acceleration $\left(\frac{meters}{seconds^2}\right)$", xbounds=None, ybounds=(-10,10)) avmark, = axes[1,1].plot(vel, acc, 'b--o') start_avmark, = axes[1,1].plot(vel[0], acc[0], 'go', ms=10) end_avmark, = axes[1,1].plot(vel[-1], acc[-1], 'ro', ms=10) axes[1,1].legend( [avmark, start_avmark, end_avmark], [r'$\left(v,a\right)$', r'First', r'Last'], numpoints=1, loc="lower right") configure( ax=axes[1,1], title="Acceleration vs Velocity", xlabel=r"Velocity $\left(\frac{meters}{second}\right)$", ylabel=r"Acceleration $\left(\frac{meters}{seconds^2}\right)$", xbounds=None, ybounds=(-10,10)) fig.suptitle("Falling Coffee Filter", size=30) fig.subplots_adjust(left=0.05, right=0.95, top=0.9, bottom=0.08) show()