classdef Rungekutta4 < time.Timestepper
    properties
        F
        dt
        t
        v
        n
    end


    methods
        % Create a time stepper for
        %   v_t = F(t,v),  v(t0) = v0
        % with step size dt.
        function obj = Rungekutta4(F, dt, t0, v0)
            obj.F = F;
            obj.dt = dt;
            obj.t = t0;
            obj.v = v0;
            obj.n = 0;
        end

        function [v,t] = getV(obj)
            v = obj.v;
            t = obj.t;
        end

        function obj = step(obj)
            v = obj.v;
            dt = obj.dt;

            k1 = obj.F(t, v);
            k2 = obj.F(t + 0.5*dt, v + 0.5*dt*k1);
            k3 = obj.F(t + 0.5*dt, v + 0.5*dt*k2);
            k4 = obj.F(t + dt, v + dt*k3);

            obj.v = v + dt*(1/6)*(k1+2*(k2+k3)+k4);
            obj.t = obj.t + obj.dt;
            obj.n = obj.n + 1;
        end
    end

    methods (Static)
        function dt = getTimeStep(lambda)
            dt = rk4.get_rk4_time_step(lambda);
        end
    end
end