Did I implement this peculiar integral in the right way?

184 Views Asked by At

I have such a integral at hand, I need to integrate it numerically in matlab. or if somebody can give a simple answer implemented in any language, it's also appreciated.

$$\int_0^a \int_0^a \phi(x_1) e^{-q|x_1-x_2|}\phi(x_2) dx_1dx_2$$

mesh.nn = 101;                   % number of nodes 
mesh.x  = linspace(0,a,mesh.nn); % node coordinates, m

[xx,yy] = meshgrid(mesh.x,mesh.x);

MM= exp(-qnorm*abs(yy-xx));
mat= (phi_1.' * phi_2).* MM;

V = trapz(mesh.x,trapz(mesh.x,mat,1),2);
1

There are 1 best solutions below

2
On

Is there a reason you're not using integral2 in Matlab to perform quadrature integration? If you're using an older version of Matlab, try quad2d or dblquad. If the problem is that your function $\phi$ is numerical data rather than an analytical function, then you can still use interpolation. For example, if $\phi($ PHI_XCOORDS $) = $ PHI_DATA then, using interp1:

phi = @(x)interp1(PHI_XCOORDS,PHI_DATA,x);
f = @(x1,x2)phi(x1).*exp(-Q.*abs(x1-x2)).*phi(x2);
V = integral2(f,0,A,0,A)

Since your code/equations are incomplete I can't really check if this will work or help you further though.