function Tabular = RK4Exercise21at6_2(int,x0,h)
% Main Runge-Kutta
% Input: Interval [ta,T], 
%       initial condition x0,  and stepsize h
%       number of steps n=T/h
% Output: the approximate solution x, at time T
% Example usage: Tabular = RK4Exercise21at6_2([0 0.3],1,0.1)
%h = [0.015 0.020 0.025 0.030];
ta = int(1);
finalT = int(2);
n = floor(finalT/h);
    for timestep = h
    [time,x] = RK4(int,x0,timestep);
figure(1)
hold all
plot(time,x,'.-.','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10);
legend('RK4 approximation')            
strtimestep = ['step = ',num2str(timestep)];            
text(finalT,x(end),strtimestep,'HorizontalAlignment','left')             
   % pause
    end
RK4solution = x';
Tabular = table(time,RK4solution);
Tabular(1:length(time),:)  ;  
end

function [time,x] = RK4(int,x0,h)
ta = int(1);
finalT = int(2);
n = round((finalT - ta)/h);
time=zeros(n,1);
x(1) = x0;
time(1) = ta;
format long 

for i = 1:n
    K1 = h*xdot(time(i),x(i));
    K2 = h*xdot(time(i)+h/2,x(i)+K1/2);
    K3 = h*xdot(time(i)+h/2,x(i)+K2/2);
    K4 = h*xdot(time(i)+h,x(i)+K3);
    x(i+1) = x(i) + (K1 + 2*K2 + 2*K3 + K4)/6;
    time(i+1) = time(i) + h;
end
end

function z = xdot(t,x)  %the RHS of the ODE
z = t+x;
end
