[R] integral depending on a parameter

Carl Witthoft carl at witthoft.com
Sun Aug 7 21:25:56 CEST 2011


Just chiming in here...

Granted some of the help files are a bit cryptic until you get used to 
them (but still much easier to understand than the Unix man pages :-) 
).  This sort of thing does provide a chance to learn about the power of R.

 From Rolf's example,  suppose that  integrate() did NOT have any 
additional arguments.  Then, do this:

bar <- function(x)  foo(x,y=pi/4)

integrate(bar, 0, 2*pi)


As an exercise to the reader :-) , write a function which takes an input 
for the value of y and uses some subset of "paste, eval, do.call, parse" 
etc., to build the function  bar()   on the fly.

Carl


<quote>
From: Rolf Turner <rolf.turner_at_xtra.co.nz>
Date: Sat, 06 Aug 2011 11:00:11 +1200
On 06/08/11 08:17, brunero liseo wrote:
 > Is it possible to use the "integrate" function when it depends on a
 > parameter?
 > in other words can i get something like
 >
 > f(y) = integrate (g(x,y))?
 > If not, how else can I get that?
 > thanks in advance
This really boils down to ``RTFM''.

The help for "integrate" ``clearly'' ( :-) ) states that there is a 
"..." argument
which consists of ``additional arguments passed to f''. So use it.

E.g.:

      foo <- function(x,y){sin(x^2 + y^2)}
      integrate(foo,0,2*pi,y=pi/4)
      0.9314315 with absolute error < 1.5e-05

Most of the time, what you need to know is in the help. You just need to 
learn to read the help *carefully* and think along the lines: ``This 
*does* mean something useful and is actually expressed in a rational 
albeit it cryptic manner. Given that fact, what could this *possibly* 
mean?''
That will usually get you there. Then *try* what you think you gleaned 
from the help using a *simple* example.

      cheers,

          Rolf Turner
</quote>
-- 
-----
Sent from my Cray XK6



More information about the R-help mailing list