Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Integrating for the volume of a torus in SciPy Nitram Programmer named Tim Posts: 9 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #1 Jan-06-2020, 12:43 PM Hi, I have been trying to perform a triple integral in cylindrical coordinates using tplquad in SciPy. The particular integral is for a torus with radius R = 4 and cross-sectional radius r = 1. This is my code: ```import numpy as np from scipy.integrate import tplquad R = 4 r = 1 # Volume integral # Order of integration is z, rho, theta f1 = lambda z, rho, theta: 8 * 2 * rho # set theta limits a, b = 0, np.pi/2 # set rho limits rho_low = lambda rho: R - r rho_high = lambda rho: R + r # set z limits z_low = lambda rho, theta: 0 z_high = lambda rho, theta: np.sqrt(r**2 - (rho - R)**2) # calculate integral V, _ = tplquad(f1, a, b, rho_low, rho_high, z_low, z_high) print(V) ```I am receiving the following error: "RuntimeWarning: invalid value encountered in sqrt z_high = lambda rho, theta: np.sqrt(r**2 - (rho - R)**2)" I've seen in previous integrations that when an integration includes a square root of one of the variables then the integration has to be undertaken in the first octant only and then multiplied by 8. This has been my approach here which is why the upper theta limit is pi/2. I am aware that I could do this integration as a volume of revolution with only one variable to integrate over but I would like to understand why this method won't work and how it can be resolved. Grateful for any pointers, thanks! Gribouillis  Posts: 2,137 Threads: 20 Joined: Jan 2018 Reputation: 201 Likes received: 517 #2 Jan-07-2020, 03:43 PM (This post was last modified: Jan-07-2020, 03:43 PM by Gribouillis. Edited 1 time in total.) My guess is that for `z_low` and `z_high` it should be `lambda theta, rho:...` because the documentation says `qfun(x, y)` and `rfun(x, y)` Nitram likes this post Nitram Programmer named Tim Posts: 9 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #3 Jan-08-2020, 04:45 PM (This post was last modified: Jan-08-2020, 04:47 PM by Nitram. Edited 2 times in total.) (Jan-07-2020, 03:43 PM)Gribouillis Wrote: My guess is that for `z_low` and `z_high` it should be `lambda theta, rho:...` because the documentation says `qfun(x, y)` and `rfun(x, y)` It turns out you are correct! Thanks! I ran the code ```import numpy as np from scipy.integrate import tplquad R = 4 r = 1 # Volume integral # Order of integration is z, rho, theta f1 = lambda z, rho, theta: 2 * rho # set theta limits a, b = 0, 2 * np.pi # set rho limits rho_low = lambda rho: R - r rho_high = lambda rho: R + r # set z limits z_low = lambda theta, rho: 0 z_high = lambda theta, rho: np.sqrt(r**2 - (rho - R)**2) # calculate integral V, _ = tplquad(f1, a, b, rho_low, rho_high, z_low, z_high) print(V) ```which gives the correct result. I did not have to integrate in the first octant only and then multiply by 8 after all . How come the order 'lambda rho, theta' did not work and 'lambda theta, rho' does? « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post error on stock indicator code on balance volume yatish 19 2,713 Dec-14-2018, 05:40 PM Last Post: yatish

Forum Jump:

Users browsing this thread: 1 Guest(s)