MATLAB Answers

Transpose for double integral

19 views (last 30 days)
Fanny Hessel
Fanny Hessel on 27 Sep 2021 at 12:52
Commented: Fanny Hessel on 28 Sep 2021 at 12:36
Hello! I'm trying to calculate a double itnegral in MATLAB, and I got help from another person on how to do. This is the code I got from them:
n=1000; m=1000;
a=0; b=1; c=-pi/2; d=pi/2;
f=@(x,y)y.*sin(y+x.*y);
x=linspace(a,b,n+1);
y=linspace(c,d,m+1);
h=(b-a)/n;
k=(d-c)/m;
qv = sum(sum(h.*k.*f(x(1:n)',y(1:m))))
this formula calculates the integral as rectangles. But my question is why we use the transpose? Using the transpose gives the correct answer, but I really don't understand why. I would, of course, ask the person who gave me the code if I could, but unfortunately I can't, and therefor I'm hoping that someone here can help me!:)

Accepted Answer

Matt J
Matt J on 27 Sep 2021 at 13:27
To perform the double integral, f(x,y) needs to be calculated at all combinations of x(i) and y(j). The transpose makes x a column vector and y is a row vector. Therefore bi-operand operations like x.*y are evaluated at all combinations, due to implicit expansion. As a simpler example;
x=(1:3).'
x = 3×1
1 2 3
y=10:10:40
y = 1×4
10 20 30 40
x.*y
ans = 3×4
10 20 30 40 20 40 60 80 30 60 90 120

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by