0
votes

In python, I was trying to resolve this symbolic integral

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

In Matlab, I was trying to resolve the same expression

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

The fact is:

In Matlab, I got a result that (before it is evaluated in "eval") it contains the form "cos(t)" inside and when it is evaluated in "eval" gave me an array with only real numbers, also this is the correct answer in the overall exercise.

But, when I make the same task in python I get an expression that doesn't contain the symbolic result with "cos(t)" instead it has many "exp(I*t)".So, when I evaluate the result in order to get the array, I have an array with only complex numbers.

Because of this, one could think "Matlab is better than python solving symbolics integrals".

My question is ¿Is there some way that I could reach the same result in python? I refuse to believe that it isn't possible to obtain the same result with some of the algebraic manipulations, or maybe, solving the integral in another way.

Of course, I have already done the usual, simplify, and expand. If you want to run the python code, it is here.

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 ) 

Thanks.

1
remember: e^(ix) = cos(x) + i sin(x) - Natsfan
numpy and sympy are not integrated. It would be a good idea to distinguish by name variables that numeric numpy, object dtype numpy with symbolic elements (anything involving symbol t), and pure sympy expressions. Do not casually mix numpy and sympy - only do so with clear intent. - hpaulj
R has complex values which carry through to the end. - hpaulj
Yes but the same occurs in Matlab, so the problem is not in the matrix, but the integration process - VEBP

1 Answers

0
votes

The solution was to add expand to inte

expand(inte)