Determination of the anhysteretic magnetization in the presence of a demagnetizing field