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

3 visualizaciones (últimos 30 días)
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?
Thanks in advance!
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

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by