def anim_orb(x): fig, ax = plt.subplots() R_max = np.max(R) + 1 line = [ax.plot([], [])[0] for _ in range(n)] line.extend([ax.plot([], [], "ok")[0] for _ in range(n)]) line.append(ax.plot([], [], "oy")[0]) ax.set_ylim((-R_max, R_max)) ax.set_xlim((-R_max, R_max)) frameskip = 12 tail_length = int(1 / dt) def animate(frame): frame = frame * frameskip + tail_length total = [] for i in range(n): xi = x[frame - tail_length:frame, i, 0] line[i].set_data(xi[:, 0], xi[:, 1]) line[n + i].set_data(xi[-1, 0], xi[-1, 1]) line[-1].set_data(0, 0) return line a = anim(fig, animate, interval=1) plt.show()
# (x, y, z)' = f(x, y, z) def f(x, y, z): s = 10 r = 28 b = 8 / 5. return np.array([-s * x + s * y, -x * z + r * x - y, x * y - b * z], dtype=np.float) def setAx(ax): ax.cla() ax.set_zlim(10, 40) ax.axis((-20, 20, -20, 20)) ax.set_axis_off() def y(n): global ys, ye, ax setAx(ax) ys = np.concatenate([ys, [RKStep(f, ys[-1], A, b, 0.01)]]) ye = np.concatenate([ye, [RKStep(f, ye[-1], Ae, be, 0.01)]]) return ax.plot(*ys.T) + ax.plot(*ye.T) if __name__ == "__main__": ys = np.array([np.array([10, 0.1, 25.])]) ye = np.copy(ys) fig = plt.figure() ax = Axes3D(fig) a = anim(fig, y, interval=10, blit=True) plt.show()
def runn(): p = Kepler(v0=8.5) p.solve(N=200) # Gráficos plt.style.use('dark_background') fig = plt.figure(figsize=(14, 8)) gs = fig.add_gridspec(1, 2) ax1 = fig.add_subplot(gs[0, 0], polar=True) ax1.plot(p.th, p.r, 'g--', 'origem') plt.scatter(820, 2.3, s=350, color='yellow') ptraj, = ax1.plot([p.th[0]], [p.r[0]], 'ro') ## ax2 eh mais importante ax2 = fig.add_subplot(gs[0, 1], polar=False) r, V, Vcf, Vef = p.Energia_graficos(rmin=.2, rmax=p.r.max() + 8) ax2.axvline(0, color='purple') ax2.axhline(0, color='purple') ax2.set_xlabel('$r$ (ua)') ax2.set_ylabel('Energias') ax2.set_ylim(-40, 40) ax2.plot(r, V, 'g--', label=f'$V(r)$') ax2.plot(r, Vcf, 'y--', label=f'$V_{{cf}}(r)$') ax2.plot(r, Vef, 'b', label=f'$V_{{ef}}(r)$') T, = ax2.plot([], [], 'k-', label=f'$T_{{ef}}(\\dot r)$') ax2.plot(p.r, p.E, label=f'$E$') E, = ax2.plot(p.r[0], p.E[0], 'ro', markersize=3) V, = ax2.plot(p.r[0], p.V[0], 'ro', markersize=3) Vef, = ax2.plot(p.r[0], p.Vef[0], 'ro', markersize=3) Vcf, = ax2.plot(p.r[0], p.Vcf[0], 'ro', markersize=3) tempo = ax2.text(.65, .02, 't = 0', transform=ax2.transAxes) plt.legend(loc='upper right') def updatefig(i): global n, pause if not pause: ptraj.set_data([p.th[n]], [p.r[n]]) T.set_data([p.r[n], p.r[n]], [p.E[n], p.Vef[n]]) V.set_data(p.r[n], p.V[n]) Vcf.set_data(p.r[n], p.Vcf[n]) Vef.set_data(p.r[n], p.Vef[n]) E.set_data(p.r[n], p.E[n]) tempo.set_text(f't={p.t[n]:.2f} anos') #rt.set_data([p.t[n]], [p.r[n]]) #tht.set_data([p.t[n]], [p.th[n]]) #rp.set_data([p.t[n]], [p.rp[n]]) #thp.set_data([p.t[n]], [p.thp[n]]) n = n + 1 if n < p.t.size - 1 else 0 return ptraj, tempo, Vef, Vcf, V, E, T def onClick(event): global pause pause ^= True fig.canvas.mpl_connect('button_press_event', onClick) n = 0 a = anim(fig, updatefig, frames=p.t.size, interval=2, blit=True) fig.tight_layout() plt.show()
global l1, l2 x1 = l1 * sin(b[0][0]) y1 = -l1 * cos(b[0][0]) x2 = x1 + l2 * sin(b[1][0]) y2 = y1 - l2 * cos(b[1][0]) return ([0, x1, x2], [0, y1, y2]) fig = plt.figure() ax = fig.add_subplot(111, xlim=(-2.5, 2.5), ylim=(-2.5, 2.5)) lines = [ax.plot([], [], "o-") for i in range(N)] def animate(n): global bs # Several timesteps increas prec. w/o need for high framerate / slow motion for i in range(100): bs = updateState(bs) for i in range(N): lines[i][0].set_data(*getXY(bs[i])) return lines a = anim(fig, animate, frames=2000) if os.path.isfile("dp_frames"): shutil.rmtree("dp_frames") a.save("dp.html", fps=24)
def run_mhs(): class dp_mola: def __init__(self, mu=.5, k=2): self.mu = mu self.k = k self.w2 = k / mu self.tau = 2 * np.pi / np.sqrt(self.w2) def EDO(self, Y, t): x, y, xp, yp = Y dx = xp dy = yp dxp = -self.w2 * x dyp = -self.w2 * y return dx, dy, dxp, dyp def set_CI(self, x0=1., y0=0, v0x=0, v0y=1): self.Y0 = [x0, y0, v0x, v0y] self.L = self.mu * (x0 * v0y - y0 * v0x) def solve(self, tmax=None, N=1000): if tmax is None: tmax = self.tau self.t = np.linspace(0, tmax, N) self.Y = spi.odeint(self.EDO, self.Y0, self.t) self.x, self.y, self.vx, self.vy = self.Y.T self.r2 = self.x**2 + self.y**2 self.v2 = self.vx**2 + self.vy**2 self.r = np.sqrt(self.r2) self.v = np.sqrt(self.v2) self.th = np.fmod(np.arctan2(self.y, self.x), 2 * np.pi) self.th[self.th < 0] = self.th[self.th < 0] + 2 * np.pi self.thp = self.L / self.mu / self.r2 self.rp = np.sqrt(self.v2 - (self.r * self.thp)**2) self.Energia() def Energia(self): self.T = .5 * self.mu * self.v2 self.V = .5 * self.k * self.r2 self.E = self.T + self.V self.Vcf = .5 * self.L**2 / self.mu / self.r2 self.Veq = self.V + self.Vcf def Energia_graficos(self, N=200, rmin=.3, rmax=1.6): r = np.linspace(rmin, rmax, N) V = .5 * self.k * r**2 Vcf = .5 * (self.L / r)**2 / self.mu Veq = V + Vcf return r, V, Vcf, Veq s = dp_mola() s.set_CI() s.solve() # gráficos style.use('dark_background') fig = plt.figure(figsize=(10, 6)) gs = fig.add_gridspec(2, 1) ax2 = fig.add_subplot(gs[0, 0]) ax2.set_xlabel('$r$') ax2.set_ylabel('Energias') r, V, Vcf, Veq = s.Energia_graficos() ax2.plot(r, V, '--', label='V') ax2.plot(r, Vcf, '--', label='Vcf') ax2.plot(r, Veq, 'b', label='Veq') Teq, = ax2.plot([s.r[0], s.r[0]], [s.Veq[0], s.E[0]], 'b-', marker='o', markerfacecolor='red', label='Teq') ax2.plot(s.r, s.E, label='E') V, = ax2.plot(s.r[0], s.V[0], 'ro') Vcf, = ax2.plot(s.r[0], s.Vcf[0], 'ro') plt.legend() ax3 = fig.add_subplot(gs[1, 0]) ax3.set_xlabel('$t$') ax3.set_ylabel('$r$') ax3.plot(s.t, s.r, 'b--') rplot, = ax3.plot(s.t[0], s.r[0], 'ro') def updatefig(i): global n if not pause: Teq.set_data([s.r[n], s.r[n]], [s.Veq[n], s.E[n]]) V.set_data(s.r[n], s.V[n]) Vcf.set_data(s.r[n], s.Vcf[n]) rplot.set_data(s.t[n], s.r[n]) n = n + 1 if n < s.t.size - 1 else 0 return Teq, Vcf, V, rplot def onClick(event): global pause pause ^= True fig.canvas.mpl_connect('button_press_event', onClick) a = anim(fig, updatefig, frames=s.t.size, interval=1, blit=True) fig.tight_layout() plt.show()