问题描述
你能告诉我吗,我需要在 python 中使用 sympy 符号求解线性方程组.我已经编写了自动创建线性方程组的程序,方程的数量由数字 n(预定义)决定.我也设置了变量.结果如下:
Can you please tell me, I need to symbolically solve a system of linear equations using sympy in python. I have programmed the automatic creation of a system of linear equations, the number of equations is determined by the number n (predefined). I set the variables too.it turns out the following:
########## system of linear equations ##########
[-mui0*exp(Ea0**2/(T0*k)) - mui0*exp(Ea1**2/(T0*k)) - mui0*exp(Ea2**2/(T0*k)) - mui0*exp(Ea3**2/(T0*k)) + (Eb0**2/(T0*k) - 2*Ec0)*exp(Ea0**2/(T0*k)) + (Eb1**2/(T0*k) - 2*Ec1)*exp(Ea1**2/(T0*k)) + (Eb2**2/(T0*k) - 2*Ec2)*exp(Ea2**2/(T0*k)) + (Eb3**2/(T0*k) - 2*Ec3)*exp(Ea3**2/(T0*k)), -mui1*exp(Ea0**2/(T1*k)) - mui1*exp(Ea1**2/(T1*k)) - mui1*exp(Ea2**2/(T1*k)) - mui1*exp(Ea3**2/(T1*k)) + (Eb0**2/(T1*k) - 2*Ec0)*exp(Ea0**2/(T1*k)) + (Eb1**2/(T1*k) - 2*Ec1)*exp(Ea1**2/(T1*k)) + (Eb2**2/(T1*k) - 2*Ec2)*exp(Ea2**2/(T1*k)) + (Eb3**2/(T1*k) - 2*Ec3)*exp(Ea3**2/(T1*k)), -mui2*exp(Ea0**2/(T2*k)) - mui2*exp(Ea1**2/(T2*k)) - mui2*exp(Ea2**2/(T2*k)) - mui2*exp(Ea3**2/(T2*k)) + (Eb0**2/(T2*k) - 2*Ec0)*exp(Ea0**2/(T2*k)) + (Eb1**2/(T2*k) - 2*Ec1)*exp(Ea1**2/(T2*k)) + (Eb2**2/(T2*k) - 2*Ec2)*exp(Ea2**2/(T2*k)) + (Eb3**2/(T2*k) - 2*Ec3)*exp(Ea3**2/(T2*k)), -mui3*exp(Ea0**2/(T3*k)) - mui3*exp(Ea1**2/(T3*k)) - mui3*exp(Ea2**2/(T3*k)) - mui3*exp(Ea3**2/(T3*k)) + (Eb0**2/(T3*k) - 2*Ec0)*exp(Ea0**2/(T3*k)) + (Eb1**2/(T3*k) - 2*Ec1)*exp(Ea1**2/(T3*k)) + (Eb2**2/(T3*k) - 2*Ec2)*exp(Ea2**2/(T3*k)) + (Eb3**2/(T3*k) - 2*Ec3)*exp(Ea3**2/(T3*k))]
###### vars ######
[Ea0, Ea1, Ea2, Ea3, Ea4, Eb0, Eb1, Eb2, Eb3, Eb4, Ec0, Ec1, Ec2, Ec3, Ec4]
###### ###### ###### ###### ###### ###### ###### ######
现在当我写:a = linsolve(list_eq,list_var)
the error gets out : SympifyError: Sympify of expression 'could not parse '['' failed, because of exception being raised:
TokenError: ('EOF in multi-line statement', (2, 0))
如何为 sympy 找到线性方程组的解
how to be For sympy to find a solution to a system of linear equations
推荐答案
你会笑,但是发现一个错误:首先,我从列表中做了一个元组,一些错误消失了;但最有趣的是,当我明确设置方程和变量时,simpi 无法求解方程:
you will laugh, but an error was found: firstly, I made a tuple from list, some of the errors were gone; but the most interesting thing is that when I explicitly set equations and variables, simpi cannot solve the equations :
n = 4
T = sp.symbols('T:' + str(n+1))
Ea0, Ea1, Ea2, Ea3, Ea4, Eb0, Eb1, Eb2, Eb3, Eb4, Ec0, Ec1, Ec2, Ec3, Ec4, T0, T1, T2,
T3, T4, mui0, mui1, mui2, mui3, mui4 = sp.symbols('Ea0, Ea1, Ea2, Ea3, Ea4, Eb0, Eb1,
Eb2, Eb3, Eb4, Ec0, Ec1, Ec2, Ec3, Ec4, T0, T1, T2, T3, T4, mui0, mui1, mui2, mui3,
mui4')
eq_one=sp.Eq(-mui0*exp(Ea0**2/(T0*k)) - mui0*exp(Ea1**2/(T0*k)) -
mui0*exp(Ea2**2/(T0*k)) -
mui0*exp(Ea3**2/(T0*k)) + (Eb0**2/(T0*k) - 2*Ec0)*exp(Ea0**2/(T0*k)) +
(Eb1**2/(T0*k) - 2*Ec1)*exp(Ea1**2/(T0*k)) + (Eb2**2/(T0*k) -
2*Ec2)*exp(Ea2**2/(T0*k)) +
(Eb3**2/(T0*k) - 2*Ec3)*exp(Ea3**2/(T0*k)))
eq_two=sp.Eq(-mui1*exp(Ea0**2/(T1*k)) - mui1*exp(Ea1**2/(T1*k)) -
mui1*exp(Ea2**2/(T1*k)) - mui1*exp(Ea3**2/(T1*k)) +
(Eb0**2/(T1*k) - 2*Ec0)*exp(Ea0**2/(T1*k)) + (Eb1**2/(T1*k) -
2*Ec1)*exp(Ea1**2/(T1*k)) + (Eb2**2/(T1*k) - 2*Ec2)*exp(Ea2**2/(T1*k)) +
(Eb3**2/(T1*k) - 2*Ec3)*exp(Ea3**2/(T1*k)))
eq_three=sp.Eq(-mui2*exp(Ea0**2/(T2*k)) - mui2*exp(Ea1**2/(T2*k)) -
mui2*exp(Ea2**2/(T2*k)) -
mui2*exp(Ea3**2/(T2*k)) + (Eb0**2/(T2*k) - 2*Ec0)*exp(Ea0**2/(T2*k)) +
(Eb1**2/(T2*k) - 2*Ec1)*exp(Ea1**2/(T2*k)) + (Eb2**2/(T2*k) -
2*Ec2)*exp(Ea2**2/(T2*k)) +
(Eb3**2/(T2*k) - 2*Ec3)*exp(Ea3**2/(T2*k)))
eq_four=sp.Eq(-mui3*exp(Ea0**2/(T3*k)) - mui3*exp(Ea1**2/(T3*k)) -
mui3*exp(Ea2**2/(T3*k)) - mui3*exp(Ea3**2/(T3*k)) + (Eb0**2/(T3*k) -
2*Ec0)*exp(Ea0**2/(T3*k)) +
(Eb1**2/(T3*k) - 2*Ec1)*exp(Ea1**2/(T3*k)) + (Eb2**2/(T3*k) -
2*Ec2)*exp(Ea2**2/(T3*k)) +
(Eb3**2/(T3*k) - 2*Ec3)*exp(Ea3**2/(T3*k)))
ans = sp.solve((eq_one, eq_two,eq_three,eq_four), (Ea0, Ea1, Ea2, Ea3))
print('ответ :', ans)
raise NotImplementedError('could not solve %s' % eq2)
NotImplementedError: could not solve ....
这篇关于sympy 中线性方程组的求解系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!