Python与MATLAB在符号集成方面的比较



在python中,我试图解决这个符号积分

t = symbols('t')
inte = sympy.Matrix( " long and complex expresion with many exp(t*numbers) in a matrix " )
Gam = sympy.integrate( inte , (t , 0 , 0.1 ) )
Gam = Gam.evalf()
Gam = np.array( Gam ) # to turn the expression into a numpy array

在Matlab中,我试图解析相同的表达式

syms t
inter = eval( int( inte , t , 0 , 0.1 ) ) % to turn the expression into a  numerical array

事实是:

在Matlab中,我得到了一个结果(在"eval"中对其进行评估之前(,它包含形式";cos(t(〃;内部和当在"内部"中评估时;eval";给了我一个只有实数的数组,这也是整个练习中的正确答案。

但是,当我在python中执行相同的任务时,我得到的表达式不包含带有"的符号结果;cos(t(〃;相反,它有许多";exp(I*t(";。所以,当我评估结果以获得数组时,我得到了一个只有复数的数组。

正因为如此,人们可以认为";Matlab比python更好地解决符号积分;。

我的问题是?有没有办法让我在python中达到同样的结果?我不相信用一些代数运算,或者用另一种方法求解积分,不可能得到同样的结果。

当然,我已经做了常规的、简化的和扩展的操作。如果你想运行python代码,它就在这里。

t  =  symbols( 't ' )
init_printing(use_unicode=True)
tini = 0
tfin = 10
T    = 1e-1
time = np.array( np.arange( tini , tfin , T ) , ndmin=2 )
A = np.array([[-8, 1, 0, ],
[-5, 0, 1, ],
[-6, 0, 0, ]])
B = np.array([[0],
[1],
[0]])
V , R = np.linalg.eig(A) # Eigvalues in a vector, Matrix Diagonalizante
M = np.round( np.linalg.inv(R) @ A @ R , 4) # Diagonal Matrix
ExpA_t = np.diag( e**(V*t) )
R      = smp.Matrix(R)
B      = smp.Matrix(B)
ExpA_t = smp.Matrix(ExpA_t)
inte = ( R @ ExpA_t @ R**-1 ) @ B
Gam = integrate( inte , (t , tini , T) ) # here is the issue
Gam = np.array( Gam , dtype = complex ) 

谢谢。

解决方案是在inte 中添加扩展

扩展

最新更新