Integration

29 visualizaciones (últimos 30 días)
Bill Luckow
Bill Luckow el 11 de En. de 2012
Does matlab have a function of Integration? For example, I have a Gaussian density function, if I use the integration function on the density function over the time of (-infinity, +infinity), then we should obtain one. Thank you very much

Respuestas (2)

Walter Roberson
Walter Roberson el 11 de En. de 2012
MATLAB has several numeric integration routines, such as quad(), quadv(), quadgk(), triplequad()
The Symbolic Toolbox offers symbolic integration.

Mike Hosea
Mike Hosea el 11 de En. de 2012
QUADGK can handle infinite limits.
>> quadgk(@(x)exp(-x.^2./2)./sqrt(2*pi),-inf,inf)
ans =
1.000000000000038
The default tolerances used there were, BTW, AbsTol = 1e-10 and RelTol = 1e-6. Since the numerical answer is about 1, and RelTol > AbsTol, only the RelTol was important. We can crank down both tolerances
>> quadgk(@(x)exp(-x.^2./2)./sqrt(2*pi),-inf,inf,'AbsTol',5e-14,'RelTol',5e-14)
ans =
1
Getting 1 exactly in that case was just luck. Evaluating CDFs is one example where pure absolute error control might make sense to you:
>> quadgk(@(x)exp(-x.^2./2)./sqrt(2*pi),-inf,inf,'AbsTol',1e-10,'RelTol',0)
ans =
1.000000000000000
If you have a very small variance, the integrator might not see the non-zero part of the curve.
>> sigma = 1e-10; f = @(x)exp(-x.^2./(2*sigma))./sqrt(2*pi*sigma)
f =
@(x)exp(-x.^2./(2*sigma))./sqrt(2*pi*sigma)
>> quadgk(f,-inf,inf,'AbsTol',1e-10,'RelTol',0)
ans =
0
This is because the function was zero everywhere the integrator "looked". If you know you have a problem like that, you can cope with it in one of two ways. You can split the interval where the action is:
>> quadgk(f,-inf,0,'AbsTol',1e-10,'RelTol',0)+quadgk(f,0,inf,'AbsTol',1e-10,'RelTol',0)
ans =
1
or you can toss in some waypoints to force the integrator to use a finer mesh where you believe the action is:
>> quadgk(f,-inf,inf,'AbsTol',1e-10,'RelTol',0,'Waypoints',linspace(-1e-3,1e-3,101))
ans =
1.000000000000000
although I have to say that using Waypoints for this purpose may require some trial and error. Obviously for such a function there is hardly any need to integrate from -inf to inf because the integrand evaluates to exactly zero for abs(x) => 4e-4 or so.
>> quadgk(f,-4e-4,4e-4,'AbsTol',1e-10,'RelTol',0)
ans =
1

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by