Smoothing PWM for electromagnet

Again.

Wawa:
What was wrong with the default PWM frequency.

What is so special about this electromagnet that it needs better than 8-bit PWM, and what was wrong with Arduino's default ~500 or ~1000hz PWM frequency.
analogWrite (5, 128); // ~1khz50%PWM on pin5

tlhsglm:
@Wawa Will the heat at lethal degrees for Mosfet?Are you know a way to make this?

You could be looking at 1watt dissipation in the mosfet and a 60C temp rise above ambient if you don't use a heatsink. Analogue/voltage control is usually a poor solution.
As said, PWM is more effficient. You just need to use the right PWM frequency and resolution.
High frequency and high resolution might not go together though.
Leo..