python sympy 解方程怎样把狄克拉函数定义出来

physics - Solving RC circuit with python/sympy - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I'm working independently out the the book Computational Physics by Mark Newman, exercise 6.5 to be specific (it can be downloaded ). The problem is to solve for node voltages in an RC circuit driven by a complex exponential source voltage.
What I would like to do is turn my node equations into the form Av = B. Here is what I'm doing so far.
import numpy as np
import sympy
# Define constants
R1=R3=R5 = 1e3
R2=R4=R6 = 2e3
C2 = 0.5e-6
x_plus = 3
x_minus = 0
# Define relations
V_plus = sympy.symbols('V_plus')
t = sympy.symbols('t')
V_plus = x_plus*sympy.exp(1j*w*t)
V_minus = 0
V1,V2,V3 = sympy.symbols('V1 V2 V3')
V1_eqn = (V1-V_plus)/R1 + (V1-V2)/(1/(1j*w*C1)) + (V1-V_minus)/R4
V2_eqn = (V2-V_plus)/R2 + (V2-V1)/(1/(1j*w*C1)) + (V2-V_minus)/R5 + (V2-V3)/(1/(1j*w*C2))
V3_eqn = (V3-V_plus)/R3 + (V3-V2)/(1/(1j*w*C2)) + (V3-V_minus)/R6
V = [V1,V2,V3]
V_vec = np.matrix(V)
A = np.matrix([
[V1_eqn.expand().coeff(x) for x in V],
[V2_eqn.expand().coeff(x) for x in V],
[V3_eqn.expand().coeff(x) for x in V]
Now, the problem I'm having is extracting the constant terms of the three equations so I can load them into B. Among other things, one of the issues is that the "constant term" isn't actually constant. It's a complex exponential function of 't'. Since this is a fairly straightforward problem with pen and paper, I know exactly what the constant term is going to look like, so I can extract it. My goal though is to solve this in a way that doesn't require).
One method I've considered is taking each equation and subtracting off all the terms that I've already put in Av, but this seems a little sloppy and not very pythonic. Does anyone know of a better method for extracting constant terms that aren't technically constant? Thanks.
Except for some shortcuts in notation, subtracting the terms you've already put into A is exactly what you should, and must, do.
Here's a slightly shorter version:
import sympy
# Define constants
R1=R3=R5 = 1e3
R2=R4=R6 = 2e3
C2 = 0.5e-6
x_plus = 3
x_minus = 0
# Define relations
V_plus = sympy.symbols('V_plus')
t = sympy.symbols('t')
V_plus = x_plus*sympy.exp(1j*w*t)
V_minus = 0
V1,V2,V3 = sympy.symbols('V1 V2 V3')
# Using a matrix to collect all equations allows you to use a matrix-vector-product below
eqns = sympy.matrices.Matrix([
(V1-V_plus)/R1 + (V1-V2)/(1/(1j*w*C1)) + (V1-V_minus)/R4,
(V2-V_plus)/R2 + (V2-V1)/(1/(1j*w*C1)) + (V2-V_minus)/R5 + (V2-V3)/(1/(1j*w*C2)),
(V3-V_plus)/R3 + (V3-V2)/(1/(1j*w*C2)) + (V3-V_minus)/R6
V = [V1,V2,V3]
V_vec = sympy.matrices.Matrix(V)
# The "or 0" does nothing if there are coefficients y in x, but if there aren't, it replaced
# the "None" with "0". This again is required for using A * V_vec below
A = sympy.matrices.Matrix([ [ x.expand().coeff(y) or 0 for y in V ] for x in eqns ])
remainder = (eqns - A * V_vec)
remainder.simplify()
print remainder
You can just solve it directly with sympy.solve:
import sympy as sp
R1=R3=R5 = 1e3
R2=R4=R6 = 2e3
C2 = 0.5e-6
t = sp.Symbol('t')
E = sp.exp(1j*w*t)
V1, V2, V3 = sp.symbols('V1 V2 V3')
V_plus = V0*E
V_minus = 0
V1_eqn = (V1-V_plus)/R1 + (V1-V2)/(1/(1j*w*C1)) + (V1-V_minus)/R4
V2_eqn = (V2-V_plus)/R2 + (V2-V1)/(1/(1j*w*C1)) + (V2-V_minus)/R5 + (V2-V3)/(1/(1j*w*C2))
V3_eqn = (V3-V_plus)/R3 + (V3-V2)/(1/(1j*w*C2)) + (V3-V_minus)/R6
print sp.solve([ sp.Eq(V1_eqn, 0), sp.Eq(V2_eqn, 0), sp.Eq(V3_eqn, 0)], [V1, V2, V3] )
27.7k53072
Substituting zeros is a good way to get the constants that remain. Also, the jacobian method is a more "mathematical" way of doing what you were doing with the coeff method.
&&& eqs = sympy.Matrix((V1_eqn, V2_eqn, V3_eqn))
&&& eqs.jacobian(V)
[0.0015 + 0.001*I,
-0.001*I, 0.0015 + 0.0015*I,
-0.0005*I],
-0.0005*I, 0.0015 + 0.0005*I]])
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledpython3怎么安装sympy和matplotlib模块(ubuntu系统)?_百度知道
python3怎么安装sympy和matplotlib模块(ubuntu系统)?
提问者采纳
就是下载源代码,把安装时候用的pyt畅互扳就殖脚帮协爆茅hon命令改成python3就可一了python3-sympy直接取官方网站下载for py3.2的包安装
其他类似问题
为您推荐:
ubuntu的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁&&国之画&&&& &&&&
版权所有 京ICP备号-2
迷上了代码!}

我要回帖

更多关于 狄克拉 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信