function [] = ballspiel()
% Konstanten
m =   0.10;
g =   9.81;
k = 500.00;

% Anfangs und Endzeitpunkt
t0 =   0.0;
t1 =  10.0;

% Startwerte
x0 = 1.2;
v0 = 5.0;


%% ----------------------------------------------------------------
% A: Freier Fall
% -----------------------------------------------------------------
options = odeset();
[T,YA] = ode45(@myFunA,[t0 t1],[x0 v0],options);
figure; plot(T, YA(:,1)); grid on; ylim([-0.5 3]);


%% ----------------------------------------------------------------
% B: Harter Aufprall
% -----------------------------------------------------------------
options = odeset('MaxStep', 0.001);
[T,YB] = ode45(@myFunB,[t0 t1],[x0 v0], options);
E_kin = 0.5*m*YB(:,2).*YB(:,2);
E_pot = m*g*abs(YB(:,1));
E = E_kin + E_pot;
YB = abs(YB);
figure; plot(T, [YB(:,1) E]); grid on; ylim([-0.5 3]);


%% ----------------------------------------------------------------
% C: Weicher Aufprall
% -----------------------------------------------------------------
options = odeset('MaxStep', 0.005);
[T,YC] = ode45(@myFunC,[t0 t1],[x0 v0], options);
E_kin = 0.5*m*YC(:,2).*YC(:,2);
E_pot = m*g*YC(:,1);
E_fed = 0.5*k*YC(:,1).*YC(:,1).*(YC(:,1)<0);
E = E_kin + E_pot + E_fed;
figure; plot(T, [YC(:,1) E]); grid on; ylim([-0.5 3]);



end



function dY = myFunA(t, Y)
    m =   0.10;
    g =   9.81;

    dY = zeros(2,1);
    dY(1) = Y(2);
    dY(2) = -g;
end


function dY = myFunB(t, Y)
    m =   0.10;
    g =   9.81;

    dY = zeros(2,1);
    dY(1) = Y(2);
    dY(2) = -sign(Y(1))*g;
end

function dY = myFunC(t, Y)
    m =   0.10;
    g =   9.81;
    k = 500.00;
    b =   0.50;

    dY = zeros(2,1);
    dY(1) = Y(2);
    if Y(1) < 0
        dY(2) = -g -(b/m)*Y(2)-(k/m)*Y(1);
    else
        dY(2) = -g;
    end
    
end




