如何在MATLAB中对向量进行数值积分? [英] How do I do numerical integration of a vector in MATLAB?
问题描述
我有358个数字的向量.我想对该向量进行数值积分,但我不知道该向量的功能.
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.
我发现我们可以使用trapz或quad,但是我真的不了解如何在没有该功能的情况下进行集成.
I found that we can use trapz or quad, but i don't really understand how to integrate without the function.
推荐答案
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);
如果仅使用trapz(y)
,则会得到更大的数字,因为假定点之间的默认距离为1.可以通过乘以x点之间的距离来解决此问题:
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);
这篇关于如何在MATLAB中对向量进行数值积分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!