5

I have a vector of 358 numbers. I'd like to make a numerical integration of this vector, but I don't know the function of this one.

I found that we can use trapz or quad, but i don't really understand how to integrate without the function.

MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
  • Have a look at my answer to a similar question, where I use trapz to integrate: http://stackoverflow.com/questions/2641809/calculate-area-under-fft-graph-in-matlab/2641824#2641824 – Jonas May 18 '10 at 22:01
  • I'll add this as a comment since it's too short to be a proper answer. Integrating without using MATLAB's built-ins would require you to have a numerical method in mind for use. The trapezoid method is one of the simplest ones; you simply find the area under the graph between adjacent points connected by a line (assuming an x-axis interval of 1, since no interval was mentioned in the question). Under such an assumption, a simple and naive scheme for vector "fx" would be (fx(2:end)+fx(1:end-1))/2. – JS Ng May 18 '10 at 23:44
  • The numerical schemes used by trapz and quad are described in the documentation to some extent, you can look them up in MATLAB's help file or online. If you'd like a fuller description of a simple algorithm, do let me know and I'll add a more complete reply. – JS Ng May 18 '10 at 23:46

3 Answers3

8

If you know the horizontal spacing of your vector, you can use trapz in order to integrate it without the function. For example, to integrate y=sin(x) from 0 to pi with 358 sections,

x=0:pi/357:pi;
y=sin(x);
area=trapz(x,y);

If you just use trapz(y), you'll get a much larger number, since the default distance between points is assumed to be 1. This problem can be fixed by multiplying by the distance between x points:

area=pi/357*trapz(y);
gnovice
  • 125,304
  • 15
  • 256
  • 359
Doresoom
  • 7,398
  • 14
  • 47
  • 61
6

You don't need to know the function in order to numerically integrate; that's the point of trapz and quad. Just pass trapz your vector. Here's a link to the documentation.

gnovice
  • 125,304
  • 15
  • 256
  • 359
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

Think about integration as to find area under the curve, which is formed by your vector. Well it's not actually a curve, but polygonal chain. What TRAPZ function is doing, it finds sum of areas of each trapezoids formed by every two neighbor points in your vector and their projection on X axis. See the function documentation, if you have uneven distance between your points or if distance not equal one.

You can read more about this method, for example, on Wikipedia.

gnovice
  • 125,304
  • 15
  • 256
  • 359
yuk
  • 19,098
  • 13
  • 68
  • 99