Why does MATLAB grader refuses to get my answer with the double integral evaluation problem?

조회 수: 1 (최근 30일)
Hello,
I am new to MATLAB, and my teacher gave us a few questions.
I am using the MATLAB grader to check whether or not my answer is correct.
I got the following question:
"Use the meshgrid command to evaluate the double integral that has limits of 0 to 2*pi each, with the integrand of ((sin(x)^2) * (cos(y)^2)dxdy): by approximating the volumes of 0.01x0.01 squared boxes that lie below the graph of the function".
I tried and wrote the following code:
x = linspace(0, 2*pi, 1000);
y = linspace(0, 2*pi, 1000);
[X, Y] = meshgrid(x, y);
Z = (sin(X).^2).*(cos(Y).^2);
L = sum(sum(Z))*(2*pi/1000)^2;
I get the result: 9.8696 which is 2 times the result I should have got. Even after dividing by 2 I get the following error in MATLAB grader:
"The value of L is incorrect".
I don't seem to understand why the grader doesn't work.
I hope that I have made myself clear.
If you know how to resolve this, your help would be much appreciated.

답변 (1개)

Alan Stevens
Alan Stevens 2023년 4월 19일
편집: Alan Stevens 2023년 4월 19일
As follows
Oops! I did the sum incorrectly! Should have been like this:
% box side length
delta = 0.01;
% mid box points
x = delta/2:delta:(2*pi-delta/2);
y = delta/2:delta:(2*pi-delta/2);
% values of Z summed for each column
Z = 0;
for i = 1:numel(x)
Z = sum((sin(x(i))^2)*(cos(y).^2))+Z;
end
L = Z*delta^2;
disp(L)
9.8596
to get same as Omer got!
  댓글 수: 1
Torsten
Torsten 2023년 4월 19일
편집: Torsten 2023년 4월 19일
integral(@(x)sin(x).^2,0,2*pi)*integral(@(x)cos(x).^2,0,2*pi)
ans = 9.8696
So the result the OP got looks fine to me.

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

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by