gpt4 book ai didi

python - 使用SciPy的线性代数方法求解三个联立方程

转载 作者:行者123 更新时间:2023-12-01 06:37:00 24 4
gpt4 key购买 nike

Question

我的问题是如何将这些方程写成数组并求解?

from scipy import linalg
import numpy as np
import matplotlib.pyplot as plt

x = np.array[-23,1100,2300],[2300,1500,550],[550,1600,]

我尝试在上面的数组中写入,但我不知道如何替换问题中的“In”和“Vs2”。你能帮我解决这个问题吗?

最佳答案

您想要针对多个电压求解这些方程,这建议使用 for 循环。为了清楚起见,通常最好使用值标识符,例如,使用 R1 而不是 1100。将 R1 放入公式中,让计算机为您执行简单的算术运算。

您可能正在考虑使用 linalgsolve 函数,因为您需要求解三阶方阵。未知数就是潮流。因此,进行代数运算,以便获得矩阵系数以及方程右侧以电阻和电压表示的表达式。

对于矩阵(如 https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve.html#scipy.linalg.solve 文档中所示),

a = np.array([[f1(Rs, Vs), f2(Rs, Vs), f3(Rs, Vs)], [...], [...]])

对于右侧的向量,

b = np.array([f4(Rs, Vs), f5(Rs,Vs), f6(Rs, Vs)])

然后电流=求解(a, b)

请注意,f1、f2 等是您必须进行代数计算的函数。

现在将此代码放入循环中,或多或少像这样:

for vs2 in [10,15,20,25]:
currents = solve(a, b)

因为代数表达式中有电阻和 vs2,所以您将得到相应的电流。您需要收集与电压相对应的电流以进行绘图。

加法:代数运算的部分结果:

nothing more to say

更多:我如何使用 sympy 库避免大多数讨厌的代数:

>>> R1, R2, R3, R4, R5, Vs1 = 1100, 2300, 1500, 550, 1600, 23
>>> from sympy import *
>>> var('I1,I2,I3,Vs2')
(I1, I2, I3, Vs2)
>>> eq1 = -Vs1 + R1*I1 + R2 * (I1-I2)
>>> eq1
3400*I1 - 2300*I2 - 23
>>> eq2 = R2*(I2-I1)+R3*I2+R4*(I2-I3)
>>> eq2
-2300*I1 + 4350*I2 - 550*I3
>>> eq3 = R4*(I3-I2)+R5*I3 + Vs2
>>> eq3
-550*I2 + 2150*I3 + Vs2
>>> from scipy import linalg
>>> import numpy as np
>>> for Vs2 in [10,15,20,25]:
... ls = np.array([[3400,-2300,0],[-2300,4350,-550],[0,-550,2150]])
... rs = np.array([23, 0, -Vs2])
... I = linalg.solve(ls, rs)
... Vs2, I
...
(10, array([ 0.01007914, 0.0048996 , -0.00339778]))
(15, array([ 0.00975305, 0.00441755, -0.00584667]))
(20, array([ 0.00942696, 0.0039355 , -0.00829557]))
(25, array([ 0.00910087, 0.00345346, -0.01074446]))

关于python - 使用SciPy的线性代数方法求解三个联立方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com