% The following script will produce a sixth order Adams-Moulton method.
% It will begin by finding the first sixpoints by means of a 5th order
% Runge-Kutta scheme. Once those values are found it will carry on with
% Adams-Moulton's method with step-size h.

function y = adamsmoult6(ode_fxns,Tspan,IV,h)
[t_RK,y_RK] = get_points_IV(ode_fxns,6,Tspan,IV,h);
t_temp = t_RK;
y_temp = y_RK;
i = 1;
y=y_temp;

    while (t_temp(6) < Tspan(2))
         y(:,6+i+1) = y_temp(:,7)+ h/1440*(4277*ode_fxns(t_temp(6),y_temp(:,6))...
          -7923*ode_fxns(t_temp(5),y_temp(:,5))+9982*ode_fxns(t_temp(4),y_temp(:,4))...
          -7298*ode_fxns(t_temp(3),y_temp(:,3))+2877*ode_fxns(t_temp(2),y_temp(:,2))...
          -475*ode_fxns(t_temp(1),y_temp(:,1)));
          check = 1;
          y_old = y(:,6+i+1);
          while (check > .5e-3)
            y(:,6+i+1) = y_temp(:,7)+ h/1440*(475*ode_fxns(t_temp(6)+h,y_old(:))+1427*...
                ode_fxns(t_temp(6),y_temp(:,6))-798*ode_fxns(t_temp(5),y_temp(:,5))...
                +482*ode_fxns(t_temp(4),y_temp(:,4))-173*ode_fxns(t_temp(3),y_temp(:,3))...
                +27*ode_fxns(t_temp(2),y_temp(:,2)));
            check = max(abs((y_old(3:4)-y(3:4,6+i+1))));
            y_old = y(:,6+i+1);
          end
    y_temp(:,1:6) = y_temp(:,2:7); t_temp(1:5)= t_temp(2:6);
    y_temp(:,7) = y(:,6+i+1); t_temp(6) = t_temp(6) + h;
    i = i+1;
    end
    
    
end
