clear;
clc;

% Draw the wave through the transmission line

Rg = 300;  % Resistance of the Thevenin-generator
U0 = 200;  % Voltage of the Thevenin-generator
R2 = 1300; % Resistance of the output
Z0 = 700;  % Wave resistance of the transmission line
l  = 1500; % Length of the transmission line
v  = 3e8;  % Velocity

T  = l/v;  % Time while wave go through the line
r1 = (Rg-Z0) / (Rg+Z0); % Reflexion coefficients (primer)
r2 = (R2-Z0) / (R2+Z0); % Reflexion coefficients (secunder)

% Draw 20 periods
U  = 0;
p  = 0;
s  = 0;
k  = 0;
for i = 1:7,
    if sum(i == [1:2:10]), 
        if i == 1, dU = U0 * Z0 / (Z0 + Rg); U1 = dU;
        else
            p  = p + 1;
            dU = (r1^p) * (r2^s) * U1;
        end;
        st  = 0:150-1; 
        st0 = 0;
    else
        s   = s + 1;
        dU  = (r1^p) * (r2^s) * U1;
        st  = 150-1:-1:0; 
        st0 = l;
    end;
    U = U + dU;
    disp([num2str(U) 'V']);
    
    for j = st,
        figure(1), cla, hold on,
        plot([st0 (j+1)*10],[U U],'LineWidth',2);
        plot([(j+1)*10 (j+1)*10],[U 0],'LineWidth',2);
        axis([0 l 0 200]); 
        set(gca,'Box','on');
        xlabel('x [m]');
        ylabel('U [V]');
        k = k + 1;
        saveas(gcf,['Reflex' num2str(k)],'bmp');
        pause(0.1);
    end;
end;