6615372e636bb213cbdf4268171ec57c7271300d
[course.git] / latex / problems / wking / problem09.limo.T.limo.py
1 from pylab import *
2 from matplotlib.patches import Polygon
3 import sys
4
5 filename = None
6 if len(sys.argv) >= 2:
7     filename = sys.argv[1]
8     
9
10 figure(facecolor='w', figsize=[4,3])
11 border = 0.15
12 ax = axes([border,border,1-2*border,1-2*border])
13 ax.set_xticks(range(-10,11,1))
14 ax.set_yticks(range(-10,11,1))
15 ax.grid()
16
17 ax.set_title("Limo paradox, garage frame")
18 ax.set_xlabel("Distance (light seconds)")
19 ax.set_ylabel("Time (seconds)")
20
21 # add light beams
22 ax.plot([-10,10],[-10,10], 'b-')
23 ax.plot([10,-10],[-10,10], 'b-')
24
25 # switch to a moving observer
26 v = 0.661
27 width = 1
28 gamma = 1.0/sqrt(1-v**2)
29 t= arange(-5,10,1)
30
31 # plot his spaceship
32 spaceship = Polygon([(v*t[0]-width,t[0]),   (v*t[0], t[0]),
33                      (v*t[-1],t[-1]),       (v*t[-1]-width, t[-1])],
34                     alpha=0.2, facecolor='k')
35 ax.add_patch(spaceship)
36
37 # plot the position track of a moving observer
38 ax.plot(v*t,t, 'r-')
39
40 # plot the position track of the garage
41 ax.plot(0*t,t, 'g-')
42 ax.plot(-width+0*t,t, 'g-')
43
44 # plot his xprime axis at his 1 second intervals
45 tp = gamma*t
46 def plot_xp(tp, v, xmin=-10, xmax=10):
47     x = arange(xmin, xmax, 0.01)
48     xp_y = x*v
49     for t in tp:
50         origin_x = v*t
51         origin_y = t
52         ax.plot(x+origin_x, xp_y+origin_y, 'r:')
53 plot_xp(tp, v)
54
55 # plot his tprime grids at his 1 light-second intervals
56 xp = tp
57 def plot_tp_grid(xp, v, tmin=-10, tmax=10):
58     t = arange(tmin, tmax, 0.01)
59     tp_x = t*v
60     for x in xp:
61         origin_x = x
62         origin_y = v*x
63         ax.plot(tp_x+origin_x, t+origin_y, 'r:')
64 plot_tp_grid(xp, v)
65
66 proper_width = gamma*width
67 x_back_limo_frame = -proper_width
68 t_back_limo_frame = 0
69 x_back_limo_frame_p = gamma*(x_back_limo_frame+v*t_back_limo_frame)
70 t_back_limo_frame_p = gamma*(t_back_limo_frame+v*x_back_limo_frame)
71 x_gar_limo_frame = -width/gamma
72 t_gar_limo_frame = 0
73 x_gar_limo_frame_p = gamma*(x_gar_limo_frame+v*t_gar_limo_frame)
74 t_gar_limo_frame_p = gamma*(t_gar_limo_frame+v*x_gar_limo_frame)
75 points = [(0,0), (-width,0), (x_back_limo_frame_p, t_back_limo_frame_p),
76           (x_gar_limo_frame_p, t_gar_limo_frame_p)]
77 labels = ["A", "B", "C", "D"]
78
79 ax.plot([p[0] for p in points], [p[1] for p in points], 'ko')
80 for p,L in zip(points, labels):
81     ax.text(p[0], p[1], '  '+L,
82             horizontalalignment='left',
83             verticalalignment='center')
84
85 ax.set_ybound(-2,0.5)
86 ax.set_xbound(-2,0.5)
87 ax.set_aspect('equal')   # Make aspect ratio equal to 1
88
89 if filename == None:
90     show()
91 else:
92     savefig(filename, dpi=200)