Strange behaviour in integral function in MATLAB

4 views (last 30 days)
I create two functions fx and fy as follows:
fx = @(x) ncx2pdf(x, 10, 1);
fy = @(y) ncx2pdf(y + 25, 10, 10);
Then, I define fs function as follows:
shift = 0
fs = @(s) fx(s) .* fy(shift - s)
Note that fs is always positive (product of two probability density function). If I compute:
integral(fs, -Inf, 100)
I obtain the true value 0.0413, but If I compute
integral(fs, -Inf, 1000)
I obtain 0. Why this strange behavior happens using integral function? Note that if I compute
integral(fs, -Inf, Inf)
I obtain the true value 0.0413.

Accepted Answer

Brendan Hamm
Brendan Hamm on 14 Sep 2015
Numeric quadrature works by iteratively splitting the interval apart and approximating the integral of each partition. The approximation is done by discretizing the subintervals and using some form of integral approximation (Riemann sums etc.). Since there is some discretization, there will be some error and this is dependent on the partitioning, therefore we are best to keep our limits of integration closer to the points which will contribute most to the integral.
Since you know this function is approximately zero almost everywhere and has its largest weight around the origin (relatively speaking), the integral which samples points closer to the origin will perform better. For this reason it is a good idea to avoid using Inf and -Inf in this example. If however you really want to use these extremes, then it is best to use the Waypoints to let MATLAB know where it should be splitting the integral. Either of these will produce the results you are looking for:
integral(fs,-10000,10000) % Even this far away from the largest function values.
integral(fs,-Inf,1000,'Waypoints',-50:50) % MATLAB will split integral at these locations
  8 Comments
Abdal-bassir Abou El-ailah
It is a nice idea to use ncx2cdf to find the support at precision X. I implemented a simple approach based on the parameters to estimate the support of non-central Chi distribution, which gives sophisticated results. But now, for example, I can directly use fzero(@(x) ncx2cdf(x,1,1e1)- 0.9999,1e1) and fzero(@(x) ncx2cdf(x,1,1e1)- 0.001,1e1) to find the support at the precision that I want. Thanks for that.
Yes, I am always interested in finding the convolution of fx and fy. Now, I am trying to sample fx and fy and use directly 'conv' function in Matlab to estimate the convolution. Normally, the support of the convolution is [minFx + minFy, maxFx + maxFy], where [minFx, maxFx] and [minFy, maxFy] are the supports of fx and fy respectively (I do that to avoid the 'integral' function because it takes a lot of time, especially I must call it for each 'shift' value in the support).
By the way, in integral(fs,0,upperLimit,'Waypoints',linspace(0,upperLimit)), we can only use the support of fx (instead of [0, upperLimit]), but shift can vary in [0, upperLimit].
Thank you a lot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!