Circuit Simulation

I am making a circuit simulation program, and I am not sure how to simulate the behavior of Diodes.

Take this circuit:
------100 Resistor -----LED------
Positive 5V-----470 Resistor-----|                                                     |---------Negative 5V
-----------200 Resistor----------

How would you calculate the voltage and current through all of these components?

Also, is it true that with a simple circuit like this:

Positive 5V------470 Resistor------LED--------Negative 5V

That you would subtract the forward voltage of the LED from the Battery's voltage, then use Ohm's law to calculate the current and resistance through the circuit?

voltage = 5;
newVoltage = voltage - forwardVoltage;
current = newVoltage / 470;
resistance = voltage / current;

Thanks for the help.

-
Trying to format with non-breaking space is intrinsically lossy, because you don't know anything about the font you test is displayed in. Use the code formatting tool (the {} button) or html pre tags to force a fixed width font. Better still, use a graphics tool of some kind and upload the image. – dmckee Sep 4 '11 at 21:13
Now, concerning you question: do you know how to figure these things by hand? If not, perhaps this would be better on Electronics.SE, and if you do perhaps this is more appropriate for Stack Overflow. Also, is there a particular reason for not just using spice? – dmckee Sep 4 '11 at 21:15
I posted on electronics.stackexchange and physics.stackexchange - I wasn't sure which one to do. I don't know how to do this by hand. This is for a high school independent study. – user434565 Sep 4 '11 at 21:22

First, for a good answer in a circuit that depends on the diode or LED properties, you should really involve the current-voltage curve of the diode from the datasheet. This is the curve where you can read off the current through it at a given voltage. The "Vf" (forward voltage) is only an approximate voltage where the diode start to conduct.

Anyway, if you just need an approximate solution, use Kirschoffs laws to set up a equation system for the circuit. These say that the current in and out of a single node in the circuit has to be zero, and the voltage drops along a closed curve in the circuit has to be zero. You label your circuit with currents along each continous segment for example, and create equations for it using ohms law for the resistors and the Vf for the diode to model the voltage drops, and create as many equations as you have unknowns preferably, then you solve the equation system.

In your first circuit, you for example have a current $Ia$ through the 470 ohm resistor, then current $Ib$ through the 100 ohm and LED, and current $Ic$ through the 200 ohm resistor.

The voltage drop along the 470 ohm is then $470*Ia$, the voltage drop along the LED segment is $100*Ib+Vf$ and along the lower segment $200*Ic$.

Kirschoffs laws then give you: $Ia = Ib + Ic$, $100*Ib+Vf = 200*Ic$, and finally $470*Ia+200*Ic=10$ (or equally, $470*Ia+100*Ib+Vf=10$). Three equations, three unknown currents.

Actually making the program is a stackoverflow question, but it would essentially have to have a graph structure of the circuit and traverse it to find enough equations to solve for all the unknowns. The design of such a program is a more or less open-ended question because you can make it more or less accurate (see spice). The above is just a trivial example really.

-
Thanks that was all I needed - the programming is the easy part :) – user434565 Sep 6 '11 at 6:05