% Exercice1
y1 = [0.4;0];

% RK2 2 time steps
nevalf2 = 2;

[tRK2_2,yRK2_2]  = RK2(@fex1,[0,1],y1,nevalf2);
yRK2_2(1)

% RK2 4 time steps
nevalf4 = 4;

[tRK2_4,yRK2_4]  = RK2(@fex1,[0,1],y1,nevalf4);
yRK2_4(1)

% Exact solution
s = sqrt(9.8/1);
A = 0.4/(cos(s)+tan(s)*sin(s));
B = A*tan(s);
f1 = A*cos(s*1)+B*sin(s*1);
f0 = A;

% relative error
error2steps = (yRK2_2(1)-f0)/f0

% To find h
nstep = 2;
yRK2 = yRK2_2;
while abs(yRK2(1)-f0)>abs(yRK2_2(1)-f0)*0.001 && nstep<100
    [tRK2,yRK2]  = RK2(@fex1,[0,1],y1,nstep);
    nstep = nstep+1;
end
h = (1-0)/nstep
yRK2(1)
    
    
    
    
    
%Exercice2

%a)
y0 = 1;
h = 0.25;
nstep = (1-0)/h;

[tEuler,yEuler]  = Euler(@fex2,[0,1],y0,nstep);

    
%plot(tEuler,yEuler);

%b)
%Because of the computional cost difference, we calculate 2 times with f in
%Heun, we can calculate with h=0.5. That means 2 times less operations.

h = 0.5;
nstep = (1-0)/h;

[tHeun,yHeun] = Heun(@fex2,[0,1],y0,nstep);

%plot(tHeun,yHeun);
