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

耗尽温柔 提交于 2019-11-29 04:44:25
Doresoom

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

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.

yuk

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!