[R] Graphing the Area of Definite Integral
David Winsemius
dwinsemius at comcast.net
Wed Dec 16 22:09:30 CET 2015
> On Dec 16, 2015, at 12:24 PM, peter dalgaard <pdalgd at gmail.com> wrote:
>
>
>> On 16 Dec 2015, at 20:58 , Steven Stoline <sstoline at gmail.com> wrote:
>>
>> Dear David:
>>
>>
>> could you please try it for the functions f(x)=x^2 from -4 to 4 and the function f(x) = sqrt(x) from 0 to 4, 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.
showIntegral.rt <- 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
fr <- f(right)
rect(left, 0, right, fr, density = 5, border = "red")
points(right, fr, col = "red", cex = 1.25, pch = 19)
text(x=(xmax-xmin)/2,
y=(f(xmax)-f(xmin))/2,
bquote( integral(.(body(f))*dx, a, b) == .(sum(fr * dx)) ),
col="orange" )
}
Works as desired with sqrt(x)
showIntegral.rt(f=function(x) sqrt(x), xmin=0, xmax=4, n=5000)
With higher n, one gets an animation effect in the painting of the interactive window.
--
David.
>
> -pd
>
>>
>>
>> Thank you very much for your helps.
>>
>>
>> steve
>>
>>
>> On Wed, Dec 16, 2015 at 2:09 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>>> On Dec 16, 2015, at 9:00 AM, Steven Stoline <sstoline at gmail.com> wrote:
>>>
>>> Dear William: *Left and Right Riemann Sums*
>>>
>>>
>>> Is there is a way to modify your function to compute Left Riemann Sum and
>>> Right Riemann Sum. I tried to modify yours, but i was not be able to make
>>> it work correctly.
>>>
>>> This is your function used to compute the Middle Riemann Sum.
>>
>> I think it's actually Dalgaard's method.
>>>
>>> showIntegral.med <- 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
>>> mid <- right - dx/2
>>> fm <- f(mid)
>>> rect(left, 0, right, fm, density = 20, border = "red")
>>> points(mid, fm, col = "red", cex = 1.25, pch = 19)
>>> sum(fm * dx)
>>> }
>>>
>>>
>>>
>>> ### Example 1: f(x) = x^2 , xmin=-4, xmax=4
>>> ### ===============================
>>>
>>>
>>>
>>> showIntegral.med(f=function(x)x^2, xmin=-4, xmax=4, n=16)
>>
>> Wouldn't it just involve skipping the 'mid' calculations and using either the right or left values? Illustration for right:
>>
>> showIntegral.rt <- 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
>> fr <- f(right)
>> rect(left, 0, right, fr, density = 20, border = "red")
>> points(right, fr, col = "red", cex = 1.25, pch = 19)
>> sum(fr * dx)
>> }
>>
>> You can make it prettier with plotmath:
>>
>> showIntegral.rt <- 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
>> fr <- f(right)
>> rect(left, 0, right, fr, density = 20, border = "red")
>> points(right, fr, col = "red", cex = 1.25, pch = 19)
>> sum(fr * dx)
>> text(0,10, # might want to do some adaptive positioning instead
>> bquote( integral( .(body(f) )*dx, a, b) == .( sum(fr * dx )) ) )
>> }
>>
>> --
>> David.
>>
>>>
>>>
>>>
>>> with many thanks
>>> steve
>>>
>>> On Sat, Nov 28, 2015 at 1:11 PM, William Dunlap <wdunlap at tibco.com> wrote:
>>>
>>>> Your right <- (1:n)*dx mean that your leftmost rectangle's left edge
>>>> is at 0, but you want it to be at -4. You should turn this into a function
>>>> so you don't have to remember how the variables in your code depend
>>>> on one another. E.g.,
>>>>
>>>> showIntegral <- 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
>>>> mid <- right - dx/2
>>>> fm <- f(mid)
>>>> rect(left, 0, right, fm, density = 20, border = "red")
>>>> points(mid, fm, col = "red", cex = 1.25, pch = 19)
>>>> sum(fm * dx)
>>>> }
>>>>> showIntegral(f=function(x)x^2, xmin=-4, xmax=4, n=16)
>>>> [1] 42.5
>>>>> showIntegral(f=function(x)x^2, xmin=-4, xmax=4, n=256)
>>>> [1] 42.66602
>>>>> showIntegral(f=function(x)x^2, xmin=-4, xmax=4, n=1024)
>>>> [1] 42.66663
>>>>
>>>>> 2*4^3/3
>>>> [1] 42.66667
>>>>> showIntegral
>>>> Bill Dunlap
>>>> TIBCO Software
>>>> wdunlap tibco.com
>>>>
>>>>
>>>> On Fri, Nov 27, 2015 at 9:50 PM, Steven Stoline <sstoline at gmail.com>
>>>> wrote:
>>>>> Dear Peter: in my previous email I forgot to reply to the list too
>>>>>
>>>>> I used your code for more than one examples, and it works nicely. But
>>>> when
>>>>> I tried to use for the the function: f(x) = x^2, it looks like I am
>>>> missing
>>>>> something, but I could not figured it out.
>>>>>
>>>>> This what I used:
>>>>>
>>>>>
>>>>>
>>>>> f <- function(x) x^2
>>>>>
>>>>> curve(f(x), from=-4, to=4, lwd=2, col="blue")
>>>>> abline(h=0)
>>>>> n <- 16
>>>>> dx <- 8/n
>>>>> right <- (1:n)*dx
>>>>> left <- right - dx
>>>>> mid <- right - dx/2
>>>>> fm <- f(mid)
>>>>> rect(left,0,right,fm, density = 20, border = "red")
>>>>> points(mid, fm, col = "red", cex = 1.25, pch=19)
>>>>> sum(fm*dx)
>>>>>
>>>>>
>>>>>
>>>>> 1/3 * (64+64)
>>>>>
>>>>>
>>>>>
>>>>> with many thanks
>>>>> steve
>>>>>
>>>>> On Fri, Nov 27, 2015 at 3:36 PM, Steven Stoline <sstoline at gmail.com>
>>>> wrote:
>>>>>
>>>>>> many thanks
>>>>>>
>>>>>> steve
>>>>>>
>>>>>> On Fri, Nov 27, 2015 at 9:20 AM, peter dalgaard <pdalgd at gmail.com>
>>>> wrote:
>>>>>>
>>>>>>> Something like this?
>>>>>>>
>>>>>>> f <- function(x) x^3-2*x
>>>>>>> curve(f(x), from=0, to=4)
>>>>>>> abline(h=0)
>>>>>>> n <- 16
>>>>>>> dx <- 4/n
>>>>>>> right <- (1:n)*dx
>>>>>>> left <- right - dx
>>>>>>> mid <- right - dx/2
>>>>>>> fm <- f(mid)
>>>>>>> points(mid, fm)
>>>>>>> rect(left,0,right,fm)
>>>>>>>
>>>>>>> sum(fm*dx)
>>>>>>>
>>>>>>> 1/4 * 4^4 - 4^2
>>>>>>>
>>>>>>>
>>>>>>> -pd
>>>>>>>
>>>>>>>
>>>>>>> On 27 Nov 2015, at 13:52 , Steven Stoline <sstoline at gmail.com> wrote:
>>>>>>>
>>>>>>>> Dear All:
>>>>>>>>
>>>>>>>> I am trying to explain to my students how to calculate the definite
>>>>>>>> integral using the Riemann sum. Can someone help me to graph the area
>>>>>>> under
>>>>>>>> the curve of the function, showing the curve as well as the
>>>> rectangles
>>>>>>>> between 0 and 4..
>>>>>>>>
>>>>>>>> *f(x) = x^3 - 2*x *
>>>>>>>>
>>>>>>>> over the interval [0 , 4]
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> with many thanks
>>>>>>>> steve
>>>>>>>>
>>>>>>>> --
>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list