Quadrature method using vectors
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposte (3)
Andrew Newell
il 5 Gen 2012
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)
0 Commenti
blindcurrent
il 5 Gen 2012
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 Commenti
Andrew Newell
il 5 Gen 2012
The method you described will be inaccurate because the integrand is biased to the bottom of each interval.
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!