Transpose for double integral
2 views (last 30 days)
Show older comments
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!:)
0 Comments
Accepted Answer
Matt J
on 27 Sep 2021
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).'
y=10:10:40
x.*y
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!