Search code examples
requationintegral

Finding of roots for an equation with an integral with no closed form in R


In R, how would I go about finding the (first) root, x for an equation such as this:

enter image description here

Unfortunately, R code such as this doesn't work since there's no symbolic structure in R

g <- function(t) integrate(sin(t)/t,lower=0,upper=x)
root <- uniroot(g,(c(0,1)))$root

In general I want the integrand to be any arbitrary function, which after integration may or may not have analytical close form solutions. Also, the left-hand-side could be any arbitrary constant.


Solution

  • Well, your syntax for such a problem is a bit off. Something like

    g <- Vectorize(function(x) integrate(function(t) sin(t)/t,lower=0,upper=x)$value-1)
    root <- uniroot(g,c(0,1))$root
    

    is a bit closer. R's not good about evaluating the equation at 0 in this case. You can move away from 0. Plus, accoring to Wolfram Alpha the "root" there appears at 1.06 which is outside your search range. This should give the right answer

    g <- Vectorize(function(x) integrate(function(t) sin(t)/t,lower=0,upper=x)$value-1)
    root <- uniroot(g,c(.01,1.5))$root
    root
    # [1] 1.06482