%%
%% 2.3  Twin paradox and light clocks  (Computational example)
% With the Lorentz transformation, world points and world lines are transformed between coordinate systems with constant relative velocity. This can be used to study the twin paradox of the Special Theory of Relativity, or to show that, in a moving frame of reference, time slows down.

clear all

% Lorentz transformation from chapter 2.2

load Kap02
LT
syms c positive

%
%% 1 World  lines
% Light cone
% Light propagates in all directions at the same speed. The world points appear shifted in different reference systems, but the cone's opening angle remains the same.
% Light cone: \[LK = \left( {\begin{array}{*{20}{c}}
%   { - 2}&{ - 1}&0&1&2 \\
%   2&1&0&1&2
% \end{array}} \right)\]

LK=[-2 -1, 0 1 2;2 1 0 1 2]

% Graphic representation of world lines (WeLi see 'function' below) in reference systems with velocities c\in \{ -1/2,0, 1/2\}

subplot(3,3,4)
WeLi(LK,-c/2,0.6*[1 1 0],LT)
axis equal
subplot(3,3,5)
WeLi(LK,0,0.6*[1 1 0],LT)
axis equal
subplot(3,3,6)
WeLi(LK,c/2,0.6*[1 1 0],LT)
axis equal

%
%% 2 Twin paradox
% One twin stays at home, the other moves away at half the speed of light, to turn back after one unit of time (in the rest frame of the one staying at home).
% World lines of the twins: \[Z{W_1} = \left( {\begin{array}{*{20}{c}}
%   0&0&0 \\
%   2&1&0
% \end{array}} \right),\quad Z{W_2} = \left( {\begin{array}{*{20}{c}}
%   0&{1/2}&0 \\
%   0&1&1
% \end{array}} \right),\quad ZW = \left[ {Z{W_1}\;Z{W_2}} \right]\]

ZW=[0 0 0 1/2 0;2 1 0 1 2];

% Graphics

Plo({LK ZW},{.6*[1 1 0] 'b'},LT)

% In the rest frame of the 'home stayer', his twin returns after one time unit, but in the rest frame of the 'traveler' this happens already after 0.86 time units.
% For the journey home, the 'traveler' also needs 0.86 time units in his frame of reference.
% For the 'home stayer' the journey took 2 time units, for the 'traveler only 1.72 time units, so he stayed younger.
%
%% 3 Light clocks
% A time unit can be defined by a light clock. One sends a light signal in the x-direction to a mirror and registers the reflected light. The elapsed time defines a time unit. We draw the world lines of the two mirrors and the light beam in the rest frame of the clock, and in a frame moving in the direction of the light. We see that this clock is slower in the moving frame than it is in the rest frame.
% Light cone, clock 1 and clock 2

LK=[0 1 0;0 1 2],U1=[0 0 0;0 1 2];U2=[1 1 1;0 1 2];
Plo({LK U1 U2},{.6*[1 1 0] 'b' 'b'},LT)

%
% ________________________________________________________________
% Plot world lines

function WeLi(X,v,col,LT)
    subs(LT*X);
    simplify(ans);
    hold on
    plot(ans(1,:),ans(2,:),'Color',col)
    plot(ans(1,:),ans(2,:),'o','Color',col,'MarkerSize',5)
    xlabel('Position x')
    ylabel('Time t')
end

% ________________________________________________________________
% Plot complete scenarios in different frames of reference

function Plo(Li,col,LT)
    syms c positive
    v=[-c/2 0 c/2];
    figure
    for n=1:size(v,2)
        subplot(1,3,n)
        for k=1:size(Li,2)
            WeLi(Li{k},v(n),col{k},LT)
        end
        axis equal, axis([-2 2 0 2.5])
        set(gca,'XGrid','on','yGrid','on','GridAlpha',.5)
        txt=['v=' char(v(n))];
        title(txt)
        set(gca,'XTick',[-2:2])
    end
end