Stephen Jacob
Stephen Jacob

Reputation: 901

Sympy integration giving NaN as output

I am fairly new to sympy and hoping if someone could guide me on the right approach. As mentioned the following when integrated gives a NaN output. Is this a mathematics error, sympy limitation or user error?

enter image description here

Upvotes: 1

Views: 610

Answers (1)

user6655984
user6655984

Reputation:

Since NaN isn't a correct answer for that integral, it is a SymPy error. But it's caused by a suboptimal setup. SymPy finds it difficult to handle expressions with floating-point numbers, because floating-point arithmetics is different from mathematical rules of arithmetics (addition is not associative, for example). This is particularly true for expressions with such constants in an exponent.

For this reason, it's best to keep the floating point constants out of the computations, including them at the end. Instead of

integrate((0.2944/z**0.22+1.0)*(1.939*log(z/10) +17.7), z)

write

a, b, c, d, e = symbols('a b c d e', positive=True)
values = {a: 0.2944, b: 0.22, c: 1.0, d: 1.939, e: 17.7}
expr = integrate((a/z**b + c) * (d*log(z/10) + e), z)
print(expr.subs(values).simplify())

which prints

0.731848205128205*z**0.78*log(z) + 4.05720568750119*z**0.78 + 1.939*z**1.0*log(z) + 11.2962875046845*z**1.0

Upvotes: 1

Related Questions