numerical integration of a ftn of 2 variables

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

numerical integration of a ftn of 2 variables

Chris Rhoads
Dear R gurus,

To start, let me confess to not being an experienced programmer, although I have used R fairly
extensively in my work as a
graduate student in statistics.

I wish to find the root of a function of two variables that is defined by an integral which must be
evaluated numerically.

So the problem I want to solve is of the form:  Find k such that f(k)=0, where f(y) = int_a^b
g(x,y) dx.  Again, the integral
involved must be done numerically.

I'm told by a friend who knows programming, but not R, that what I need to do is create something
like a "local environment"
within which I could create a placeholder for x.  So I want to make something like the following work.

f(var) <- function(var) {

cons <- var

g <- function(x,cons) {h(x,cons)}

ret <- function(cons) integrate(g(x,cons),a,b)$value
ret
}

I could then use (e.g.) a Newton Raphson algorithm to find the root of the function"f".

Thanks,

Chris Rhoads
Northwestern University

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: numerical integration of a ftn of 2 variables

paul smith-6
On Tue, Feb 19, 2008 at 11:07 PM, Chris Rhoads
<[hidden email]> wrote:

>  To start, let me confess to not being an experienced programmer, although I have used R fairly
>  extensively in my work as a
>  graduate student in statistics.
>
>  I wish to find the root of a function of two variables that is defined by an integral which must be
>  evaluated numerically.
>
>  So the problem I want to solve is of the form:  Find k such that f(k)=0, where f(y) = int_a^b
>  g(x,y) dx.  Again, the integral
>  involved must be done numerically.
>
>  I'm told by a friend who knows programming, but not R, that what I need to do is create something
>  like a "local environment"
>  within which I could create a placeholder for x.  So I want to make something like the following work.
>
>  f(var) <- function(var) {
>
>  cons <- var
>
>  g <- function(x,cons) {h(x,cons)}
>
>  ret <- function(cons) integrate(g(x,cons),a,b)$value
>  ret
>  }
>
>  I could then use (e.g.) a Newton Raphson algorithm to find the root of the function"f".

Can you Chris provide us an example with concrete functions? To us, it
would be easier to think about a concrete example.

Paul

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: numerical integration of a ftn of 2 variables

Berend Hasselman
In reply to this post by Chris Rhoads
Chris Rhoads wrote
I wish to find the root of a function of two variables that is defined by an integral which must be
evaluated numerically.

So the problem I want to solve is of the form:  Find k such that f(k)=0, where f(y) = int_a^b
g(x,y) dx.  Again, the integral
involved must be done numerically.
g <- function(x,p) 2*(x-p)

Find value of p such that integral_0^1 g(x,p)dx  is zero

f <- function(p) integrate(g,0,1,p)$value

uniroot(f, c(0,1))

$root should be 0.5 which is what is expected here.

Berend Hasselman