0

Hi I'm having a hard time solving this equation in python (34), I also added 35 and 36 as information for Ci.Eq 34

Eq 35, 36

Here is what I have so far:

import numpy as np
import scipy as sp
import sympy as smp
smp.init_printing(True)
C_i = smp.Function('C_i')
t, x, v, tao = smp.symbols('t, x, v, tao', positive=True) # tao = lambda x, v: x/v
m2 = smp.Integral((C_i(t-tao))**2,(tao, 0,t))
m2.doit()
m2_s = m2.doit().simplify()
t_arr = np.arange(0,1000,1)
fm2_1 = smp.lambdify((x,v,t), m2_s, ['scipy', {'C_i': lambda e:0.666666}])

I'm not sure how to add the condition that tao = x/v. I do not need to use sympy its just what I could find online as a guide.

Tulio Soto
  • 11
  • 1
  • Possible duplicate of: https://stackoverflow.com/questions/72900048/solving-an-integral-with-dirac-delta-using-sympy – Davide_sd Jul 11 '22 at 17:38
  • Possible duplicate of: https://stackoverflow.com/questions/72912501/problems-with-quad-when-using-lambdify – Davide_sd Jul 11 '22 at 17:39

2 Answers2

0

You can use the Integral.transform method to do a substitution in an integral:

In [10]: m2
Out[10]: 
t              
⌠              
⎮   2          
⎮ Cᵢ (t - τ) dτ
⌡              
0              

In [11]: m2.transform(tau, (x/v, x))
Out[11]: 
t⋅v              
 ⌠               
 ⎮    2⎛    x⎞   
 ⎮  Cᵢ ⎜t - ─⎟   
 ⎮     ⎝    v⎠   
 ⎮  ────────── dx
 ⎮      v        
 ⌡               
 0 

Otherwise I'm not really sure what your equation 34 is supposed to mean.

Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
0

Your sympy expression is an unevaluated integral:

In [10]: m2_s
Out[10]: 

enter image description here

The resulting lambdified function uses scipy.integrate.quad:

In [13]: help(fm2_1)
Help on function _lambdifygenerated:

_lambdifygenerated(x, v, t)
    Created with lambdify. Signature:
    
    func(x, v, t)
    
    Expression:
    
    Integral(C_i(t - tao)**2, (tao, 0, t))
    
    Source code:
    
    def _lambdifygenerated(x, v, t):
        return quad(lambda tao: C_i(t - tao)**2, 0, t)[0]
    
    
    Imported modules:
    
    from scipy.integrate import quad

While you specified x,v as function arguments, they aren't used in the expression at all!

If you want more control over the scipy integration, you should lambdify the expression that you want to integrate:

In [14]: fn = smp.lambdify((C_i,t,tao),(C_i(t-tao))**2)    
In [15]: help(fn)
Help on function _lambdifygenerated:

_lambdifygenerated(C_i, t, tao)
    Created with lambdify. Signature:
    
    func(C_i, t, tao)
    
    Expression:
    
    C_i(t - tao)**2
    
    Source code:
    
    def _lambdifygenerated(C_i, t, tao):
        return C_i(t - tao)**2
hpaulj
  • 221,503
  • 14
  • 230
  • 353