|
"Tomer Alon" <tomeralon112@gmail.com> wrote in message <is0cmq$fbf$1@newscl01ah.mathworks.com>...
> hi, i have a question about 2-d integral.
> i have a matrix for x,y,data and i can't express my data as a function.
> how can i do an integral on the data with respect to the x,y points?
> thanks for the helpers
- - - - - - - - -
If the data you have is given for a rectangular region in the x-y plane in a mesh of discrete points, you can use nested 'trapz' function calls to approximate the double integral of that data over the region. That is, suppose x and y are column vectors, and you have the data in an array z such that z(i,j) = data(x(i),y(j)) for each i and j. Then do:
I = trapz(y,trapz(x,z));
This would be the trapezoidal approximation of the double integral of data with respect to x and y.
Here's a concrete example of that for the function data(x,y) = (x+3*y)^3 integrated over the rectangle 1<=x<=3, 0<=y<=4:
n = 32; m = 50;
x = linspace(1,3,n);
y = linspace(0,4,m);
z = zeros(n,m); %
for i = 1:n % (meshgrid or ndgrid would create z more easily)
for j = 1:m
z(i,j) = (x(i)+3*y(j))^3;
end
end
f = trapz(y,trapz(x,z))
ans =
6.466052394922165e+03
The perfect answer is 6464 so the approximation is off by about 2.
If you need greater accuracy and your data is comparatively free of noise with a reasonably smooth function, I believe there are integration methods implemented in the File Exchange that use higher order approximations for such integration using discrete points.
Note: In your description you said, "integral on the data with respect to the x,y points". It is better terminology to say 'with respect to the x and y variables'. Integration "with respect to points" conjures up (as John hinted) measure theory concepts where a non-zero measure can sometimes be assigned to individual points, which is not the kind of numerical integration matlab deals with.
Roger Stafford
|