Can't figure out how to use pwm with an optocoupler and a MOSFET

tempo1218:
How are resistors (R2 and R3 ) chosen for mosfets? How are they calculated?

Sheer necromancy!

R2 and R3 relate to the fact that the gate of the FET is highly capacitive and its switching speed is limited by how fast you can charge and discharge the gate. R2 in particular needs to be fairly low - you can calculate just how fast you need it to switch and work on that and the gate capacitance to derive the value.

R3 is a curious matter - I am not quite sure what it does. The opto-coupler has a transfer factor, so the actual current it can draw is constrained, but if it is commutating rapidly, it may be preferable to have it saturate in order to pull the gate down, so that its power dissipation is minimised.