Integrating in MATLAB: Find Area of X Values

Click For Summary

Discussion Overview

The discussion revolves around finding the area under a curve represented by discrete measurements in MATLAB. Participants explore various integration methods and their accuracy, as well as the challenges of using built-in functions for this purpose.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to integrate a set of measurements in MATLAB but finds that existing functions require a function file.
  • Another participant suggests using the 'trapz' function, which can calculate the area over a vector of values.
  • A participant expresses concern about the accuracy of 'trapz', stating that it is insufficient for their precision requirement of 1e-3.
  • There is a suggestion that reducing the step size through interpolation could improve accuracy.
  • A later reply questions the estimation of error from the 'trapz' method, arguing that without a known "real" area, the concept of error is not applicable to discrete data.
  • Another participant mentions having 500 data points and confirms that linear interpolation did not change the calculated areas using 'trapz'.
  • One participant proposes using Simpson's formula for integration, which involves fitting parabolas to sets of three points.
  • There is a request for a built-in MATLAB function for Simpson's method, with a suggestion to search the MathWorks site for user-developed functions.

Areas of Agreement / Disagreement

Participants express differing views on the accuracy of the 'trapz' method and the applicability of error estimation for discrete data. There is no consensus on the best method for integration, as multiple approaches are discussed.

Contextual Notes

Participants mention the importance of step size and the nature of the data points, indicating that the accuracy of integration methods may depend on these factors. The discussion does not resolve the uncertainties surrounding the best integration technique for the given data.

n0_3sc
Messages
238
Reaction score
1
I have a set of measurements that form a plot in MATLAB.
I just want MATLAB to integrate (ie. find the area) in a certain region of x values...is this possible?

All the integration functions I've seen require a function file or some sort of function...
 
Physics news on Phys.org
Try help trapz
 
trapz is not bad - it does the job by finding the area over a vector of values but its too inaccurate for my measurements...I require something with a precision to 1e-3.

I'm sure there are functions out there but I seem to lack the ability to find any :)
 
n0_3sc said:
trapz is not bad - it does the job by finding the area over a vector of values but its too inaccurate for my measurements...I require something with a precision to 1e-3.

I'm sure there are functions out there but I seem to lack the ability to find any :)
The error is a (cubic?) function of the step size. You can make the error small by defining a smaller step size.
 
I see.
So perhaps I could get MATLAB to interpolate my data giving a smaller step size and thus a more accurate area...

Thanks, I'll try that.
 
Wait a minute. In post#3, how did you estimate the error from the trapz method? You need to know a "real" area in order to do that. But for discrete data, there isn't any such thing as a unique real area. The area under any curve passing through all points in the data set is as real as any other (though, for obvious reasons, some may be preferred over others). So, there is actually no error if you use trapz with the step size coming from your data. A linear interpolation making the step size an integer factor of the actual width of your data steps will not change the area calculated (and will not affect the "error").

Besides, how many data points do you have in the dataset?
 
Last edited:
I understand what your saying Gokul.
I never really thought about it. But I have 500 data points and yes you were right, a linear interpolation did not make a difference to the calculated 'trapz' areas.
 
If you have equally spaced intervals in x, you can also try Simpson's formula. It takes every three points and passes a parabola through them. Then it calculates the area of the parabola.
The integral I is:
[tex]I = \frac{2h}{3}\left(y_0+4y_1+2y_2+4y_3+...+2y_{n-2}+4y_{n-1}+y_n\right)[/tex]
Where h is the size of the interval.
 
Yeah, I was going to try that but I was just looking for a 'ready' made function that MATLAB had built in...
 
  • #10
n0_3sc said:
Yeah, I was going to try that but I was just looking for a 'ready' made function that MATLAB had built in...

I suggest that you visit the site www.mathworks.com and make a search for Simpson. There is probably some user that has already developped such function.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K