function [t,x] = RK4Exercise10at10_2(int,x0,t0,n)
%Input: Interval [a,b], initial condition x0, at time t0, with a<t0<b, and the number of steps n
%Output: the approximate solution x, at time T
%Example usage: [t,x]=RK4Exercise10at10_2([1 7],4,2,100);

a = int(1);
b = int(2);
h = (b - a)/n %%step size h=(T-ta)/n

format long 

x = x0;
t = t0;
n2 = int8((b-t0)/h)

for j = 1:n2
    K1 = h*xdot(t,x);
    K2 = h*xdot(t+h/2,x+K1/2);
    K3 = h*xdot(t+h/2,x+K2/2);
    K4 = h*xdot(t+h,x+K3);
    x = x + (K1 + 2*K2 + 2*K3 + K4)/6;
    t = t + h;
figure(1)
hold all
plot(t,x,'.','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10);
end

y = x0;
t = t0;
n1 = int8((t0 - a)/h)
h=-h;     % going backwards now
for j = 1:n1;
    KK1 = h*xdot(t,x);
    KK2 = h*xdot(t+h/2,x+KK1/2);
    KK3 = h*xdot(t+h/2,x+KK2/2);
    KK4 = h*xdot(t+h,x+KK3);
    y = y + (KK1 + 2*KK2 + 2*KK3 + KK4)/6;
    t = t + h;       
plot(t,y,'o','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',10);            

            
end
hold all


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