class BDFDerivative < handle
    properties
        dt
        coefficents
    end
    methods
        function obj = BDFDerivative(order, dt)
            obj.coefficents = obj.getBDFCoefficients(order);
            obj.dt = dt;
        end

        function DvDt = evaluate(v, v_prev)
            DvDt = (obj.coefficients(1)*v - sum(obj.coefficients(2:end).*v_prev),2)/obj.dt;
        end

        function c = getBDFCoefficients(obj, order)
            switch order
                case 1
                    c(1,1) = 1; c(1,2) = 1;
                case 2 
                    c(1,1) = 3/2; c(1,2) = 4/2; c(1,3) = -1/2;
                case 3
                    c(1,1) = 11/6; c(1,2) = 18/6; c(1,3) = -9/6; c(1,4) = 2/6;
                case 4
                    c(1,1) = 25/12; c(1,2) = 48/12; c(1,3) = -36/12; c(1,4) = 16/12; c(1,5) = -3/12;
                case 5
                    c(1,1) = 137/60; c(1,2) = 300/60; c(1,3) = -300/60; c(1,4) = 200/60; c(1,5) = -75/60; c(1,6) = 12/60;
                case 6
                    c(1,1) = 147/60; c(1,2) = 360/60; c(1,3) = -450/60; c(1,4) = 400/60; c(1,5) = -225/60; c(1,6) = 72/60; c(1,7) = -10/60;
            assert(length(c) == (order+1));
        end
    end
end
