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

1 回表示 (過去 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 件)

カテゴリ

Help Center および File ExchangeModify Image Colors についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by