Integrating in MATLAB: Find Area of X Values

In summary, Gokul suggests that he try to find a function that MATLAB has built in, or consult a website that specializes in MATLAB functions. He also points out that if he has equally spaced intervals in x, he can use Simpson's formula.
  • #1
n0_3sc
243
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
  • #2
Try help trapz
 
  • #3
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
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.
 
  • #5
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
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:
  • #7
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
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.
 
  • #9
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.
 

1. How do I find the area of X values in MATLAB?

To find the area of X values in MATLAB, you can use the trapz function. This function takes in two arguments: the X values and the corresponding Y values. It then calculates the area under the curve using the trapezoidal rule.

2. Can I integrate a function with irregularly spaced X values?

Yes, you can integrate a function with irregularly spaced X values in MATLAB. The trapz function can handle both regular and irregularly spaced data. You just need to make sure that the X and Y values are in the correct order.

3. How can I plot the area under the curve in MATLAB?

You can plot the area under the curve in MATLAB by using the plot function. First, use the trapz function to calculate the area under the curve. Then, plot the X and Y values using the plot function. You can also add a shaded area using the fill function to make the plot more visually appealing.

4. Does MATLAB have any other functions for integrating?

Yes, besides the trapz function, MATLAB also has the quad and quadl functions for numerical integration. These functions use different integration methods and may be more accurate for certain types of functions. Additionally, the integral function can be used for symbolic integration.

5. How can I calculate the error in my integrated value?

You can calculate the error in your integrated value by using the trapzerr function. This function takes in the same arguments as the trapz function but also returns an estimate of the error in the integrated value. You can also use the quadgk function, which returns an accurate estimate of the error in the integrated value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
Back
Top