# [R] Solving an integral in R gives the error “The integral is probably divergent”

peter dalgaard pdalgd at gmail.com
Fri Apr 12 20:31:54 CEST 2013

But is it supposed to be t^{-3/2} or t^{-0.5}?? The formula has the former and the code the latter, and the integral is clearly divergent with the former.

-pd

On Apr 12, 2013, at 04:51 , Thomas Lumley wrote:

> I don't get an error message (after I correct the missing line break after
> the comment
>
>> b<- sapply(a, Cfun, upper=1)
>> b
>  [1]  1.583458e-54  7.768026e-50  2.317562e-45  4.206260e-41  4.645737e-37
> 3.123801e-33  1.279358e-29  3.193257e-26  4.860876e-23
> [10]  4.516582e-20  2.564400e-17  8.908932e-15  1.896996e-12  2.481084e-10
> 1.998561e-08  9.946570e-07  3.067751e-05  5.862075e-04
> [19]  6.818952e-03  4.297061e-02  0.000000e+00  3.175122e-01  3.723022e-01
> 2.364930e-01  9.144836e-02  2.190878e-02  3.252754e-03
> [28]  2.983763e-04  1.685692e-05  5.849602e-07  1.244158e-08  1.619155e-10
> 1.287603e-12  6.250149e-15  1.850281e-17  3.338241e-20
> [37]  3.668412e-23  2.454192e-26  9.991546e-30  2.474577e-33  3.727226e-37
> 3.413319e-41  1.900112e-45  6.428505e-50  1.321588e-54
> [46]  1.650722e-59  1.252524e-64  5.772750e-70  1.615916e-75  2.746972e-81
> 2.835655e-87  1.777399e-93 6.764271e-100 1.562923e-106
> [55] 2.192373e-113 1.866955e-120 9.651205e-128 3.028623e-135 5.769185e-143
> 6.670835e-151 4.682023e-159 1.994643e-167 5.157808e-176
> [64] 8.095084e-185 7.711162e-194 4.458042e-203 1.564139e-212 3.330362e-222
> 4.302974e-232 3.373500e-242 1.604721e-252 4.631224e-263
> [73] 8.108474e-274 8.611898e-285 5.547745e-296  0.000000e+00  0.000000e+00
> 0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
> [82]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
> 0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
> [91]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
> 0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
> [100]  0.000000e+00
>
>
>  -thomas
>
>
>
> On Tue, Apr 9, 2013 at 3:14 PM, Janesh Devkota <janesh.devkota at gmail.com>wrote:
>
>> I am trying to solve an integral in R. However, I am getting an error when
>> I am trying to solve for that integral.
>>
>> The equation that I am trying to solve is as follows:
>>
>> $$C_m = \frac{{abs{x}}e^{2x}}{\pi^{1/2}}\int_0^t t^{-3/2}e^{-x^2/t-t}dt$$
>>
>> [image: enter image description here]
>>
>> The code that I am using is as follows:
>>
>> a <- seq(from=-10, by=0.5,length=100)
>> ## Create a function to compute integrationCfun <- function(XX, upper){
>>  integrand <- function(x)x^(-0.5)*exp((-XX^2/x)-x)
>>  integrated <- integrate(integrand, lower=0, upper=upper)\$value
>>  (final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }
>>
>>
>> b<- sapply(a, Cfun, upper=1)
>>
>> The error that I am getting is as follows:
>>
>> Error in integrate(integrand, lower = 0, upper = upper) :
>>  the integral is probably divergent
>>
>> Does this mean I cannot solve the integral ?
>>
>> Any possible ways to fix this problem will be highly appreciated.The
>> question can be found on
>>
>> http://stackoverflow.com/questions/15892586/solving-an-integral-in-r-gives-error-the-integral-is-probably-divergent
>> also.
>>
>> Thanks.
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help