# integral2 failing to calculate area of a square inside a circle?

6 views (last 30 days)
Jesus Sanchez on 10 Jun 2021
Commented: Jesus Sanchez on 15 Jun 2021
Hello all,
I am integrating the area contained within a square and a circle using integral2. I use a circle centered at (0,0) with radius 2.75 and two squares with edge 0.5 (blue on image) and 1 (red on the image), whose bottom left corner lies on (0.5, 0.5). As the circle is larger than the squares, I would expect the result of the integral to be the areas of each square, 0.25 for the blue and 1 for the red. However, the output for the red one is 0.75. Am I missinterpretating the result?
The image:
The code:
Zfun1 = @(x,y) double((x.^2 + y.^2) <= 2.75); % Circle function
integral2(Zfun1,0.5,1.5,0.5,1.5,'Method','iterated',...
'AbsTol',1e-5,'RelTol',1e-5) % Red square
integral2(Zfun1,0.5,1,0.5,1,'Method','iterated',...
'AbsTol',1e-5,'RelTol',1e-5) % Blue square
rectangle1 = [0.5 , 0.5; % Coordinates red square (for plotting)
0.5 , 1.5;
1.5 , 1.5;
1.5 , 0.5;
0.5 , 0.5];
rectangle2 = [0.5 , 0.5; % Coordinates blue square (for plotting)
0.5 , 1;
1 , 1;
1 , 0.5;
0.5 , 0.5];
figure
circle(0,0,2.75); % circle
hold on; grid on
plot(rectangle1(:,1),rectangle1(:,2),'-.r'); % red square
plot(rectangle2(:,1),rectangle2(:,2),'-.b'); % blue square
axis equal
xlim([-0.1 2.8])
ylim([-0.1 2.8])
function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
end

Tarunbir Gambhir on 15 Jun 2021
Edited: Tarunbir Gambhir on 15 Jun 2021
Please correct the equation of the circular region in function 'Zfun1'. It should be of the form, x^2+y^2<=r^2.
Jesus Sanchez on 15 Jun 2021
Yes, I see I had that mistake. Now everything is working as it should. Thank you!

### Categories

Find more on 2-D and 3-D Plots 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!

Translated by