Varying the internal resistance of a circuit

I want to vary the internal resistance of a circuit. I plan on using a MOSFET in the ohmic range to do this, whereby the gate-source voltage controls the drain-source voltage. In order to control the gate-source voltage of the MOSFET, I plan on using PWM and vary the duty cycle. Does anyone know if this will work and is there is a code already available?

This will be tough to do right. In the ohmic range the resistance changes very steeply, and it changes with load current, so it will be tough to keep constant without a feedback control loop.

Depending on your application (what is your application? voltage? current?) it might be better to use a digital potentiometer (ex. MCP42010).

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

It also depends on the temperature...
This is not how transistors work...

As R.C. said, you can try to measure the source current and feed back. Assumed you have a constant voltage...

Also, this is not a typical PWM application. You have to low pass filter the PWM at the gate.

You will have to use a filter on the PWM:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

whereby the gate-source voltage controls the drain-source voltage

No the gate source voltage controls the drain source resistance not the voltage. If you have a drain load resistor then the potential divider action will allow a changing voltage at the drain.

You will have to be careful of the power dissipation of a circuit like this because you will have a significant resistance in the FET and any amount of current will push up the power the FET has to dissipate.
So there will be design considerations but basically you can do this.

If you give a few more detailed then we can be more specific.

You should look through some datasheets. Interesting curves will look like those in this article

You want to work in a part of the "ohmic region" as it's called in figure 7.
This needs very low source drain voltages... try to find some values so you get a feeling of the range..
The gate threshhold voltage is not very sharply defined, and there is a non-linearity at the beginning...