# MATLAB Question.

1. Aug 31, 2007

### n0_3sc

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...

2. Aug 31, 2007

### J77

Try help trapz

3. Aug 31, 2007

### n0_3sc

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 :)

4. Aug 31, 2007

### Gokul43201

Staff Emeritus
The error is a (cubic?) function of the step size. You can make the error small by defining a smaller step size.

5. Aug 31, 2007

### n0_3sc

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.

6. Sep 1, 2007

### Gokul43201

Staff Emeritus
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 prefered 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: Sep 1, 2007
7. Sep 1, 2007

### n0_3sc

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.

8. Sep 2, 2007

### CEL

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.

9. Sep 2, 2007

### n0_3sc

Yeah, I was going to try that but I was just looking for a 'ready' made function that matlab had built in...

10. Sep 2, 2007

### CEL

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.