MATLAB Answers

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

조회 수: 3(최근 30일)
Jesus Sanchez
Jesus Sanchez 2021년 6월 10일
댓글: Jesus Sanchez 2021년 6월 15일
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

채택된 답변

Tarunbir Gambhir
Tarunbir Gambhir 2021년 6월 15일
편집: Tarunbir Gambhir 2021년 6월 15일
Please correct the equation of the circular region in function 'Zfun1'. It should be of the form, x^2+y^2<=r^2.
  댓글 수: 1
Jesus Sanchez
Jesus Sanchez 2021년 6월 15일
Yes, I see I had that mistake. Now everything is working as it should. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by