Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
2-d numerical integration

Subject: 2-d numerical integration

From: Tomer Alon

Date: 30 May, 2011 15:22:02

Message: 1 of 3

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

Subject: 2-d numerical integration

From: John D'Errico

Date: 30 May, 2011 16:10:20

Message: 2 of 3

"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

You cannot integrate points. Points, shmoints. Well,
you can integrate over them, but it is a set of measure
zero, so a rather boring result.

You can integrate a function. So you need to define
a domain. You need to find a function that approximates
or interpolates those points. Then integrate that function
over the indicated domain. This may involve a triangulation
of the domain from these points. If the domain is not
convex, then the triangulation will be more difficult.

HTH,
John

Subject: 2-d numerical integration

From: Roger Stafford

Date: 30 May, 2011 21:07:02

Message: 3 of 3

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us