用 SymPy 从 RLC 与 RC 电路推导状态空间模型

状态空间方法把系统写成一组一阶微分方程,除了输入与输出之间的关系,还保留系统内部状态。对电路而言,电感电流和电容电压通常是自然的状态选择:它们与储能元件直接相关。

本文译写自 Electrical Problems using StateSpace。2026 年 10 月 05 日读取的页面标识为 SymPy 1.14.0,页脚更新日期为 2025 年 4 月 27 日;这不是本文首次发布日期。两个例子都做符号建模,没有数值仿真、频域绘图或实验验证。

两个模型的电路示意图:上方为输入电压驱动的串联 RLC,状态为电感电流 i 和电容电压 vC,输出为 vC;下方为两级等值 RC 网络,节点电压 v1 和 v2 为状态,输出为 v2。
依据原文物理连接关系重新绘制的串联 RLC 和双节点 RC 电路。两个 RC 电阻均为 R、两个电容均为 C;示意图不代表测量结果。制图:未完纪。

统一表示:先选状态,再确定输入与输出

线性系统的状态空间形式是:

ẋ(t) = A x(t) + B u(t)
y(t) = C_out x(t) + D u(t)

其中,x(t) 是状态向量,u(t) 是输入向量,y(t) 是输出向量。矩阵 A 描述状态之间的动态关系,B 描述输入怎样作用于状态,C_out 选择或组合要观测的状态,D 描述输入到输出的直接通道。

命名整理:原文同时用字母 C 表示电容量和输出矩阵,Python 代码也会把变量 C 从符号重新绑定为矩阵。本稿在公式中仍用 C 表示电容量,而将输出矩阵写为 C_out,Python 中电容量变量写为 C_cap。这只是命名修订,不改变模型。以下矩阵内含除法,物理模型要求相关电阻、电感、电容非零;实际无源元件通常取正值。

例一:串联 RLC 电路

电阻 R、电感 L 和电容 C 串联,输入是电压 v_in(t)。取电感电流 i(t) 与电容电压 v_C(t) 为状态,输出为电容电压。

从基尔霍夫电压定律得到电流方程

沿图中回路应用基尔霍夫电压定律(KVL),输入电压等于三个元件电压之和:

v_in(t) = R i(t) + L di(t)/dt + v_C(t)

把电流导数单独移到等式左侧,得到:

di(t)/dt = -(R/L) i(t) - (1/L) v_C(t) + (1/L) v_in(t)

电容电压与流过电容的电流满足积分关系。原文写为 v_C(t) = (1/C) ∫ i(t) dt;若显式写出初值,等价的完整表达是:

v_C(t) = v_C(t₀) + (1/C) ∫[t₀,t] i(τ) dτ
dv_C(t)/dt = i(t)/C

初值写法是本文的说明性补充。对积分关系求导后,得到的电容状态方程与原文相同。

把两个一阶方程整理成矩阵

定义:

x₁(t) = i(t)
x₂(t) = v_C(t)
x(t) = [x₁(t), x₂(t)]ᵀ
u(t) = v_in(t)
y(t) = v_C(t) = x₂(t)

状态方程为:

ẋ₁(t) = -(R/L)x₁(t) - (1/L)x₂(t) + (1/L)u(t)
ẋ₂(t) = (1/C)x₁(t)

因此四个矩阵是:

A = [ -R/L   -1/L ]      B = [ 1/L ]
    [  1/C     0  ]          [  0  ]

C_out = [ 0  1 ]        D = [ 0 ]

A 是 2×2 矩阵,B 是 2×1 矩阵,C_out 是 1×2 矩阵,D 是 1×1 矩阵。输出只选取第二个状态,没有输入到电容电压的直接通道。把这些矩阵放回统一表达式,即得到整个串联 RLC 的状态空间模型。

用 SymPy 构造模型并改写为传递函数

下面保留原例的矩阵及转换调用,采用前述清晰命名,并把通配符导入改为显式导入。两项差异都不会改变方程:

from sympy import Matrix, symbols
from sympy.physics.control import StateSpace, TransferFunction

R, L, C_cap = symbols("R L C")
A = Matrix([[-R/L, -1/L], [1/C_cap, 0]])
B = Matrix([[1/L], [0]])
C_out = Matrix([[0, 1]])
D = Matrix([[0]])

ss = StateSpace(A, B, C_out, D)
tf = ss.rewrite(TransferFunction)[0][0]

状态空间对象所包含的四个矩阵应与上式一致。原文给出的符号显示结果如下;这是一段来源输出,不是本文执行代码得到的结果:

StateSpace(Matrix([
[-R/L, -1/L],
[ 1/C,    0]]), Matrix([
[1/L],
[  0]]), Matrix([[0, 1]]), Matrix([[0]]))

TransferFunction(1, C*L*s**2 + C*R*s + 1, s)

rewrite(TransferFunction) 返回按输出与输入排列的传递函数结构;该例只有一个输入、一个输出,因此取 [0][0]。对应的传递函数是:

V_C(s) / V_in(s) = 1 / (L C s² + R C s + 1)

这里的传递函数描述零初始条件下的输入输出关系。非零初始电流或电容电压造成的自由响应,应结合状态初值另行处理。检查这个结果时,要始终记住输出选的是电容电压;改变输出矩阵后,传递函数也可能改变。

例二:双节点 RC 网络

第二个电路包含两个相同的电阻 R 和两个相同的电容 C。输入电压 u(t) 经第一个电阻到节点 v₁(t),第二个电阻连接 v₁(t) 与 v₂(t);每个节点各有一个电容接地。取节点电压作为状态:

x₁(t) = v₁(t)
x₂(t) = v₂(t)

节点 v₁ 的电流平衡

应用基尔霍夫电流定律(KCL),把从节点流向输入端、流入本节点电容和流向第二节点的电流相加:

(v₁(t) - u(t))/R + C dv₁(t)/dt + (v₁(t) - v₂(t))/R = 0

代入状态变量并合并同类项:

(x₁(t) - u(t))/R + C dx₁(t)/dt + (x₁(t) - x₂(t))/R = 0
C ẋ₁(t) = -2x₁(t)/R + x₂(t)/R + u(t)/R
ẋ₁(t) = -2x₁(t)/(RC) + x₂(t)/(RC) + u(t)/(RC)

节点 v₂ 的电流平衡

第二个节点只有接地电容和通向第一个节点的电阻支路,因此:

C dv₂(t)/dt + (v₂(t) - v₁(t))/R = 0
C dx₂(t)/dt + (x₂(t) - x₁(t))/R = 0
C ẋ₂(t) = x₁(t)/R - x₂(t)/R
ẋ₂(t) = x₁(t)/(RC) - x₂(t)/(RC)

构造状态空间对象

输出定义为第二个节点电压,即 y(t)=v₂(t)=x₂(t),所以矩阵为:

A = [ -2/(RC)   1/(RC) ]       B = [ 1/(RC) ]
    [  1/(RC)  -1/(RC) ]           [    0   ]

C_out = [ 0  1 ]               D = [ 0 ]

相应的 SymPy 代码如下。本例与原文一样省略 D 参数,状态空间对象显示的直接传递矩阵为零:

from sympy import Matrix, symbols
from sympy.physics.control import StateSpace

R, C_cap = symbols("R C")
A = Matrix([
    [-2/(R*C_cap), 1/(R*C_cap)],
    [1/(R*C_cap), -1/(R*C_cap)]
])
B = Matrix([[1/(R*C_cap)], [0]])
C_out = Matrix([[0, 1]])

ss = StateSpace(A, B, C_out)

原文的模型显示结果为:

StateSpace(Matrix([
[-2/(C*R),  1/(C*R)],
[ 1/(C*R), -1/(C*R)]]), Matrix([
[1/(C*R)],
[      0]]), Matrix([[0, 1]]), Matrix([[0]]))

两个对角项分别表示各节点电压自身的衰减,非对角项表示两个节点通过中间电阻耦合。输入只直接出现在第一条状态方程中,而输出取第二个节点。这也给了我们逐项对照电路与矩阵的检查方法。

代码整理与验证边界

原文把 Python 变量 C 重新赋为输出矩阵后,先前构造的 SymPy 表达式仍保留原来的电容量符号,所以原例可以显示出正确矩阵。但后续继续使用变量 C 时容易把矩阵当电容量。本文用 C_cap 与 C_out 消除歧义,并使用显式导入。这是可读性与维护性修订,没有添加数值参数或宣称性能改进。

本次仅静态核对 KVL/KCL、状态次序、矩阵维度、符号系数与来源输出。代码中的符号均由程序直接定义,没有读取不可信文本并送入求值器,也没有硬编码秘密、联网或文件操作。未安装或运行 SymPy;没有运行测试,也没有实物电路验证。静态核对没有发现这些具体问题,不代表代码或依赖不存在漏洞。

来源、参考文献与许可

原作者归属:SymPy 文档贡献者;原文未列单篇作者,页脚为 Copyright © 2025 SymPy Development Team。原文参考资料保留为 BMS College of Engineering:STATE SPACE ANALYSIS。

本文为未完纪中文译写,示意图重新绘制。所引 SymPy 示例代码随项目采用 BSD 许可条件,文末附 SymPy 1.14 的 LICENSE 文本,保留版权、条件与免责声明。项目开源状态并不代表所有第三方材料均适用同一许可。

SymPy 示例代码许可及原始第三方通知全文

Copyright (c) 2006-2023 SymPy Development Team

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
  a. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
  b. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  c. Neither the name of SymPy nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
--------------------------------------------------------------------------------

Patches that were taken from the Diofant project (https://github.com/diofant/diofant)
are licensed as:

Copyright (c) 2006-2018 SymPy Development Team,
              2013-2023 Sergey B Kirpichev

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
  a. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
  b. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  c. Neither the name of Diofant or SymPy nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
--------------------------------------------------------------------------------

Submodules taken from the multipledispatch project (https://github.com/mrocklin/multipledispatch)
are licensed as:

Copyright (c) 2014 Matthew Rocklin

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
  a. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
  b. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  c. Neither the name of multipledispatch nor the names of its contributors
     may be used to endorse or promote products derived from this software
     without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
--------------------------------------------------------------------------------

The files under the directory sympy/parsing/autolev/tests/pydy-example-repo
are directly copied from PyDy project and are licensed as:

Copyright (c) 2009-2023, PyDy Authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* Neither the name of this project nor the names of its contributors may be
  used to endorse or promote products derived from this software without
  specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PYDY AUTHORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------

The files under the directory sympy/parsing/latex
are directly copied from latex2sympy project and are licensed as:

Copyright 2016, latex2sympy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容