implemented analytical gradient

implemented the analytical gradient function, calculated the gradient by
hand