Why does MATLAB grader refuses to get my answer with the double integral evaluation problem?
3 次查看(过去 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.
0 个评论
回答(1 个)
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)
to get same as Omer got!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!