ZeroBug
ZeroBug

Reputation: 21

Why sympy can't calculate very simple integral

I don't understand why sympy can't calculate very simple integral from 0 to t. How to solve this problem?

import sympy
from sympy import sin , cos , sqrt, asinh , log
t= sympy.symbols('t')
x = 't^2'
y = 't^3'

x_derivatives = sympy.diff(x , t)
y_derivatives = sympy.diff(y , t)
expression = x_derivatives**2 + y_derivatives**2

print(expression)

fi_t = sympy.integrate(sympy.sqrt(expression), (t,0,t))
print(fi_t)

Result:

9*t**4 + 4*t**2
Integral(sqrt(9*t**4 + 4*t**2), (t, 0, t))

Upvotes: 1

Views: 445

Answers (2)

Oscar Benjamin
Oscar Benjamin

Reputation: 14480

It seems that sympy's integrate function struggles with this integral but we can help it along by showing what substitution to use:

In [46]: fi_t = Integral(sqrt(9*t**4 + 4*t**2), (t, 0, t))                                                                                                                        

In [47]: fi_t                                                                                                                                                                     
Out[47]: 
t                    
⌠                    
⎮    _____________   
⎮   ╱    4      2    
⎮ ╲╱  9⋅t  + 4⋅t   dt
⌡                    
0                    

In [48]: z = Symbol('z', positive=True)                                                                                                                                           

In [49]: fi_t.transform(t, sqrt(z))                                                                                                                                               
Out[49]: 
 2                   
t                    
⌠                    
⎮     ____________   
⎮    ╱    2          
⎮  ╲╱  9⋅z  + 4⋅z    
⎮  ─────────────── dz
⎮        2⋅√z        
⌡                    
0                    

In [50]: factor_terms(fi_t.transform(t, sqrt(z)))                                                                                                                                 
Out[50]: 
 2               
t                
⌠                
⎮    _________   
⎮  ╲╱ 9⋅z + 4  dz
⌡                
0                
─────────────────
        2        

In [51]: factor_terms(fi_t.transform(t, sqrt(z))).doit()                                                                                                                          
Out[51]: 
          3/2     
⎛   2    ⎞        
⎝9⋅t  + 4⎠      8 
───────────── - ──
      27        27

Upvotes: 3

Kate Melnykova
Kate Melnykova

Reputation: 1873

I re-run your code in the in-build sympy shell. Changing t in the upper bound of the integral to 1 fixes the code.

import sympy
from sympy import sin , cos , sqrt, asinh , log
t= sympy.symbols('t')
x = 't^2'
y = 't^3'

x_derivatives = sympy.diff(x , t)
y_derivatives = sympy.diff(y , t)
expression = x_derivatives**2 + y_derivatives**2

print(expression)

def fi_t(x):
    return sympy.integrate(sympy.sqrt(expression), (t,0,x))
print(fi_t(4))
print(fi_t(-4))

Upvotes: 1

Related Questions