Integration of a 2D matrix using MATLAB

Click For Summary
SUMMARY

The integration of a 2D matrix in MATLAB can be achieved using the cumtrapz() function, which computes the cumulative integral along each dimension. For a given matrix, such as the one defined over the square region [0,4]x[0,4], the command cumtrapz(I) returns a matrix of the same size, providing cumulative integral values. The total area under the data points can be calculated using sum(trapz(I)), yielding a result of 72.5000. It is important to note that the accuracy of the integration improves with a denser grid of data points.

PREREQUISITES
  • Familiarity with MATLAB syntax and functions
  • Understanding of 2D matrices and their indexing in MATLAB
  • Knowledge of numerical integration techniques, specifically trapezoidal integration
  • Basic plotting skills in MATLAB for visualizing data
NEXT STEPS
  • Explore the trapz() function for 1D and 2D integration in MATLAB
  • Learn about data interpolation techniques to improve integration accuracy
  • Investigate the meshgrid() function for creating coordinate matrices
  • Study advanced numerical methods for integration, such as Simpson's rule
USEFUL FOR

Data scientists, engineers, and researchers who need to perform numerical integration on 2D datasets using MATLAB will benefit from this discussion.

AlonsoMcLaren
Messages
89
Reaction score
2
How to do integration for a 2D matrix using MATLAB?

That is to say, suppose I have a bunch of data points over a rectangular region in xy plane, how to find the integral over this plane?

More explicitly, suppose I want to integrate over a square region, [0,4]x[0,4]

my data is

2 4 5 8 6
6 8 7 8 9
1 0 2 0 1
5 2 1 3 2
2 2 2 1 3

Therefore the value at (3,0) is 5, the value at (1,1) is 8, etc.

How to find the integral of these data over this square region?

If it were a 1D vector I am sure I can do it with trapz. But what about 2D? Thanks
 
Physics news on Phys.org
AlonsoMcLaren said:
Therefore the value at (3,0) is 5, the value at (1,1) is 8, etc.

No. The indices of a matrix in MATLAB always start with 1 and the column is specified first. So 5 is at (1,3) and 8 is at (2,2).

If you use cumtrapz() on your matrix it will return a matrix of the same size:

Code:
[x,y] = meshgrid(0:4,0:4);
I = [2 4 5 8 6; 6 8 7 8 9;1 0 2 0 1; 5 2 1 3 2; 2 2 2 1 3];
cumtrapz(I)

ans =

         0         0         0         0         0
    4.0000    6.0000    6.0000    8.0000    7.5000
    7.5000   10.0000   10.5000   12.0000   12.5000
   10.5000   11.0000   12.0000   13.5000   14.0000
   14.0000   13.0000   13.5000   15.5000   16.5000

The last row is equivalent to the output from trapz(). To visualize:

Code:
plot3(x,y,cumtrapz(I),x,y,I)
view(-84,6)

So the area under the data points is

Code:
sum(trapz(I))

ans =

   72.5000

Of course, this is a bad approximation since the points are far apart and linear behavior is assumed. More data points spaced closer together will give better results.
 
Last edited:

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K