首页 > 解决方案 > 图中的并联电阻(电路的节点表示)

问题描述

所以我有这个对象:电阻器、电压源、节点和图表。我的主要目标是从电路中表示一些图表,我遇到了这个问题:当我添加从 Node2 到 NodeRef 的并行节点的权重时,显然 R2 电阻被 R3 电阻取代。有什么建议可以在没有多图的情况下实现这个吗?

def create_components(self):

    NodeRef = Node("0")
    Node1 = Node("1")
    Node2 = Node("2")
  
    R1  = Resistor("R1", 100) #R1.get_val() returns the value in ohms
    R2  = Resistor("R2", 200)
    R3  = Resistor("R3", 300)
    V1 = Voltage("Source", 5)

    NodeRef.add_destination(Node1, 0) # Node.add_destination(destination, weight)
    Node1.add_destination(Node2, R1.get_val())        
    Node2.add_destination(NodeRef, R2.get_val())
    Node2.add_destination(NodeRef, R3.get_val())

因为我还不能发布图片,所以电路原理图是这样的:

|-------R1------------
|             |      |
V1            R2     R3
|             |      |
|---------------------

我想用图表表示的电路:

我想用图表表示的电路

标签: pythondata-structuresnodes

解决方案


跟踪两个节点之间所有电导的总和。电导是电阻的倒数。平行电导正常相加。要获得净电阻,只需返回总跟踪电导和的倒数。

class Node():
    def __init__(self,label):
        self.label = label
        self.connections = {}
    def add_destination(self,other,weight):
        if not other.label in self.connections:
            self.connections[other.label] = 0
        self.connections[other.label] += (1.0/weight if weight !=0 else float('inf'))
        other.connections[self.label] = self.connections[other.label]
    def resistance(self,other):
        if other.label in self.connections and self.connections[other.label] != 0:
            return 1.0/self.connections[other.label]
        return float('inf')

推荐阅读