图形中的并联电阻器(电路的节点表示法)

2024-07-05 08:59:01 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有这些对象:电阻器,电压源,节点和图形。我的主要目标是从一个电路中表示一些图形,我有一个问题:当我为从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
|             |      |
|---------------------

我想用图形表示的电路:

The circuit I want to represent as a graph


Tags: addnode图形get节点valdestination电路
1条回答
网友
1楼 · 发布于 2024-07-05 08:59:01

跟踪两个节点之间所有导体的总和。电导是电阻的倒数。并联导体通常相加。要得到净电阻,只需返回总电阻和的倒数

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')

相关问题 更多 >