In [1]:
from lcapy import Circuit, t
cct = Circuit("""
V1 1 0_1 8; down
R1 1 x 3; right, size=1.5, i=I
R2 x 2 1; right, size=1.5
V2 2 0_2 4; down
W 0_1 0; right
W 0 0_2; right
Ox x 0; down, v=V_x
""")
cct.draw(label_ids=True)


Let's determine the current through R1. There are many ways to solve this; the easiest is to combine the sources, combine the resistances, and then use Ohm's law. You can also use the principle of superposition.


In [2]:
cct.R1.i


Out[2]:
$$1$$

Not surprisingly due to KCL, this is the same as the current through R2.


In [3]:
cct.R2.i


Out[3]:
$$1$$

Now given the current, we can use Ohm's law to determine the voltage drop across R1.


In [4]:
(cct.R1.Z * cct.R1.I).time()


Out[4]:
$$3$$

Lcapy can determine this directly.


In [5]:
cct.R1.v


Out[5]:
$$3$$

Using KVL, the unknown voltage is thus the voltage generated by the source V1 minus the voltage drop across R1.


In [6]:
cct.V1.v - cct.R1.v


Out[6]:
$$5$$

Of course, Lcapy can determine this directly. Here Ox is the name of the open circuit over which we wish to determine the voltage difference.


In [7]:
cct.Ox.V


Out[7]:
$$\left\{ t : 5\right\}$$

Alternatively, we can query Lcapy for the voltage at node 'x' with respect to ground.


In [8]:
cct['x'].v


Out[8]:
$$5$$

Finally, we should check that the answer makes sense. It is between 4 and 8 V and so is plausible. Oh, we should also specify the units for each of our answers. This helps to find errors.

Let's now try using superposition. We start by analysing the circuit with all the sources except V1 killed.


In [9]:
cct2 = cct.kill_except('V1')
cct2.draw(label_ids=True)


This is a simpler circuit to analyse. We can either recognise the circuit as a voltage divider or calculate the current and find the voltage drops. Let's use the latter approach. Combining the resistances gives 4 ohms and using Ohm's law, the current through R1 and R2 is given by:


In [10]:
cct2.V1.v / 4


Out[10]:
$$2$$

Lcapy can determine this directly:


In [11]:
cct2.R2.i


Out[11]:
$$2$$

The voltage drop across R2 can then be found from Ohm's law.


In [12]:
cct2.R2.Z


Out[12]:
$$1$$

In [13]:
(cct2.R2.I * cct2.R2.Z)(t)


Out[13]:
$$2$$

Again, Lcapy can determine this directly:


In [14]:
cct2['x'].v


Out[14]:
$$2$$

So we have determine the voltage Vx due to source V1. Let's now kill just V2 and determine the voltage Vx due to V2.


In [15]:
cct3 = cct.kill_except('V2')
cct3.draw(label_ids=True)


In this circuit the current flows in the opposite direction to the labelled current I. Thus we will get a negative value for the current. If you do not like negative currents, just relabel the current direction.


In [16]:
cct3.R1.i


Out[16]:
$$-1$$

The unknown voltage Vx can be found as before, either by using the principle of the voltage divider or by calculating the current through R1 and R2 and then determining the voltage dropped across R2 (taking into account that the current flows in the opposite direction to that shown):


In [17]:
(cct3.V2.V + cct3.R2.I * cct3.R2.Z)(t)


Out[17]:
$$3$$

Alternatively, the voltage drop across R1 (taking into account the current direction) is:


In [20]:
(-cct3.R1.I * cct3.R1.Z)(t)


Out[20]:
$$3$$

We now know Vx due to V1 and Vx due to V2. Due to linearity, the total voltage Vx is simply the sum of the two contributions:


In [19]:
cct2['x'].v + cct3['x'].v


Out[19]:
$$5$$

This is the same as the result determined earlier.