# KCL vs KVL in circuit analysis

I'm making a simplified version of SPICE to teach basic electronics to high school students (This is for a high school independent study).

Why does SPICE analyze circuits using Kirchhoff's current law instead of Kirchhoff's voltage law? Usually you have a voltage source to start with instead of a current source, and it is easier and more efficient to analyze a circuit using voltage instead of current.

Using voltage to analyze circuit: (See best answer) Circuit Simulation

Using current to analyze circuit: http://en.wikipedia.org/wiki/Nodal_analysis

Thanks!

The nodal analysis (KCL) and mesh analysis (KVL) will give equivalent results when analyzing any given circuit...Though of course there are different special cases for each one (a voltage source doesn't map neatly into KCL, while a current source doesn't map neatly into KVL, for example).

One difference is that it's relatively easy to automatically set up your equations for KCL. Each node (except GND) has one associated equation. So as the simulator is scanning the netlist, whenever a node is mentioned as connected to a device, either a term is added to the equation for that node, or a new equation is set up if the node was never mentioned before.

To set up the equations for KVL, you have to do some additional analysis to find all of the loops in the circuit so as to build an equation for each loop. Nowadays there are probably well-established graph analysis algorithms to identify those loops. But when the first SPICE simulators were written, in the early 1970's, even if those algorithms did exist (probably many of them actually did), implementing them would have meant substantial extra work for the grad students who were developing SPICE.

• Speaking as a programmer, this makes sense. Enumerating through the nodes, which are already just sitting there in the net list is a gimme; whereas to KVL equations would require setting up ancillary data structures to identify all the loops. Commented Nov 27, 2011 at 12:43
• Also remember the original SPICE was written in FORTRAN...where data structures other than arrays were not well supported. Commented Nov 28, 2011 at 0:11

I'll add some additional detail on the mechanics of how the equations are obtained.

Many SPICE algorithms use what are called "element stamps" to create the equation for a node. The relevant stamp is selected based on the type of component and then the appropriate terms are added to the appropriate locations in the Nodal Matrix. For a nice explanation of a couple of formulation methods for SPICE, see these lecture slides. Slides starting at #21 illustrate the stamp method for a Resistor, a voltage controlled current source and an independent current source.

Finally, when voltage sources are present, the nodal method needs to be expanded by adding an additional equation to the matrix (slides 26-27). While the final example uses hand equations, the same resulting matrix can be obtained by through the addition of the applicable element stamps.

I think SPICE uses KCL because it is easier to solve matrix equations this way. Using KVL leaves unknowns and makes it more difficult to perform Gaussian elimination.
The link I posted in my reply to your previous question mentions this and gives an example (pages 9-10)

Edit - about the diode voltage/current derivative, I noticed at the bottom of the first document it says "next time we will look at the diode model".
I tried going back to the /docs part of the address but no luck. So I tried adding 1 to the document number and by good fortune this appeared, which I think is the diode document. Hopefully it may be of some help.

• By the way, thank a bunch for the link, it's extremely helpful. Right now I'm having trouble with getting the derivative of the voltage-current function for diodes... Commented Sep 8, 2011 at 1:05
• No problem, wish I could help with something more but I'm far from being knowledgeable in this area, just interested in the workings of something I use all the time. I updated the answer with another link relating to diodes. Commented Sep 8, 2011 at 1:23
• Oh, the derivative of an exponential function e^x is actually e^x - that's what confused me. Thanks for the links! Commented Sep 8, 2011 at 1:31
• Back to my question though, I see that in the article it says that you cannot analyze the voltages just using KVL, however in the "best answer" at my last question electronics.stackexchange.com/questions/19068/… it is shown how to do it. Commented Sep 8, 2011 at 1:35