added magnitude calculation function