How to integrate over a discrete 2D surface in MATLAB?

math, matlab

Solution

If you have a discrete dataset for which you have all the x and y values over which z is defined, then just obtain the Zdata matrix corresponding to those (x,y) pairs. Save this matrix, and then you can make it a continuous function using interp2:

function z_interp = fun(x,y)

    z_interp = interp2(Xdata,Ydata,Zdata,x,y);

end

Then you can use integral2 to find the integral:

q = integral2(@fun,xmin,xmax,ymin,ymax)

where `@fun` is your function handle that takes in two inputs.

Problem

I have a function `z = f(x, y)`, where `z` is the value at point `(x, y)`. How may I integrate `z` over the `x-y` plane in MATLAB? By function above, I actually mean I have something similar to a hash table. That is, given a `(x, y)` pair, I can look up the table to find the corresponding `z` value. The problem would be rather simple, if the points were uniformly distributed over `x-y` plane, in which case I can simply sum up all the `z` values, multiply it with the bottom area, and finally divide it by the number of points I have. However, the distribution is not uniform as shown below. So I am actually asking for the computation method that minimises the error.

Original source