Is dynamic interpolation of input of function for 'quad' integration possible?
Afficher commentaires plus anciens
My task is to integrate a function given limited data, e.g.
max_energy = 1000;
quad(@(x) a.*x.*exp(-b.*x),0,max_energy)
where a, b are vectors, functions of energy. My problem is that it seems quad uses Simpson's rule iteratively until some tolerance is met, so the array size of a, b would need to change dynamically to match. Is it possible to interpolate between the data points I have to match quad? Must I do it in advance or use a different integration method?
Réponse acceptée
Plus de réponses (2)
Andrew Newell
le 30 Jan 2012
Any increase in accuracy based on interpolating a and b would probably be illusory. If your points are regularly spaced, you could use trapz:
y = a.*x.*exp(-b.*x);
yInt = trapz(x,y);
5 commentaires
Bård Skaflestad
le 30 Jan 2012
That's a good point. If the 'a' and 'b' functions are known only empirically, then |trapz| is the way to go. Don't forget to scale the result with the sample point distance if the integrand is not sampled at unit intervals.
Andrew Newell
le 30 Jan 2012
Actually, scaling is only necessary if you use yInt = trapz(y). Consider this example:
x = 1:.1:10;
y = x.^2;
yInt = cumtrapz(x,y);
plot(x,yInt,x,x.^3/3)
The documentation is confusing, though - I had to read it a couple of times before I realized that scaling is not necessary.
Daniel
le 30 Jan 2012
Bård Skaflestad
le 30 Jan 2012
@Andrew
Thanks a lot -- I'd missed that part of the documentation. I obviously need to pay more attention...
Andrew Newell
le 30 Jan 2012
I send MATLAB some feedback about the documentation.
Bård Skaflestad
le 30 Jan 2012
All of MATLAB's quadrature methods require an integrand that can be evaluated at vector inputs and return an equally sized vector result.
Do you mean to say that your a and b in some way depend on x? If so, you may have to implement a traditional function (i.e., .m) file that evaluates both a and b along with the resulting integrand.
For instance,
function y = integrand(x)
a = some_function(x);
b = some_other_function(x);
y = a .* x .* exp(-b .* x);
end
Does this help at all?
2 commentaires
Bård Skaflestad
le 30 Jan 2012
And then, of course, I forgot the |quad| call:
quad(@integrand, 0, max_energy)
Daniel
le 30 Jan 2012
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!