I am trying to solve an integral, such that
$$F_z = G\sigma z\int\int_{-L/2}^{L/2} \frac{dxdy} {(x^2 + y^2 + z^2)^{3/2}}$$
I tried to appraoch the problem in this way,
$$F(x,y) = \int_{-L/2}^{L/2} \frac{dx} {(x^2 + y^2 + z^2)^{3/2}}$$
and then
$$F_z = G\sigma z \int_{-L/2}^{L/2} F(x,y) dy$$
But in the coding part I learned gaussian quadrature for only x component, however in the function there are 2 components, so when I try to apply the gaussian quadrature I dont know what the y value should be. When I take the integral they should be treated as constants I guess ?
from numpy import linspace, cos, pi, tan, ones, copy
sigma = 100 #kg/m^2 sheet density
G = 6.674 * 10**-11 #m^3kg^-1s^-2
L = 10 # m
N = 100 # step size
def gaussxw(N):
# Initial approximation to roots of the Legendre polynomial
a = linspace(3,4*N-1,N)/(4*N+2)
x = cos(pi*a+1/(8*N*N*tan(a)))
# Find roots using Newton's method
epsilon = 1e-15
delta = 1.0
while delta>epsilon:
p0 = ones(N,float)
p1 = copy(x)
for k in range(1,N):
p0,p1 = p1,((2*k+1)*x*p1-k*p0)/(k+1)
dp = (N+1)*(p0-x*p1)/(1-x*x)
dx = p1/dp
x -= dx
delta = max(abs(dx))
# Calculate the weights
w = 2*(N+1)*(N+1)/(N*N*(1-x*x)*dp*dp)
return x,w
def f(x):
return (x**2 + y**2 + z**2)**(-3/2)
def gaussian(a, b):
x, w = gaussxw(N)
xp = 0.5 * (b - a) * x + 0.5 * (b + a)
wp = 0.5 * (b - a) * w
s = 0
for k in range(N):
s += wp[k] * f(xp[k])
return s
z = 1
Fz = G * sigma * z * gaussian(-L/2, L/2)
The problem is in the above code what should be the y value ? (which thats basically what I am asking) or should I change the gaussxw to gaussyw and try that ? (but I am also not sure how to do that)
$\displaystyle\int_{-L/2}^{L/2}\frac{dx}{(x^2+y^2+z^2)^{3/2}}$ is not a function of $x$. If we call it $F(y)$, then when you compute $\displaystyle\int_{-L/2}^{L/2}F(y)\,dy$ using the Gaussian quadrature, as a subproblem you have to compute $F(y)$ at given $y$. As $F(y)$ is represented by an integral, you can compute it using the quadrature again. That is, if the quadrature you use for a single integral is $\int_{-L/2}^{L/2}f(x)\,dx\approx\sum_{i=1}^{n}w_i f(x_i)$, then for a double integral it is used like this: $$\int_{-L/2}^{L/2}\int_{-L/2}^{L/2}f(x,y)\,dx\,dy\approx\sum_{i=1}^{n}\sum_{j=1}^{n}w_i w_j f(x_i,x_j).$$
A side note: the integral has a closed form. Denoting $a=L/2$ and using polar coordinates, we have \begin{align} z\iint\limits_{[-a,a]^2}\frac{dx\,dy}{(x^2+y^2+z^2)^{3/2}}&=8z\int\limits_{0}^{\pi/4}\int\limits_{0}^{a/\!\cos\phi}\frac{r\,dr\,d\phi}{(r^2+z^2)^{3/2}}\\&=8z\int_0^{\pi/4}\left(\frac{1}{z}-\frac{\cos\phi}{\sqrt{a^2+z^2\cos^2\phi}}\right)d\phi\\\color{gray}{[w=z\sin\phi]}&=2\pi-8\int_0^{z/\sqrt{2}}\frac{dw}{\sqrt{a^2+z^2-w^2}}\\&=2\pi-8\arcsin\frac{z}{\sqrt{2(a^2+z^2)}}. \end{align}