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

3 Ansichten (letzte 30 Tage)
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.

Antworten (1)

Alan Stevens
Alan Stevens am 19 Apr. 2023
Bearbeitet: Alan Stevens am 19 Apr. 2023
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 Kommentar
Torsten
Torsten am 19 Apr. 2023
Bearbeitet: Torsten am 19 Apr. 2023
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by