% Special relativity: the twin paradox; first script
% 
% Carlos R. Paiva
%
% 18 August 2017

clear all
close all

x_max = 5;
t_max = 12;

T = 10;
beta = 3/5;
L = beta*T/2;
gamma = 1/sqrt(1-beta^2);
T_prime = T/gamma;
e0 = 2.5;
e1 = e0;

patch([0 x_max x_max 0],[0 0 t_max t_max],[255 255 204]/255,'EdgeColor','none')
axis equal
axis off
hold on
patch([0 L 0],[0 T/2 T],[204 204 255]/255,'EdgeColor','none')

plot([0 x_max],[0 0],'b','LineWidth',1)
plot([0 0],[0 t_max],'b','LineWidth',1)

plot([0 L 0],[0 T/2 T],'b','LineWidth',2)
plot([0 0],[0 T],'b','LineWidth',2)

plot([0 L],[T/2 T/2],'--b','LineWidth',1)
plot([L L],[0 T/2],'--b','LineWidth',1)

quiver(0,0,0,e0,0,'r','LineWidth',2)
quiver(0,0,e1,0,0,'r','LineWidth',2)

plot(0,0,'ok','MarkerFaceColor','b','MarkerSize',8)
plot(0,T,'ok','MarkerFaceColor','b','MarkerSize',8)
plot(L,T/2,'ok','MarkerFaceColor','b','MarkerSize',8)
plot(0,T/2,'ok','MarkerFaceColor','b','MarkerSize',8)
plot(L,0,'ok','MarkerFaceColor','b','MarkerSize',8)

figure

beta = 3/5;
t_max = 12;
x_max = beta*t_max;
x_min = -beta*t_max;
t_min = -beta*x_max;

tau = 2.5;  csi = 2.5;
n_max = 1000;
phi_max = asinh(x_max/tau);
phi_min = -phi_max;

for n = 1:n_max
    phi = (phi_max-phi_min)*(n-1)/(n_max-1)+phi_min;
    x_t(n) = tau*sinh(phi);
    t_t(n) = tau*cosh(phi);
end

phi_max = acosh(x_max/csi);
phi_min = asinh(t_min/csi);

for n = 1:n_max
    phi = (phi_max-phi_min)*(n-1)/(n_max-1)+phi_min;
    x_s(n) = csi*cosh(phi);
    t_s(n) = csi*sinh(phi);
end

patch([x_min x_max x_max x_min],[t_min t_min t_max t_max],[255 255 204]/255,'EdgeColor','none')
axis equal
axis off
hold on
patch([0 L 0],[0 T/2 T],[204 204 255]/255,'EdgeColor','none')

plot([0 x_max],[0 0],'b','LineWidth',1)
plot([0 0],[0 t_max],'b','LineWidth',1)
plot([0 beta*t_max],[0 t_max],'b','LineWidth',1)
plot([0 x_max],[0 beta*x_max],'b','LineWidth',1)
plot([0 -beta*t_max],[0 t_max],'b','LineWidth',1)
plot([0 x_max],[0 -beta*x_max],'b','LineWidth',1)

plot(x_t,t_t,'r',x_s,t_s,'r','LineWidth',1)

plot([0 L 0],[0 T/2 T],'b','LineWidth',2)
plot([0 0],[0 T],'b','LineWidth',2)

plot([0 x_max],[0 x_max],'--r','LineWidth',1)
plot([0 -t_min],[0 t_min],'--r','LineWidth',1)
plot([0 x_min],[0 -x_min],'--r','LineWidth',1)

plot([0 L],[T/2 T/2],'--b','LineWidth',1)
plot([L L],[0 T/2],'--b','LineWidth',1)

quiver(0,0,0,e0,0,'r','LineWidth',2)
quiver(0,0,e1,0,0,'r','LineWidth',2)
quiver(0,0,gamma*beta*e0,gamma*e0,0,'r','LineWidth',2)
quiver(0,0,gamma*e1,gamma*beta*e1,0,'r','LineWidth',2)
quiver(0,0,-gamma*beta*e0,gamma*e0,0,'r','LineWidth',2)
quiver(0,0,gamma*e1,-gamma*beta*e1,0,'r','LineWidth',2)




