coded the gradient function