%
%
% 
% predkosc

m=1000;
dt=0.1; %0.01;
v_in = 70 * (1000/3600); % m/s
d_in = 20; %37389; % metrow
przedzial = 10;
a1 = 1;
a2 = 2;
a3 = 3
a4 = 4;
a5 = 5;
a6 = 6;
a7 = 7;
a8 = 8;
a9 = 9;
aa = 10;

av=0;
ad=0;
afh=0;
t=1;

v= [00,00, 10,01, 20, 00,  % zerowa
   10,00, 20,01, 40, 00,   % mala
   20,00, 40,01, 60, 00,   % srednia
   40,00, 60,01, 80, 00,  % wysoka
   60,00, 80,01, 100,00,  % duza
   80,00, 100,01, 120,0, % ogromna
   100,0, 120,01, 140,0,
   120,0, 140,01, 160,0,
   140,0, 160,01, 180,0,
   160,0, 180,01, 201,0];

d= [0,00, 10, 01, 20, 00,  % zerowa
   10,00, 20, 01, 50, 00,   % mala
   20,00, 50, 01, 70, 00,   % srednia
   50,00, 70, 01, 90, 00,  % wysoka
   70,00, 90, 01, 110, 00,  % duza
   90,00, 110,01, 130, 00, % ogromna
   110,0, 130,01, 150, 00,
   130,0, 150,01, 170, 00,
   150,0, 170,01, 200, 00,
   170,0, 248,01, 300, 01];

f=[000,00,  0,01, 1000, 00,   % b.mala
   400,00,  1700,01, 1000, 00,   % mala
   0,0000,  2100,01, 1000, 00,   % wieksza
   600,00,  2800,01, 3000, 00,   % srednia
   600,00,  3500,01, 3000, 00,   % p.sredniej
   2000,00, 4200,01, 5000, 00,  % wysoka
   3000,00, 4900,01, 7000,00,  % duza
   3000,00, 5600,01, 11000,01; % ogromna
   0000,00, 6300,01, 11111,01,
   0000,00, 7000,01, 11111,01];

% predkosc
%    zerowa ,    mala,  srednia, wysoka,duza, ogromna, maksymalna
reg=[aa,aa,aa,aa,aa,aa,aa,aa,aa,aa,   % o  zerowa
     a1,a2,a2,a4,a4,a6,a9,aa,aa,aa,   % d
     a1,a2,a2,a1,a2,a6,a8,aa,aa,aa,   % l
     a1,a2,a2,a3,a2,a5,a8,aa,aa,aa,   % e
     a1,a2,a2,a3,a2,a5,a8,aa,aa,aa,   % g
     a1,a3,a3,a3,a2,a5,a8,a8,aa,aa,   % l
     a1,a3,a3,a3,a2,a5,a8,a8,aa,aa,   % o
     a1,a2,a2,a2,a2,a2,a8,a8,aa,aa,   % s
     a1,a1,a4,a4,a2,a3,a8,a8,aa,aa,   % c
     a1,a1,a1,a1,a1,aa,aa,aa,aa,aa];  %   maksymalna

while (d_in>0) 
 
vout=zeros(6);
dout=zeros(6);
fdol = 0;
fgora= 0;
    for a=1:1:przedzial
        if (v(a,5)>(v_in/(1000/3600)) && v(a,1)<(v_in/(1000/3600)))
        fx = [v(a,1), v(a,3), v(a,5)];
        fy = [v(a,2), v(a,4), v(a,6)];
        vout(a) = interp1(fx,fy,(v_in/(1000/3600)));
        end;
    
        if (d(a,5)>d_in && d(a,1)<d_in)
        fx = [d(a,1), d(a,3), d(a,5)];
        fy = [d(a,2), d(a,4), d(a,6)];
        dout(a) = interp1(fx,fy,d_in);
        end;    
    end;

    for a=1:1:przedzial
        for b=1:1:przedzial
            if ((vout(b)>0) && (dout(a)>0))
              zz = reg(a,b);
              ka=a;
              kb=b;
              %disp(a);disp(b);
              %return
              %if (zz<ogromna) 
                  %disp(a); disp(b);
                  %return;
              %end;
              dd =  min(vout(b),dout(a));
              if (dd>0)
              fdol = fdol + (dd * f(zz,3));
              fgora= fgora + dd;
              end;
            end;
        end;
    end;

    if (fgora > 0)
    fh = fdol / fgora;
    else
    fh =0 ;
    end;
    %if (fh > 1500 && t > 50 && t < 60)
     %   ka
      %  kb
        %return;
    %end;
    afh(t)=fh;
    v_in1 = v_in - (fh / m * dt);
    if (v_in1 <0)
        v_in1=0;
    av(t)=v_in1;
    d_in = d_in - (v_in * dt);
    ad(t)=d_in;
    t=t+1;
    v_in = v_in1;

        disp('error');
        break;
        %return;
    end;
    av(t)=v_in1;
    d_in = d_in - (v_in * dt);
    ad(t)=d_in;
    t=t+1;
    v_in = v_in1;

end;

skala = 1:1:t-1;
skala = skala.*dt;
size(skala)
size(av)
figure(1);
clf;
hold on;
title('predkosc');
plot(skala,av,'r');
plot(skala,0,'b');
hold off;
figure(2);
clf;
hold on;
title('odleglosc');
plot(skala,ad,'r');
plot(skala,0,'b');
hold off;
figure(3);
clf;
hold on;
title('hamowanie');
plot(skala,afh,'r');
plot(skala,0,'b');
hold off;
disp('wynik');
d_in
v_in