Functions for calculating the cost function around a single dipole (not working yet)