<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754</link>
    <title>MATLAB Central Newsreader - trapz, numerical integration</title>
    <description>Feed for thread: trapz, numerical integration</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 02 Jun 2009 22:27:02 -0400</pubDate>
      <title>trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#654206</link>
      <author>leo nidas</author>
      <description>&lt;br&gt;
Hi there,&lt;br&gt;
&lt;br&gt;
I have a code in a for loop. At the end of each iteration a (matematical) function, let &lt;br&gt;
f(x), comes up which is always decreasing. x&amp;gt;0 and f(x)-&amp;gt;0 as x-&amp;gt;Inf, also f(x)&amp;gt;0. Actually it is going exponentially to zero.&lt;br&gt;
&lt;br&gt;
My goal is to integrate numerically (using the trapz function) the function from 0 to Inf.&lt;br&gt;
Obviously I cannot insert Inf into the trapz (at least I think so, because it will at least take forever :-)  ).&lt;br&gt;
&lt;br&gt;
The thing is that I do not know a priori the exact form of the function to be integrated so as to insert a logical &quot;large&quot; number as an upper bound in the trapz. Till now I visualized some repetitions of the function that came up and the value 60 seemed more that large. But I want to be 100% sure that none of my functions gets away without proper integration.&lt;br&gt;
&lt;br&gt;
Is there any trick or any other function that &quot;detects&quot; the logical point up to a function should be integrated, if one wants to integrate it till Inf?&lt;br&gt;
&lt;br&gt;
Should I create a condition such as if f(x(i))&amp;lt;0.000001 then x(i) is my number? Or is there any better way?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanx in advance!!&lt;br&gt;
&lt;br&gt;
P.S. Anallytical integration is not possible.</description>
    </item>
    <item>
      <pubDate>Tue, 02 Jun 2009 23:15:04 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#654213</link>
      <author>John D'Errico</author>
      <description>&quot;leo nidas&quot; &amp;lt;bleonidas25@yahoo.gr&amp;gt; wrote in message &amp;lt;h048vl$2v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hi there,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a code in a for loop. At the end of each iteration a (matematical) function, let &lt;br&gt;
&amp;gt; f(x), comes up which is always decreasing. x&amp;gt;0 and f(x)-&amp;gt;0 as x-&amp;gt;Inf, also f(x)&amp;gt;0. Actually it is going exponentially to zero.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My goal is to integrate numerically (using the trapz function) the function from 0 to Inf.&lt;br&gt;
&amp;gt; Obviously I cannot insert Inf into the trapz (at least I think so, because it will at least take forever :-)  ).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The thing is that I do not know a priori the exact form of the function to be integrated so as to insert a logical &quot;large&quot; number as an upper bound in the trapz. Till now I visualized some repetitions of the function that came up and the value 60 seemed more that large. But I want to be 100% sure that none of my functions gets away without proper integration.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there any trick or any other function that &quot;detects&quot; the logical point up to a function should be integrated, if one wants to integrate it till Inf?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Should I create a condition such as if f(x(i))&amp;lt;0.000001 then x(i) is my number? Or is there any better way?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanx in advance!!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; P.S. Anallytical integration is not possible.&lt;br&gt;
&lt;br&gt;
Consider a Gauss-Laguerre or generalized Gauss-Laguerre&lt;br&gt;
quadrature. You can surely find something on the FEX.&lt;br&gt;
&lt;br&gt;
Or, transform your problem, using a mapping to a&lt;br&gt;
finite interval.&lt;br&gt;
&lt;br&gt;
Then use your favorite quadrature tool.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 08:40:17 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#655152</link>
      <author>leo nidas</author>
      <description>&lt;br&gt;
Thanx John,&lt;br&gt;
&lt;br&gt;
I downloaded quadgr that seemed better than others if one wanted to integrate till Inf.&lt;br&gt;
&lt;br&gt;
But consider the case:&lt;br&gt;
&lt;br&gt;
b0=-5;&lt;br&gt;
b1=3;&lt;br&gt;
m=3&lt;br&gt;
&lt;br&gt;
pc=  quadgr(@(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m.*exp(-t./m),0,Inf)&lt;br&gt;
&lt;br&gt;
%plot the function I want to integrate:&lt;br&gt;
gr=0:0.01:30;&lt;br&gt;
plot(gr,exp(b0+b1.*gr)./(1+exp(b0+b1.*gr)).*1/m.*exp(-gr./m))&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Quadgr doesn't seem to work here. I know that if I use trapz I could get a proper approximation but I need the integration untill Inf because my code is in a for loop and a new function (of the above form) to be integrated comes up. I.e. tha b0, b1, m may be different in each iteration with m&amp;gt;0.&lt;br&gt;
&lt;br&gt;
Thanx again for any help.</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 12:05:02 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#655171</link>
      <author>John D'Errico</author>
      <description>&quot;leo nidas&quot; &amp;lt;bleonidas25@yahoo.gr&amp;gt; wrote in message &amp;lt;h0fudh$pfo$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanx John,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I downloaded quadgr that seemed better than others if one wanted to integrate till Inf.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But consider the case:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; b0=-5;&lt;br&gt;
&amp;gt; b1=3;&lt;br&gt;
&amp;gt; m=3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; pc=  quadgr(@(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m.*exp(-t./m),0,Inf)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %plot the function I want to integrate:&lt;br&gt;
&amp;gt; gr=0:0.01:30;&lt;br&gt;
&amp;gt; plot(gr,exp(b0+b1.*gr)./(1+exp(b0+b1.*gr)).*1/m.*exp(-gr./m))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Quadgr doesn't seem to work here. I know that if I use trapz I could get a proper approximation but I need the integration untill Inf because my code is in a for loop and a new function (of the above form) to be integrated comes up. I.e. tha b0, b1, m may be different in each iteration with m&amp;gt;0.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanx again for any help.&lt;br&gt;
&lt;br&gt;
I had originally suggested the use of a Gauss-Laguerre&lt;br&gt;
numerical integration. If your function is this:&lt;br&gt;
&lt;br&gt;
fun = @(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m.*exp(-t./m)&lt;br&gt;
&lt;br&gt;
then it can be broken down into two pieces.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m&lt;br&gt;
&lt;br&gt;
and &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exp(-t./m)&lt;br&gt;
&lt;br&gt;
I'll look at the part which is not exponential to see if a&lt;br&gt;
Gauss-Laguerre will work well.&lt;br&gt;
&lt;br&gt;
K =@(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t))./m;&lt;br&gt;
ezplot(K)&lt;br&gt;
&lt;br&gt;
It is not really that polynomial, although it is well behaved,&lt;br&gt;
with a sigmoidal shape.&lt;br&gt;
&lt;br&gt;
I then tried a Gauss-Laguerre quadrature on this function. It&lt;br&gt;
is not polynomial enough that a low order Gauss-Laguerre&lt;br&gt;
works, and roots fails above 14 nodes to generate any higher&lt;br&gt;
order Gauss-Laguerre rules than that. (I should have used a&lt;br&gt;
better routine to solve for the Gauss rule. No matter, since&lt;br&gt;
there are better choices to be found.) Look instead at a&lt;br&gt;
numerical tool like quadgk.&lt;br&gt;
&lt;br&gt;
quadgk(fun,0,30,'reltol',1e-12)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584687911313577&lt;br&gt;
&lt;br&gt;
quadgk(fun,0,100,'reltol',1e-12)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243336&lt;br&gt;
&lt;br&gt;
quadgk(fun,0,200,'reltol',1e-12)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243339&lt;br&gt;
&lt;br&gt;
Or, try quadl. Note that quadl used more than 1200&lt;br&gt;
function evaluations to get this result, but I had set&lt;br&gt;
the tolerance rather small.&lt;br&gt;
&lt;br&gt;
[I,fev] = quadl(fun,0,200,1e-12)&lt;br&gt;
I =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243339&lt;br&gt;
fev =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1248&lt;br&gt;
&amp;nbsp;&lt;br&gt;
The default tolerance for quadl gave me a reasonable&lt;br&gt;
result at a cost of only 168 function evaluations. In fact&lt;br&gt;
this is far better accuracy than I would get from trapz&lt;br&gt;
anyway.&lt;br&gt;
&lt;br&gt;
[I,fev] = quadl(fun,0,200)&lt;br&gt;
I =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733310572222&lt;br&gt;
fev =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;168&lt;br&gt;
&lt;br&gt;
How well does trapz do? At a cost of 3000 function&lt;br&gt;
evaluations, you only get about 4 digits of accuracy,&lt;br&gt;
whereas the tools like quadl or quadgk will yield&lt;br&gt;
twice as many correct digits for vastly less work.&lt;br&gt;
&lt;br&gt;
t = 0:0.01:30;&lt;br&gt;
trapz(t,fun(t))&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584687862068707&lt;br&gt;
&lt;br&gt;
So I'm not at all sure why you have chosen to use&lt;br&gt;
trapz here, perhaps only that you did not know these&lt;br&gt;
other tools exist in MATLAB. Trapezoidal integration&lt;br&gt;
is rarely a good choice.&lt;br&gt;
&lt;br&gt;
Can we do better yet? Certainly so, if I knew that your&lt;br&gt;
function would always have the same essential character&lt;br&gt;
of a sigmoid multiplied by a negative exponential. &lt;br&gt;
&lt;br&gt;
I'll stop here for now, unless you wanted me to explore&lt;br&gt;
this last avenue more deeply. Ask again if so.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 16:53:01 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#655203</link>
      <author>leo nidas</author>
      <description>&lt;br&gt;
&amp;gt; I had originally suggested the use of a Gauss-Laguerre&lt;br&gt;
&amp;gt; numerical integration. If your function is this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; fun = @(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m.*exp(-t./m)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; then it can be broken down into two pieces.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     exp(-t./m)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'll look at the part which is not exponential to see if a&lt;br&gt;
&amp;gt; Gauss-Laguerre will work well.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K =@(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t))./m;&lt;br&gt;
&amp;gt; ezplot(K)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; It is not really that polynomial, although it is well behaved,&lt;br&gt;
&amp;gt; with a sigmoidal shape.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I then tried a Gauss-Laguerre quadrature on this function. It&lt;br&gt;
&amp;gt; is not polynomial enough that a low order Gauss-Laguerre&lt;br&gt;
&amp;gt; works, and roots fails above 14 nodes to generate any higher&lt;br&gt;
&amp;gt; order Gauss-Laguerre rules than that. (I should have used a&lt;br&gt;
&amp;gt; better routine to solve for the Gauss rule. No matter, since&lt;br&gt;
&amp;gt; there are better choices to be found.) Look instead at a&lt;br&gt;
&amp;gt; numerical tool like quadgk.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; quadgk(fun,0,30,'reltol',1e-12)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;          0.584687911313577&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; quadgk(fun,0,100,'reltol',1e-12)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;          0.584733311243336&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; quadgk(fun,0,200,'reltol',1e-12)&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;          0.584733311243339&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Or, try quadl. Note that quadl used more than 1200&lt;br&gt;
&amp;gt; function evaluations to get this result, but I had set&lt;br&gt;
&amp;gt; the tolerance rather small.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [I,fev] = quadl(fun,0,200,1e-12)&lt;br&gt;
&amp;gt; I =&lt;br&gt;
&amp;gt;          0.584733311243339&lt;br&gt;
&amp;gt; fev =&lt;br&gt;
&amp;gt;         1248&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; The default tolerance for quadl gave me a reasonable&lt;br&gt;
&amp;gt; result at a cost of only 168 function evaluations. In fact&lt;br&gt;
&amp;gt; this is far better accuracy than I would get from trapz&lt;br&gt;
&amp;gt; anyway.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [I,fev] = quadl(fun,0,200)&lt;br&gt;
&amp;gt; I =&lt;br&gt;
&amp;gt;          0.584733310572222&lt;br&gt;
&amp;gt; fev =&lt;br&gt;
&amp;gt;    168&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; How well does trapz do? At a cost of 3000 function&lt;br&gt;
&amp;gt; evaluations, you only get about 4 digits of accuracy,&lt;br&gt;
&amp;gt; whereas the tools like quadl or quadgk will yield&lt;br&gt;
&amp;gt; twice as many correct digits for vastly less work.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; t = 0:0.01:30;&lt;br&gt;
&amp;gt; trapz(t,fun(t))&lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt;          0.584687862068707&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So I'm not at all sure why you have chosen to use&lt;br&gt;
&amp;gt; trapz here, perhaps only that you did not know these&lt;br&gt;
&amp;gt; other tools exist in MATLAB. Trapezoidal integration&lt;br&gt;
&amp;gt; is rarely a good choice.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Can we do better yet? Certainly so, if I knew that your&lt;br&gt;
&amp;gt; function would always have the same essential character&lt;br&gt;
&amp;gt; of a sigmoid multiplied by a negative exponential. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'll stop here for now, unless you wanted me to explore&lt;br&gt;
&amp;gt; this last avenue more deeply. Ask again if so.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; John&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanx again John,&lt;br&gt;
&lt;br&gt;
It's me again.&lt;br&gt;
&lt;br&gt;
You were right that I didn't know these other tools in MATLAB. I tried them though and with trial and error I think that quadgk is the right choise. Although it didn't work for the next:&lt;br&gt;
&lt;br&gt;
setb0=-4.5313&lt;br&gt;
setb1=2.7481&lt;br&gt;
mhat=2.6470&lt;br&gt;
pc=quadgk(@(gr) exp(setb0+setb1.*gr)./(1+exp(setb0+setb1.*gr)).*1/mhat.*exp(-gr./mhat),0,Inf,'reltol',1e-12)&lt;br&gt;
&lt;br&gt;
For small changes of the values setb0 setb1 mhat it might work. As you can suppose setb0, setb1 mhat are random variables and I get a value of them in each iteration of the code. Then I try to integrate the above function which is the (sigmoidal*exponential distribution) . &lt;br&gt;
&lt;br&gt;
To tell you the whole problem I am interested in solving the following integrals for whatever values of setb0 setb1, and  mhat&amp;gt;0.&lt;br&gt;
&lt;br&gt;
Integrals for the exponential distribution:&lt;br&gt;
&lt;br&gt;
pc=  quadgk(@(gr) exp(setb0+setb1*gr)/(1+exp(setb0+setb1*gr))*1/mhat*exp(-gr/mhat),0,Inf);&lt;br&gt;
&lt;br&gt;
muc1= quadgk(@(gr) exp(setb0+setb1*gr)/((1+exp(setb0+setb1*gr))^2)*1/mhat*exp(-gr/mhat),0,Inf); &lt;br&gt;
&lt;br&gt;
muc2= quadgk(@(gr) gr*exp(setb0+setb1*gr)/((1+exp(setb0+setb1*gr))^2)*1/mhat*exp(-gr/mhat),0,Inf);&lt;br&gt;
&lt;br&gt;
As you can see, in all the above there is a product (something * exponential distribution).&lt;br&gt;
&lt;br&gt;
The integrals for another distribution (    specifically the extended generalized gamma with p.d.f.=(1/gamma(g)).*(g.^g)*(l.^(a.*g)).*(x.^(a.*g-1)).*exp(-g.*((l.*x).^a))   )    where  -Inf&amp;lt;a&amp;lt;Inf , l&amp;gt;0, g&amp;gt;0, x&amp;gt;0.&lt;br&gt;
&lt;br&gt;
are the corresponding ones that come if one substitute the pdf of exponential with the pdf of the GG. &lt;br&gt;
&lt;br&gt;
Any help for the above will be more than welcome...&lt;br&gt;
&lt;br&gt;
Maybe if you can guide me through the example I gave you on top I might find me way through to the rest...&lt;br&gt;
&lt;br&gt;
Thanx again for your time John!</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 18:20:16 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#655211</link>
      <author>leo nidas</author>
      <description>&lt;br&gt;
&lt;br&gt;
Well after some searching I think this is it:&lt;br&gt;
&lt;br&gt;
A new quadrature routine for improper and oscillatory integrals&lt;br&gt;
Applied Mathematics and Computation 189 (2007) 452&amp;#8211;461&lt;br&gt;
&lt;br&gt;
E. Sermutlu , H.T. Eyyuboglu</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 19:03:01 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#655215</link>
      <author>Matt Fig</author>
      <description>Why not use regular old Gauss legendre:&lt;br&gt;
&lt;br&gt;
setb0=-4.5313&lt;br&gt;
setb1=2.7481&lt;br&gt;
mhat=2.6470&lt;br&gt;
pc=gaussquad(@(gr) exp(setb0+setb1.*gr)./(1+exp(setb0+setb1.*gr)).*1/mhat.*exp(-gr./mhat),0,200)&lt;br&gt;
pc = &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.551709492450232&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
b0=-5;&lt;br&gt;
b1=3;&lt;br&gt;
m=3&lt;br&gt;
pc= quadgr(@(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m.*exp(-t./m),0,200)&lt;br&gt;
&lt;br&gt;
pc =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243339&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/8679&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/8679&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Sun, 07 Jun 2009 21:44:02 -0400</pubDate>
      <title>Re: trapz, numerical integration</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/252754#655236</link>
      <author>John D'Errico</author>
      <description>&quot;Matt Fig&quot; &amp;lt;spamanon@yahoo.com&amp;gt; wrote in message &amp;lt;h0h2t5$juo$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Why not use regular old Gauss legendre:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; setb0=-4.5313&lt;br&gt;
&amp;gt; setb1=2.7481&lt;br&gt;
&amp;gt; mhat=2.6470&lt;br&gt;
&amp;gt; pc=gaussquad(@(gr) exp(setb0+setb1.*gr)./(1+exp(setb0+setb1.*gr)).*1/mhat.*exp(-gr./mhat),0,200)&lt;br&gt;
&amp;gt; pc = &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      0.551709492450232&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; b0=-5;&lt;br&gt;
&amp;gt; b1=3;&lt;br&gt;
&amp;gt; m=3&lt;br&gt;
&amp;gt; pc= quadgr(@(t) exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*1/m.*exp(-t./m),0,200)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; pc =&lt;br&gt;
&amp;gt;          0.584733311243339&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/8679&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/8679&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
The problem is that that you need to decide how far to&lt;br&gt;
integrate out. Yes, 200 works here. It is not too large that&lt;br&gt;
the integration fails, seeing only zeros, returning zero.&lt;br&gt;
Too small of a value for the upper limit and it fails again,&lt;br&gt;
missing too much of the area. So if you choose a reasonable&lt;br&gt;
value for that upper limit, any code of reasonable quality&lt;br&gt;
will work nicely - quadgk, quadgr, quadl, etc. &lt;br&gt;
&lt;br&gt;
I'll suggest that a reasonable compromise is to pick some&lt;br&gt;
value cheaply, such that the integration need not do too&lt;br&gt;
much work. &lt;br&gt;
&lt;br&gt;
b0=-5;&lt;br&gt;
b1=3;&lt;br&gt;
m=3&lt;br&gt;
&lt;br&gt;
Here is the function of interest:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;exp(b0+b1.*t)./(1+exp(b0+b1.*t)).*exp(-t./m)/m&lt;br&gt;
&lt;br&gt;
Do a transformation,&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;s = t/m&lt;br&gt;
&lt;br&gt;
and play around a bit, so the function becomes&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;exp(-s)/(1 + exp(-(b0 + b1*m*s)))&lt;br&gt;
&lt;br&gt;
or, this&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;1/(exp(s) + exp(-b0 + (1 - b1*m)*s))&lt;br&gt;
&lt;br&gt;
At s = 0, we get 1/(1+exp(-b0)). It will grow to some&lt;br&gt;
maximum value, and then head back down to zero.&lt;br&gt;
&lt;br&gt;
Where does the max occur? When the denominator is&lt;br&gt;
minimized. This happens at the point:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;smax = (-b0 + log(b1*m - 1))/(b1*m)&lt;br&gt;
smax =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.786604615742204&lt;br&gt;
&lt;br&gt;
for these values of b0, b1, m.&lt;br&gt;
&lt;br&gt;
quadgr(funs,0,100*smax,1.e-13)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243334&lt;br&gt;
&lt;br&gt;
quadgk(funs,0,100*smax,'reltol',1.e-13)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243339&lt;br&gt;
&lt;br&gt;
We can integrate out to some reasonably large multiple&lt;br&gt;
of smax, and be confident that our result is correct.&lt;br&gt;
Perhaps even better is to use a composite rule. We&lt;br&gt;
know this function behaves like exp(-s) for large&lt;br&gt;
enough s. 10*smax will surely be reasonably large.&lt;br&gt;
&lt;br&gt;
So faster would seem to be to use a Gauss-Laguerre&lt;br&gt;
rule beyond that point, adding the result to that of&lt;br&gt;
your favorite code for small s. (In fact, quadgr seems&lt;br&gt;
to be a bit faster here than quadgk.) The composite&lt;br&gt;
method would seem to maximize both speed and&lt;br&gt;
accuracy. &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% ====================================&lt;br&gt;
function [I,smax] = lapint(b0,b1,m)&lt;br&gt;
% smax is the maximum of the integrand.&lt;br&gt;
% the break point between quadgr and the&lt;br&gt;
% laguerre will be 10*smax&lt;br&gt;
smax = (-b0 + log(b1*m - 1))/(b1*m);&lt;br&gt;
&lt;br&gt;
funs = @(s) 1./(exp(s) + exp(-b0 + (1-b1*m)*s));&lt;br&gt;
I = quadgr(funs,0,10*smax,1e-12);&lt;br&gt;
&lt;br&gt;
% now, use gauss-Laguerre for the rest, out to inf.&lt;br&gt;
s0 = 10*smax;&lt;br&gt;
Lagfun = @(u) 1./(1+exp(-b0-b1*m*(u+s0)));&lt;br&gt;
&lt;br&gt;
nodes = [0.229680505425136, 0.772144910375415, 1.63105309906745, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2.81514459001226, 4.33716407733756, 6.21464276455924, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8.47116398134672, 11.1383319657508, 14.2589100216242, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;17.8920534381695, 22.1226201748336, 27.0793114990475, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;32.9749735523974, 40.2165837114966, 49.8462217085566];&lt;br&gt;
weights = [0.0705024286637906, 0.249558909040495, 0.325533115492132, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.227727088139343, 0.0961880579830968, 0.0256342848941844, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.00435664720289306, 0.000467455769950909, 3.07992383129659e-05, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.18839046780406e-06, 2.49313557094408e-08, 2.52956064020393e-10, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1.01977766969811e-12, 1.12207052360831e-15, 1.30933713763764e-19];&lt;br&gt;
&lt;br&gt;
ILag = exp(-s0)*dot(Lagfun(nodes),weights);&lt;br&gt;
I = I + ILag;&lt;br&gt;
% ====================================&lt;br&gt;
&lt;br&gt;
Is it reasonably accurate? Yes. As accurate as are the&lt;br&gt;
others. &lt;br&gt;
&lt;br&gt;
[I,smax] = lapint(b0,b1,m)&lt;br&gt;
I =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.584733311243339&lt;br&gt;
smax =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.786604615742204&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
How fast is it?&lt;br&gt;
&lt;br&gt;
timeit(@() lapint(b0,b1,m))&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.00337505370568894&lt;br&gt;
&lt;br&gt;
timeit(@() quadgr(funs,0,100*smax))&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.00418888466636987&lt;br&gt;
&lt;br&gt;
timeit(@() quadgk(funs,0,100*smax))&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.00491137823164936&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John</description>
    </item>
  </channel>
</rss>

