Calculating gradient and displacement vector