MATLAB Integrating in MATLAB: Find Area of X Values

AI Thread Summary
MATLAB can integrate a set of measurements to find the area under a curve, but users often seek more precise methods than the basic 'trapz' function, which may lack accuracy for certain datasets. Interpolating data can improve precision by reducing the step size, although the actual area calculated may not change significantly with linear interpolation. For datasets with equally spaced intervals, Simpson's rule is suggested as a more accurate alternative, utilizing a parabolic approximation to calculate area. Users are encouraged to explore MATLAB's resources or user-developed functions for built-in solutions to integration challenges. Overall, achieving the desired precision in area calculations requires careful consideration of the method and data characteristics.
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:
I = \frac{2h}{3}\left(y_0+4y_1+2y_2+4y_3+...+2y_{n-2}+4y_{n-1}+y_n\right)
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
Views
3K
Replies
4
Views
2K
Replies
32
Views
4K
Replies
22
Views
4K
Replies
1
Views
2K
Replies
5
Views
3K
Back
Top