0

I am trying to solve integral using Python, I made the following code

from sympy import integrate
import sympy as sy
from scipy.integrate import quad
import math
k_2=Symbol('k_2')
k_1=Symbol('k_1')
t_d=Symbol('t_d')
C_0=Symbol('C_0')
y=Symbol('y')
f=lambda y:((k_2+C_0*sy.exp(-y/t_d))/(k_2+C_0))**(k_1*t_d)
print(quad(f,0,y))

But I got following error:

cannot determine truth value of Relational

It would be of great help who can help me with this error, thank you very much

  • Where's the traceback? – hpaulj Nov 03 '21 at 01:53
  • `quad` js a numerical integrator! Your function is `sympy`, symbolic. – hpaulj Nov 03 '21 at 01:55
  • See my answer to a question with the same error message, https://stackoverflow.com/questions/65512106/sympy-typeerror-cannot-determine-truth-value-of-relational – hpaulj Nov 03 '21 at 02:02
  • You can't use numerical integration if the integrand or limits involve symbols other than the integration variable. It doesn't look like sympy can compute this integral symbolically but you can use either sympy or scipy to compute this numerically if you substitute values for all symbols except the integration variable. – Oscar Benjamin Nov 03 '21 at 10:02

1 Answers1

0

When I try to run your code:

In [51]: integrate.quad(f,0,y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-3605c48e48f5> in <module>
----> 1 integrate.quad(f,0,y)

/usr/local/lib/python3.8/dist-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    346 
    347     # check the limits of integration: \int_a^b, expect a < b
--> 348     flip, a, b = b < a, min(a, b), max(a, b)
    349 
    350     if weight is None:

/usr/local/lib/python3.8/dist-packages/sympy/core/relational.py in __bool__(self)
    396 
    397     def __bool__(self):
--> 398         raise TypeError("cannot determine truth value of Relational")
    399 
    400     def _eval_as_set(self):

TypeError: cannot determine truth value of Relational

The numeric integrator is trying to verify that the integration bounds are in the right numeric order. But one bound is a symbol:

In [52]: 0<y
Out[52]: y > 0

In [53]: bool(0<y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-b72cf53c1f50> in <module>
----> 1 bool(0<y)

/usr/local/lib/python3.8/dist-packages/sympy/core/relational.py in __bool__(self)
    396 
    397     def __bool__(self):
--> 398         raise TypeError("cannot determine truth value of Relational")
    399 
    400     def _eval_as_set(self):

TypeError: cannot determine truth value of Relational

Note that f evaluated at y is symbolic. quad is a numeric integrator!

In [54]: f(y)
Out[54]: 
              k₁⋅t_d
⎛    -y      ⎞      
⎜    ───     ⎟      
⎜    t_d     ⎟      
⎜C₀⋅ℯ    + k₂⎟      
⎜────────────⎟      
⎝  C₀ + k₂   ⎠      

but even at a numeric value:

In [55]: f(1)
Out[55]: 
              k₁⋅t_d
⎛    -1      ⎞      
⎜    ───     ⎟      
⎜    t_d     ⎟      
⎜C₀⋅ℯ    + k₂⎟      
⎜────────────⎟      
⎝  C₀ + k₂   ⎠ 
hpaulj
  • 221,503
  • 14
  • 230
  • 353