πŸ’» Activity: Create a Class for Parallel Circuits#

We built a class for circuits in series. Use what you learned last week about circuits with resistors in parallel and what you now know about classes to construct a class for circuits in parallel.

Consider this circuit with 9 volts of energy and three different resistors in parallel.

image

  1. Build a class for a parallel type circuit called ParallelCircuit that

  • holds information about a circuit with resistors in parallel and

  • calculates the total resistance, current, and voltages across each resistor.

  1. Add to the ParallelCircuit class a method that allows the resistance of each resistor to be associated with the circuit.

  2. Add to the ParallelCircuit class a method to the class to calculate the total resistance.

  3. Add to the ParallelCircuit class methods that calculate the current flowing through the circuit or the voltage across each resistor.

# create a class object
class ParallelCircuit:
    # create a constructor for the ParallelCircuit class
    def __init__(self):
        self.components = []  # List to store resistances of the circuit components

    # create a method that adds a resistor to an instantiation of the ParallelCircuit class
    def add_resistor(self, resistance):
        """Add a resistor to the circuit."""
        self.components.append(resistance)

    # create a method that sums the list `components` of an instantiation of the ParallelCircuit class
    def total_equivalent_resistance(self):
        """Calculate the total equivalent resistance in the parallel circuit."""
        inverse_total = sum([1.0 / r for r in self.components])
        if inverse_total > 0:
            return 1.0 / inverse_total
        else:
            return float('inf')  # Infinite resistance if no components

    # create a method that calculates the current flowing through an instantiation of the ParallelCircuit class
    def circuit_current(self, voltage):
        """Calculate the total current flowing from the voltage source."""
        equivalent_resistance = self.total_equivalent_resistance()
        if equivalent_resistance > 0:
            return voltage / equivalent_resistance
        else:
            return 0

    # create a method that calculates the voltage across each resistor an instantiation of the ParallelCircuit class
    def component_currents(self, voltage):
        """Calculate the current through each resistor in the parallel circuit."""
        return [voltage / r for r in self.components]
  1. Apply this class to revisit the circuit with 9 volts of energy and three different resistors in parallel that we considered with nested lists last week.

image

# instantiate the class
circuit = ParallelCircuitCircuit()

# use the add_resistor method to construct the circuit
circuit.add_resistor(10000)  # Add a 3000 ohm resistor
circuit.add_resistor(2000)  # Add a 10000 ohm resistor
circuit.add_resistor(1000)   # Add a 5000 ohm resistor
# apply the total_resistance method to the instantiation of the class
total_resistance = circuit.total_resistance()
print(f"Total Resistance: {total_resistance} Ohms")

# apply the circuit_current method to the instantiation of the class
voltage_source = 9  # 9V voltage source
current = circuit.circuit_current(voltage_source)
print(f"Circuit Current: {current} A")

# apply the component_voltages method to the instantiation of the class
voltages = circuit.component_voltages(voltage_source)
print(f"Voltages across each component: {voltages}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 2
      1 # apply the total_resistance method to the instantiation of the class
----> 2 total_resistance = circuit.total_resistance()
      3 print(f"Total Resistance: {total_resistance} Ohms")
      5 # apply the circuit_current method to the instantiation of the class

NameError: name 'circuit' is not defined