http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148
MATLAB Central Newsreader  Integration
Feed for thread: Integration
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 22 Feb 2012 00:06:11 +0000
Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867466
steingre
Hi,<br>
<br>
I have a simple question concerning numerical integration.<br>
<br>
I need to numerical integrate a vector and use the trapz comment. However, I do not understand of how to do the non uniform spacing.<br>
<br>
For example, when I run the following program to compute the CDF of a exponential distribution. <br>
<br>
lambda(1)=1/8;<br>
Num=10000;<br>
<br>
for i=1:Num<br>
y(i,1) = i*10/Num;<br>
z(i,1) = exppdf(y(i,1),(1./lambda(1,1)));<br>
end<br>
<br>
trapz(y,z)<br>
<br>
Why do I not get 1?<br>
<br>
I appreciate any help!<br>
<br>
Best

Wed, 22 Feb 2012 04:15:12 +0000
Re: Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867486
Roger Stafford
"steingre" wrote in message <ji1bhj$cvt$1@newscl01ah.mathworks.com>...<br>
> lambda(1)=1/8;<br>
> Num=10000;<br>
> for i=1:Num<br>
> y(i,1) = i*10/Num;<br>
> z(i,1) = exppdf(y(i,1),(1./lambda(1,1)));<br>
> end<br>
> trapz(y,z)<br>
> <br>
> Why do I not get 1?<br>
        <br>
You haven't gone far enough out in the range of y to approximate infinity. You have stopped where y is equal to 10 which makes exppdf equal to 1/8*exp(10/8) = .0358 . This is not nearly far enough. Also you started at y = 0.001 instead of zero, another source of error. Finally, you should not expect exact answers because trapezoidal integration is only a numerical approximation to the ideal integral value.<br>
<br>
By the way, you *are* using uniform spacing here. Each interval in y is 0.001 .<br>
<br>
Roger Stafford

Wed, 22 Feb 2012 04:28:11 +0000
Re: Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867488
Roger Stafford
"Roger Stafford" wrote in message <ji1q4g$on4$1@newscl01ah.mathworks.com>...<br>
> You haven't gone far enough out in the range of y to approximate infinity. You have stopped where y is equal to 10 which makes exppdf equal to 1/8*exp(10/8) = .0358 . This is not nearly far enough.<br>
       <br>
I made a mistake in calculation, the furthest value is 8*exp(80) which is very small and should be far enough out. Whatever error you are getting is probably due to starting at .001. Besides correcting this, try taking smaller steps to see the effect. How large is your error at present?<br>
<br>
Roger Stafford

Wed, 22 Feb 2012 04:55:12 +0000
Re: Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867489
Roger Stafford
"Roger Stafford" wrote in message <ji1qsr$qmr$1@newscl01ah.mathworks.com>...<br>
> "Roger Stafford" wrote in message <ji1q4g$on4$1@newscl01ah.mathworks.com>...<br>
> > You haven't gone far enough out in the range of y to approximate infinity. ...<br>
> I made a mistake in calculation,....<br>
         <br>
Blush! My first post was correct and my second one in error. You haven't gone far enough out after all. The last value of z is <br>
<br>
1/(1/lambda)*exp(10/(1/lambda)) = 1/8*exp(10/8) = .0358<br>
<br>
That's too large.<br>
<br>
Roger Stafford

Wed, 22 Feb 2012 13:06:12 +0000
Re: Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867538
steingre
I started at 0 and go until 10 with steps 0.01 and it works!<br>
<br>
lambda(1)=1/10;<br>
Num=1000;<br>
<br>
x(:,1) = (exprnd((1./lambda(1,1)),Num,1));<br>
for i=1:Num<br>
y(i,1) = (i1)*100/Num;<br>
z(i,1) = exppdf(y(i,1),(1./lambda(1,1)));<br>
end<br>
trapz(y,z) <br>
<br>
<br>
However, do you know a method of how I get the optimal spacing? <br>
<br>
For example, I change lambda to 1/100 and I do not get 1 for the PDF only if I also change y.

Wed, 22 Feb 2012 18:45:12 +0000
Re: Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867619
Roger Stafford
"steingre" wrote in message <ji2p84$p2u$1@newscl01ah.mathworks.com>...<br>
> I started at 0 and go until 10 with steps 0.01 and it works! ......<br>
       <br>
The reason it worked is that you went until y equals 100, not 10. If you had only gone to 10 that is only at the new mean value point, which is way short. A 100 is safely past there. When you change to lambda = 1/100, that sets the mean value to 100, so you would then need to go far past there.<br>
<br>
You would get a better feel for what you are doing if you made plots of z against y ahead of time. You would see why some settings are okay and others not so good.<br>
<br>
plot(y,z)<br>
<br>
You need to take into consideration the nature of your integrand as you alter the mean parameter. As you have it set up, 'lambda' is the reciprocal of the mean of this distribution. The smaller 'lambda', the broader the distribution and therefore the further out you need to integrate for approximating an infinite limit.<br>
<br>
Roger Stafford

Wed, 22 Feb 2012 20:42:13 +0000
Re: Integration
http://www.mathworks.com/matlabcentral/newsreader/view_thread/317148#867632
steingre
"Roger Stafford" wrote in message <ji3d3o$79e$1@newscl01ah.mathworks.com>...<br>
> "steingre" wrote in message <ji2p84$p2u$1@newscl01ah.mathworks.com>...<br>
> > I started at 0 and go until 10 with steps 0.01 and it works! ......<br>
>        <br>
> The reason it worked is that you went until y equals 100, not 10. If you had only gone to 10 that is only at the new mean value point, which is way short. A 100 is safely past there. When you change to lambda = 1/100, that sets the mean value to 100, so you would then need to go far past there.<br>
> <br>
> You would get a better feel for what you are doing if you made plots of z against y ahead of time. You would see why some settings are okay and others not so good.<br>
> <br>
> plot(y,z)<br>
> <br>
> You need to take into consideration the nature of your integrand as you alter the mean parameter. As you have it set up, 'lambda' is the reciprocal of the mean of this distribution. The smaller 'lambda', the broader the distribution and therefore the further out you need to integrate for approximating an infinite limit.<br>
> <br>
> Roger Stafford<br>
<br>
Thank you for the clarification!