Now integrates over energy conservation for cost function.