How do I do numerical integration of a vector in MATLAB?

user330860 picture user330860 · May 18, 2010 · Viewed 60.5k times · Source

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.

Answer

Doresoom picture Doresoom · May 19, 2010

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