Integration of a 2D matrix using MATLAB

In summary, the conversation was about how to perform integration for a 2D matrix in MATLAB, specifically over a square region. Using the function cumtrapz(), the integral can be calculated and visualized in a matrix format. However, the accuracy of the result depends on the spacing and number of data points.
  • #1
AlonsoMcLaren
90
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
  • #2
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:

1. What is the purpose of integrating a 2D matrix using MATLAB?

The purpose of integrating a 2D matrix using MATLAB is to calculate the area under the curve represented by the matrix. This is useful in a variety of scientific and engineering fields, such as signal processing, image processing, and data analysis.

2. How does MATLAB perform integration on a 2D matrix?

MATLAB uses numerical integration methods, such as the trapezoidal rule or Simpson's rule, to calculate the area under the curve represented by the matrix. These methods divide the matrix into smaller segments and approximate the area under each segment using a mathematical formula.

3. Can I integrate a 2D matrix with irregularly spaced data points?

Yes, MATLAB has functions specifically designed for integrating a 2D matrix with irregularly spaced data points. These functions, such as "trapz2" and "quad2d," take into account the spacing between data points to provide a more accurate integration result.

4. Is it possible to integrate a 2D matrix using symbolic variables in MATLAB?

Yes, MATLAB has a symbolic math toolbox that allows you to perform mathematical operations, including integration, on matrices containing symbolic variables. This is useful for solving complex mathematical problems symbolically.

5. Can I visualize the integration result of a 2D matrix in MATLAB?

Yes, MATLAB has built-in functions for visualizing the integration result of a 2D matrix, such as "surf" and "mesh." These functions create a 3D plot of the matrix, with the height of the surface representing the integrated value at each point.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
984
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top