How to calculate a double integral inside the domain of intersection of two functions?
Show older comments
I want to calculate a double integral of an arbitrary function (f) inside the region of intersection of two other functions. Please suggest a fast and convenient approach.
clear
JJ = 5;
II = 5;
W = rand(II, JJ);
syms x y
w = sym('0');
f = sym('0');
for i=1:II
for j=1:JJ
w =w+W(i, j)*legendreP(i-1, x)*legendreP(j-1, y);
f =f+(legendreP(i-1, x)*legendreP(j-1, y))^2;
end
end
H = 0.5*(1+tanh(w));
fsurf(w,[-1,1,-1,1],'red')
hold on
% figure
fsurf(H,[-1,1,-1,1],'blue')
%F = double integral of f inside the domain of intersection of two functions as
%the region showed in pic

13 Comments
Matt J
on 16 Jan 2023
Is the area shaded in black the region to be integrated? It is not clear what defines that region. The red and black surfaces do not seem to be touching there, so in what sense do they "intersect"?
Mehdi
on 16 Jan 2023
You mean the region somehow enclosed by the curve H-w = 0 ?
Then integrate f.*(H<w) over [-1,1] x [-1,1].
rng("default")
JJ = 5;
II = 5;
W = rand(II, JJ);
syms x y
w = sym('0');
for i=1:II
for j=1:JJ
w =w+W(i, j)*legendreP(i-1, x)*legendreP(j-1, y);
end
end
H = 0.5*(1+tanh(w));
Hminusw = fimplicit(matlabFunction(H-w),[-2 2 -2 2]);
Mehdi
on 17 Jan 2023
Mehdi
on 17 Jan 2023
x = [1 2 3 4];
y = [3 4 1 2];
x<y
% Compute area of circle with radius 1
f = @(x,y) (x.^2+y.^2<=1)*1;
value = integral2(f,-2,2,-2,2)
I don't know how trustworthy the results are, but I don't see a different way to get what you want.
But the results don't seem that bad since added, they give the integral of f over [-1,1]^2.
rng("default")
JJ = 5;
II = 5;
W = rand(II, JJ);
syms x y
w = sym('0');
f = sym('0');
for i=1:II
for j=1:JJ
w =w+W(i, j)*legendreP(i-1, x)*legendreP(j-1, y);
f =f+(legendreP(i-1, x)*legendreP(j-1, y))^2;
end
end
H = 0.5*(1+tanh(w));
f = matlabFunction(f)
g = matlabFunction(H-w)
D1 = @(x,y)f(x,y).*(g(x,y)<0);
D2 = @(x,y)f(x,y).*(g(x,y)>0);
D1int = integral2(D1,-1,1,-1,1)
D2int = integral2(D2,-1,1,-1,1)
D1int + D2int
Fint = integral2(f,-1,1,-1,1)
Torsten
on 17 Jan 2023
Because g is constant and MatlabFunction turns g into a numerical function with no input arguments.
You might use
JJ = 5;
II = 5;
W = 0*rand(II, JJ);
syms x y
w = sym('0');
f = sym('0');
for i=1:II
for j=1:JJ
w =w+W(i, j)*legendreP(i-1, x)*legendreP(j-1, y);
f =f+(legendreP(i-1, x)*legendreP(j-1, y))^2;
end
end
H = 0.5*(1+tanh(w));
f = matlabFunction(f,'Vars',[x y]);
g = matlabFunction(H-w,'Vars',[x y]);
D = @(x,y)f(x,y).*(g(x,y)<0);
Dint = integral2(D,-1,1,-1,1)
Fint = integral2(f,-1,1,-1,1)
instead.
Mehdi
on 18 Jan 2023
Answers (1)
Bjorn Gustavsson
on 17 Jan 2023
Edited: Bjorn Gustavsson
on 17 Jan 2023
0 votes
Perhaps you can use Green's theorem (you'd be very lucky if you could - but if you were to be that lucky in this case it would be a shame to miss it). That would take you from a sum of integrals over rather complicated regions to perhaps simpler integrals around the boundaries of the region. That would be nice. Given the shape of your function it doesn't seem entirely improbable.
For the case where you actually have to perform the calculations you would use the steps suggested in @Torsten's comment.
HTH
5 Comments
Mehdi
on 24 Jan 2023
Torsten
on 25 Jan 2023
M and L might work - the main problem is C.
Bjorn Gustavsson
on 25 Jan 2023
@Torsten: I thought(hoped) it would be possible to piece together C from the output of fimplicit (and the [0,1], [0,1] edges). Then it ought to be a "reasonably" straightforward number of integrals.
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

