Evaluating integrals, derivatives, etc. with Simpy...

Click For Summary
SUMMARY

This discussion focuses on evaluating integrals using the SymPy library in Python, specifically for a vector field in polar coordinates. The user attempts to compute the integral of the squared magnitude of the vector field ##\vec J## over a quarter annulus region but encounters discrepancies between SymPy and Mathematica results. The final expression for the integral, ##Q##, is derived correctly in both SymPy and Mathematica, confirming that there is no factor of 4 difference as initially feared.

PREREQUISITES
  • Familiarity with SymPy 1.10 for symbolic mathematics in Python
  • Understanding of polar coordinates and vector fields
  • Knowledge of integral calculus, specifically double integrals
  • Basic programming skills in Python
NEXT STEPS
  • Explore advanced features of SymPy for symbolic integration
  • Learn about vector calculus in polar coordinates
  • Investigate performance comparisons between SymPy and Mathematica
  • Study error analysis in symbolic computation
USEFUL FOR

Researchers, physicists, and engineers who need to perform symbolic computations involving integrals and derivatives, particularly those using Python's SymPy library.

fluidistic
Gold Member
Messages
3,932
Reaction score
283
I would like to evaluate expressions with Simpy, but unfortunately I am unable to get a simple answer, the one I would get by hand if I had the time to perform all the computations. As far as I understand, Mathematica does it and yields 4 times the Simpy result, which is a big worry since I wish to trust the result I get.

I have the expression of a vector field ##\vec J## in polar coordinates, in a region of a quarter of an annulus (2D), theta goes from 0 to pi/2 and r goes from ri to ro. I need to compute ##\int _\Omega \rho |\vec J|^2d\Omega =\int_0^{\pi/2} \int_{ri}^{ro}\vec J \cdot \vec J rdrd\theta##.

Python:
# Define the variables
r, theta, H0, ri, ro, rho, T_0, Delta_T, S_xx, S_yy = sp.symbols('r theta H0 ri ro rho T_0 Delta_T S_xx S_yy')

# Define H0
H0_expr = -(S_xx - S_yy) / (4 * rho) * Delta_T / sp.log(ro/ri)

# Define the components of the vector field J_r and J_theta
J_r = 2 * H0 * (1/r - 1/(ri**2 + ro**2)*(r + ri**2*ro**2/r**3)) * sp.cos(2*theta)
J_theta = H0 * (1/(ri**2 + ro**2)*(2*r - 2*ri**2*ro**2/r**3)) * sp.sin(2*theta)

# Calculate the magnitude squared of J (J^2)
J_squared = J_r**2 + J_theta**2

# Set up the integral for Q_total
Q_total = sp.integrate(r * rho * J_squared, (theta, 0, sp.pi/2), (r, ri, ro))

# Substitute H0 in Q_total
Q_total_sub = Q_total.subs(H0, H0_expr)
Q_total_sub = sp.simplify(Q_total_sub)
print(f"This is Q_total, the total Q: {Q_total_sub}.")
This yields:
Code:
This is Q_total, the total Q: pi*Delta_T**2*(S_xx - S_yy)**2*(-ri**4 + 2*ri**2*(ri**2 + ro**2) + ri**2*(ri**2 + 2*ro**2) + ro**4 - 2*ro**2*(ri**2 + ro**2) - ro**2*(2*ri**2 + ro**2) - 2*(ri**2 + ro**2)**2*log(ri) + 2*(ri**2 + ro**2)**2*log(ro))/(32*rho*(ri**4 + 2*ri**2*ro**2 + ro**4)*log(ro/ri)**2)
If I take it from here, by hand, I reach ##Q=\pi (\Delta T)^2(S_\text{xx}-S_\text{yy})^2\frac{\left[ri^2-ro^2+(ri^2+ro^2)\ln\left( \frac{ro}{ri} \right)\right]}{16\rho(ri^2+ro^2)\ln^2(\frac{ro}{ri})}##. How can I reach this result using Simpy? Can someone else confirm that Mathematica or other software yields 4 times this result?
 
Physics news on Phys.org
fluidistic said:
I reach ##Q=\pi (\Delta T)^2(S_\text{xx}-S_\text{yy})^2\frac{\left[ri^2-ro^2+(ri^2+ro^2)\ln\left( \frac{ro}{ri} \right)\right]}{16\rho(ri^2+ro^2)\ln^2(\frac{ro}{ri})}##. How can I reach this result using Simpy? Can someone else confirm that Mathematica or other software yields 4 times this result?
Actually, Mathematica gives precisely the same answer (no factor of 4 difference!):
1732478421959.png
 
  • Like
Likes   Reactions: fluidistic

Similar threads

Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
11
Views
1K
  • · Replies 9 ·
Replies
9
Views
1K
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K