% Beispielimplementierung zu Aufgabe 1.3
function []= Blatt1()
clear all; close all; clc;
a=2; % velocity of the person
c=1; % velocity of the dog
T=2; % maximun time
h=0.1; % time step

% a) See the functions hund1 and hund2 below

% b) Fix a,c,T, test with different h

for i=1:3
    h=h*2^(-i+1);
    hund1(a,c,T,h)
    hund2(a,c,T,h)
end

% Fix h,T, test with a>c, a<c, a=c
h=0.01;
% The dog runs slower than the person a>c
hund1(a,c,T,h)
hund2(a,c,T,h)

% The dog runs faster than the person 
hund1(c,a,T,h)
hund2(c,a,T,h)

% % The dog and the person have the same velocity
hund1(a,a,T,h)
hund2(a,a,T,h)
end

% Sinnvolle Abbruchbedingungen sind z.B.:
% x(t) >= 0
% y(t) >= at

% c) Denote by H(t) := (x(t),y(t)) the position of the dog
%    and P(t):=(0,z(t)) the position of the person
% then if the dog follows the new position of the person, we have the
% following equation:
% H(t+h)=H(t) + ch(P(t+h)-H(t))/||P(t+h)-H(t)||
% Taking the limit as h->0 we get
% H'(t) + c*H(t)/||P(t)-H(t)|| = c*P(t)/||P(t)-H(t)||

% If the dog follows the old position of the person, we get
% H(t+h)=H(t) + ch(P(t)-H(t))/||P(t)-H(t)||
% Taking the limit as h->0 we also obtain
% H'(t) + c*H(t)/||P(t)-H(t)|| = c*P(t)/||P(t)-H(t)||


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function []= hund1(a,c,T,h)
% Zuerst macht die Person einen Schritt und dann geht der Hund auf die Position
% der Person zu
pausetime=0.05;
% Deklarierung/Initialisierung:
t=0:h:T;
r=length(t);

x=zeros(1,r);
y=zeros(1,r);
z=zeros(1,r);

x(1)=-1; %initial position of the dog in x-axis
y(1)=0; %initial position of the dog in y-axis
z(1)=0; %initial position of the person in y-axis
% Note that the position of the person in x-axis is always 0

for i=2:r % Umsetzung des schrittweisen Ablaufs 
    
    z(i)=z(i-1)+a*h; % position of the person at time t=(i-1)h
    dist= norm([x(i-1),y(i-1)-z(i)]); % distance between the dog and the person
    
    % position of the dog at time t=(i-1)h
    x(i)=x(i-1)+c*h*(0-x(i-1))/dist;
    y(i)=y(i-1)+c*h*(z(i)-y(i-1))/dist;
    
end

% figure-Einstellungen:
figure
xlabel('x-Achse')
ylabel('y-Achse')
string=['Hund (Geschwindigkeit c=',num2str(c),') und Person (Geschwindigkeit a=',num2str(a),')'];
title(string)
axis([-1.05,0.1,-0.05,a*T+0.7])
string=['Time step h=',num2str(h)];
text(-1,1,string)
text(-1,1.3,'Hund geht auf die neue Position zu')

hold on

% schrittweiser Plot:
for i=2:r 
    plot([x(i-1),x(i)],[y(i-1),y(i)],'r.-')
    pause(pausetime)
    plot(0,z(i),'.g')
    pause(pausetime)
end
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function []= hund2(a,c,T,h)
% Der Hund auf die alte Position zugeht
% Deklarierung/Initialisierung:
t=0:h:T;
r=length(t);
pausetime=0.05;x=zeros(1,r);
y=zeros(1,r);
z=zeros(1,r);

x(1)=-1; %initial position of the dog in x-axis
y(1)=0; %initial position of the dog in y-axis
z(1)=0; %initial position of the person in y-axis
% Note that the position of the person in x-axis is always 0

for i=2:r % Umsetzung des schrittweisen Ablaufs 
    dist= norm([x(i-1),y(i-1)-z(i-1)]); % distant between the dog and the person at time t=(i-2)h
    z(i)=z(i-1)+a*h; % position of the person at time t=(i-1)h
    
    % position of the dog at time t=(i-1)h
    x(i)=x(i-1)+c*h*(0-x(i-1))/dist;
    y(i)=y(i-1)+c*h*(z(i-1)-y(i-1))/dist;
    
end

% figure-Einstellungen:
figure
xlabel('x-Achse')
ylabel('y-Achse')
string=['Hund (Geschwindigkeit c=',num2str(c),') und Person (Geschwindigkeit a=',num2str(a),')'];
title(string)
axis([-1.05,0.1,-0.05,a*T+0.7])
string=['Time step h=',num2str(h)];
text(-1,1,string)
text(-1,1.3,'Hund geht auf die alte Position zu')

hold on

% schrittweiser Plot:
for i=2:r 
    plot([x(i-1),x(i)],[y(i-1),y(i)],'r.-')
    pause(pausetime)
    plot(0,z(i),'.g')
    pause(pausetime)
end
end


