[R] Graphing the Area of Definite Integral

peter dalgaard pdalgd at gmail.com
Wed Dec 16 23:40:53 CET 2015


> On 16 Dec 2015, at 22:09 , David Winsemius <dwinsemius at comcast.net> wrote:
> 
>>> and look watch the graphs.
>> 
>> Looks fine to me, at least the non-plotmath version?
> 
> Here's a plotmath version that positions the expression in the center of the plot region.

Just in case: I wasn't implying an issue with the plot math version, I just never tried it.

(And by the way, the story was that I sketched a solution and Bill wrapped it as a function).

For completeness, here's a version of the trapezoidal rule (the only slightly tricky bit is that polygon() does not vectorize like rect() does):

showIntegral.tr <- function (f, xmin, xmax, n = 16)
{
  curve(f(x), from = xmin, to = xmax, lwd = 2, col = "blue")
  abline(h = 0)
  dx <- (xmax - xmin)/n
  right <- xmin + (1:n) * dx
  left <- right - dx
  fl <- f(left)
  fr <- f(right)
  PP <- Vectorize(function(l,r,fl,fr)
     polygon(c(l, r, r, l), c(0, 0, fr, fl) , density=20, border = "red"))
  PP(left,right, fl, fr)
  sum((fr+fl)/2 * dx)
}

showIntegral.tr(sqrt, xmin=0, xmax=4)


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list