Transpose for double integral

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!:)

 采纳的回答

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

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by