Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Question.

  1. Aug 31, 2007 #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...
     
  2. jcsd
  3. Aug 31, 2007 #2

    J77

    User Avatar

    Try help trapz
     
  4. Aug 31, 2007 #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 :)
     
  5. Aug 31, 2007 #4

    Gokul43201

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The error is a (cubic?) function of the step size. You can make the error small by defining a smaller step size.
     
  6. Aug 31, 2007 #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.
     
  7. Sep 1, 2007 #6

    Gokul43201

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
  8. Sep 1, 2007 #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.
     
  9. Sep 2, 2007 #8

    CEL

    User Avatar

    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.
     
  10. Sep 2, 2007 #9
    Yeah, I was going to try that but I was just looking for a 'ready' made function that matlab had built in...
     
  11. Sep 2, 2007 #10

    CEL

    User Avatar

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: MATLAB Question.
  1. MATLAB questions (Replies: 0)

  2. MATLAB question (Replies: 2)

  3. MATLAB question (Replies: 1)

  4. Matlab question (Replies: 2)

  5. Matlab question (Replies: 10)

Loading...