function [xp,fp] = bfd(x,y,n)
% Computes the nth derivate of y(x) using backward finite difference
% using the basic formula O(h)
% n can be from 1 to 4
% assumes equal spaces between x-values

h = x(2)-x(1);
switch n
    case 1
        fp = -(y(1:end-1)-y(2:end))./h;
        xp = x(2:end);
    case 2
        fp = (y(1:end-2)-2.*y(2:end-1)+y(3:end))./(h.^2);
        xp = x(3:end);
    case 3
        fp = -(y(1:end-3)-3.*y(2:end-2)+3.*y(3:end-1)-y(4:end))./(h.^3);
        xp = x(4:end);
    case 4
        fp = (y(1:end-4)-4.*y(2:end-3)+6.*y(3:end-2)-4.*y(4:end-1)+y(5:end))./(h.^4);
        xp = x(5:end);
end