# derivation-of-time-dilation.ipynb

In [1]:
from sympy import symbols, Eq, solve, Symbol, latex, sqrt, UnevaluatedExpr
import matplotlib.pyplot as plt
import numpy as np

In [2]:
t_0 = Symbol('t_0', constant = True)
t = Symbol('t')
d, L, c, gamma = symbols('d, L, c, gamma', constant = True)
u = symbols('u')

In [3]:
def get_mirror_center(center_of_mirrors, d_val):
center_of_mirror_up = np.array([center_of_mirrors[0], center_of_mirrors[1] + (d_val/2)])
center_of_mirror_down = np.array([center_of_mirrors[0], center_of_mirrors[1] - (d_val/2)])
return center_of_mirror_up, center_of_mirror_down

def get_mirrors(center_of_mirrors, length, d_val):
center_of_mirror_up, center_of_mirror_down = get_mirror_center(center_of_mirrors, d_val)
mirror_up = np.array([[center_of_mirror_up[0] - (length/2), center_of_mirror_up[1]],
[center_of_mirror_up[0] + (length/2), center_of_mirror_up[1]]])

mirror_down = np.array([[center_of_mirror_down[0] - (length/2), center_of_mirror_down[1]],
[center_of_mirror_down[0] + (length/2), center_of_mirror_down[1]]])

return mirror_up, mirror_down

In [4]:
eq1 = Eq(t_0, 2*d/c)
eq1

Out[4]:
$\displaystyle t_{0} = \frac{2 d}{c}$
In [5]:
length = 4
d_val = 5
center_of_mirrors = np.array([0, 0])

mirror_up, mirror_down = get_mirrors(center_of_mirrors, length, d_val)
center_of_mirror_up, center_of_mirror_down = get_mirror_center(center_of_mirrors, d_val)

d_arrow1 = np.array([[center_of_mirror_up[0] + 0.1, center_of_mirror_up[1]],
[0, -d_val]])

d_arrow2 = np.array([[center_of_mirror_down[0] - 0.1, center_of_mirror_down[1]],
[0, d_val]])

In [6]:
plt.figure(figsize = (6, 6))
plt.plot(mirror_up[:, 0], mirror_up[:, 1], color = 'black')
plt.plot(mirror_down[:, 0], mirror_down[:, 1], color = 'black')

plt.arrow(*d_arrow1.flatten(), length_includes_head = True, width = 0.05, color = 'red')
plt.arrow(*d_arrow2.flatten(), length_includes_head = True, width = 0.05, color = 'red')

plt.text(center_of_mirrors[0] + 0.2, center_of_mirrors[1], s = 'd', fontsize = 16)
plt.gca().set_aspect('equal')

In [7]:
length = 4
d_val = 5
center_of_mirrors1 = np.array([0, 0])
mirror1_up, mirror1_down = get_mirrors(center_of_mirrors1, length, d_val)
center_of_mirror1_up, center_of_mirror1_down = get_mirror_center(center_of_mirrors1, d_val)

center_of_mirrors2 = np.array([10, 0])
mirror2_up, mirror2_down = get_mirrors(center_of_mirrors2, length, d_val)
center_of_mirror2_up, center_of_mirror2_down = get_mirror_center(center_of_mirrors2, d_val)

center_of_mirrors3 = np.array([20, 0])
mirror3_up, mirror3_down = get_mirrors(center_of_mirrors3, length, d_val)
center_of_mirror3_up, center_of_mirror3_down = get_mirror_center(center_of_mirrors3, d_val)

L_arrow1 = np.array([center_of_mirror1_down, center_of_mirror2_up - center_of_mirror1_down ])
L_arrow2 = np.array([center_of_mirror2_up, center_of_mirror3_down - center_of_mirror2_up ])

mid_point_mirror_1_3_down = (center_of_mirror1_down + center_of_mirror3_down)/2
u_t_arrow1 = np.array([mid_point_mirror_1_3_down - np.array([0, 1.5]), center_of_mirror1_down - mid_point_mirror_1_3_down])
u_t_arrow2 = np.array([mid_point_mirror_1_3_down - np.array([0, 1.5]), center_of_mirror3_down - mid_point_mirror_1_3_down])

In [25]:
plt.figure(figsize = (13, 9))
plt.plot(mirror1_up[:, 0], mirror1_up[:, 1], color = 'black')
plt.plot(mirror1_down[:, 0], mirror1_down[:, 1], color = 'black')

plt.plot(mirror2_up[:, 0], mirror2_up[:, 1], color = 'black')
plt.plot(mirror2_down[:, 0], mirror2_down[:, 1], color = 'black')

plt.plot(mirror3_up[:, 0], mirror3_up[:, 1], color = 'black')
plt.plot(mirror3_down[:, 0], mirror3_down[:, 1], color = 'black')

plt.arrow(*L_arrow1.flatten(), length_includes_head = True, width = 0.1, color = 'red')
plt.arrow(*L_arrow2.flatten(), length_includes_head = True, width = 0.1, color = 'red')

plt.text((L_arrow1[0] + (L_arrow1[1]/2))[0] - 1, (L_arrow1[0] + (L_arrow1[1]/2))[1], s = 'L', fontsize = 16, ha = 'center', va = 'center')
plt.text((L_arrow2[0] + (L_arrow2[1]/2))[0] + 1, (L_arrow2[0] + (L_arrow2[1]/2))[1], s = 'L', fontsize = 16, ha = 'center', va = 'center')

plt.arrow(*u_t_arrow1.flatten(), length_includes_head = True, width = 0.1, color = 'blue')
plt.arrow(*u_t_arrow2.flatten(), length_includes_head = True, width = 0.1, color = 'blue')

plt.text(mid_point_mirror_1_3_down[0], mid_point_mirror_1_3_down[1] - 1, s = fr'$u t$', fontsize = 16, ha = 'center', va = 'center', color = 'blue')

plt.plot([center_of_mirror2_up[0], center_of_mirror2_down[0]], [center_of_mirror2_up[1], center_of_mirror2_down[1]], color = 'green')
plt.text(center_of_mirrors2[0] + 0.5, center_of_mirrors2[1], s = 'd', fontsize = 16, ha = 'center', va = 'center')

plt.gca().set_aspect('equal')
plt.axis('off')
plt.show()

In [9]:
eq2 = Eq(t, 2*L/c)
eq2

Out[9]:
$\displaystyle t = \frac{2 L}{c}$
In [10]:
eq3 = Eq(L, sqrt(d**2 + UnevaluatedExpr(u*t/2)**2))
eq3

Out[10]:
$\displaystyle L = \sqrt{d^{2} + \left(\frac{t u}{2}\right)^{2}}$
In [11]:
eq4 = Eq(d, t_0*UnevaluatedExpr(c/2))
eq4

Out[11]:
$\displaystyle d = t_{0} \frac{c}{2}$
In [12]:
eq5 = Eq(L, sqrt((t_0*c/2)**2 + (u*t/2)**2))
eq5

Out[12]:
$\displaystyle L = \sqrt{\frac{c^{2} t_{0}^{2}}{4} + \frac{t^{2} u^{2}}{4}}$
In [13]:
eq6 = Eq(t, UnevaluatedExpr(2/c) * sqrt((t_0*c/2)**2 + (u*t/2)**2))
eq6

Out[13]:
$\displaystyle t = \sqrt{\frac{c^{2} t_{0}^{2}}{4} + \frac{t^{2} u^{2}}{4}} \frac{2}{c}$
In [14]:
eq7 = Eq(t**2, UnevaluatedExpr(2/c)**2 * ((t_0*c/2)**2 + (u*t/2)**2))
eq7

Out[14]:
$\displaystyle t^{2} = \left(\frac{c^{2} t_{0}^{2}}{4} + \frac{t^{2} u^{2}}{4}\right) \left(\frac{2}{c}\right)^{2}$
In [15]:
eq8 = eq7.simplify()
eq8

Out[15]:
$\displaystyle t^{2} = t_{0}^{2} + \frac{t^{2} u^{2}}{c^{2}}$
In [16]:
eq9 = Eq(t**2 - (t*u/c)**2, t_0**2)
eq9

Out[16]:
$\displaystyle t^{2} - \frac{t^{2} u^{2}}{c^{2}} = t_{0}^{2}$
In [17]:
eq10 = Eq(t**2 * (1 - (u/c)**2), t_0**2)
eq10

Out[17]:
$\displaystyle t^{2} \left(1 - \frac{u^{2}}{c^{2}}\right) = t_{0}^{2}$
In [18]:
eq11 = Eq(t**2, t_0**2 / (1 - (u/c)**2))
eq11

Out[18]:
$\displaystyle t^{2} = \frac{t_{0}^{2}}{1 - \frac{u^{2}}{c^{2}}}$
In [19]:
eq12 = Eq(t, t_0 / sqrt(1 - (u/c)**2))
eq12

Out[19]:
$\displaystyle t = \frac{t_{0}}{\sqrt{1 - \frac{u^{2}}{c^{2}}}}$
In [20]:
eq13 = Eq(gamma, 1 / sqrt(1 - (u/c)**2))
eq13

Out[20]:
$\displaystyle \gamma = \frac{1}{\sqrt{1 - \frac{u^{2}}{c^{2}}}}$
In [21]:
eq13 = Eq(t, gamma*t_0)
eq13

Out[21]:
$\displaystyle t = \gamma t_{0}$
In [22]:
t_0_val = 10
c_val = 1
u_val = np.linspace(0, c_val, 100)
gamma_val = 1 / np.sqrt(1 - (u_val/c_val)**2)
t_val = gamma_val*t_0_val

u_val_dots = np.array([0, 0.25, 0.5, 0.6, 0.87, 0.95])
gamma_val_dots = 1 / np.sqrt(1 - (u_val_dots/c_val)**2)
t_val_dots = gamma_val_dots*t_0_val

/opt/tljh/user/lib/python3.7/site-packages/ipykernel_launcher.py:4: RuntimeWarning: divide by zero encountered in true_divide
after removing the cwd from sys.path.

In [23]:
fig = plt.figure(figsize = (6, 6))
plt.plot(u_val, t_val, color = 'blue')
plt.scatter(u_val_dots, t_val_dots, color = 'red')

for i in range(len(u_val_dots)):
x_offset = 0
if i == 3:
x_offset = 0.1

plt.text(u_val_dots[i] + x_offset, t_val_dots[i] + 3, s= f'({u_val_dots[i]}, {round(t_val_dots[i], 2)})', ha = 'right', va = 'center')

plt.xlabel('speed of object(expressed in terms of c)', fontsize = 14)
plt.ylabel('time delay', fontsize = 14)

Out[23]:
Text(0, 0.5, 'time delay')
In [ ]: