Quadrature method using vectors

I was wondering if anyone knew of a method in MATLAB to numerically integrate a function using a quadrature method with a vector rather than a function as the input (step size/location defined by a second vector). The system of integrals I am attempting to integrate are coupled such that an explicit function cannot be passed to a quad(fun,a,b) type MATLAB function. I'd be happy to post the systems of integrals if you're interested.
Writing the code for a vector based quadrature method shouldn't be too bad but it would be nice to save the time if I didn't have to do it.
Any ideas?

回答 (3 件)

Andrew Newell
Andrew Newell 2012 年 1 月 5 日

1 投票

One approach is the trapezoidal rule:
vecsum = dot(diff(t),x(1:end-1)+x(2:end))/2;
or, in loop form,
vecsum = 0;
for i=1:length(x)
vecsum = vecsum + (t(i+1)-t(i))*(x(i+1)+x(i))/2;
end
(edited to correct errors)
Jan
Jan 2012 年 1 月 5 日

1 投票

See: trapz

1 件のコメント

blindcurrent
blindcurrent 2012 年 1 月 6 日
Yes, this one does the trick! Thanks!

サインインしてコメントする。

blindcurrent
blindcurrent 2012 年 1 月 5 日

0 投票

As I understand it, you have your function in the form of a vector containing the data points, and another vector (of the same length) with the corresponding time values.
Let vector x be the data and t the time values. Then you could use for example the rectangle method:
sum=0;
for i=1:1:size(x,1)-1
sum=sum+x(i)*(t(i+1)-t(i));
end;
In the end, sum would be your integral value. Note that the time points need not be equispaced.
I thought Matlab would have something that does this, but could not find anything. Therefore I tried this piece of code. It is rather crude and there are much more sophisticated numerical quadrature methods available. Hope that someone has got a better idea!

2 件のコメント

Andrew Newell
Andrew Newell 2012 年 1 月 5 日
The method you described will be inaccurate because the integrand is biased to the bottom of each interval.
Jan
Jan 2012 年 1 月 5 日
Shadowing the builtin function "sum" by a variable is a bad idea.

サインインしてコメントする。

カテゴリ

製品

質問済み:

2011 年 4 月 28 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by