added gradient functions and step1matrix