Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've got this task:

Use Mathematica to obtain an approximate value for the area between the curve $y=1/4$ and the x-axis over the interval $[1,2]$ with $50$ subintervals using the left endpoint, midpoint and right endpoint approximations. Note: the entire calculation needs to be shown in Mathematica.

Here's what I've tried:

a = 1; b = 2; n = 20; 
Ln = (b - a[f)/n*Sum[f[a + (i - 1) (b - a) / n], {i, 1, n}]
LeftSum[f_, a_, b_, n_] := (b - a)/n* Sum[f[a + (i - 1) (b - a)/n], {i, 1, n}]
f[x_] := 1/xLeft = N[LeftSum[f, 1, 2, 20]]
share|improve this question
    
a = 1; b = 2; n = 20; Ln = (b - a[f)/n*Sum[f[a + (i - 1) (b - a) / n], {i, 1, n}] –  Ashneel Aug 31 at 23:59
    
LeftSum[f_, a_, b_, n_] := (b = a)/n* Sum[f[a + (i - 1) (b - a)/n], {i, 1, n}] –  Ashneel Aug 31 at 23:59
    
Clear[f]f[x_] := 1/xLeft = N[LeftSum[f, 1, 2, 20]] –  Ashneel Sep 1 at 0:01
    
how can you please show me –  Ashneel Sep 1 at 0:03
    
2 - (3771059091081773 f)/5342931457063200 is this the output –  Ashneel Sep 1 at 0:15

2 Answers 2

I'll change your proposed function because it's a constant and for such a function all sums will be equal.So:

f[x_] := 1/4 x^2
di = (2 - 1)/50;
intervals = Range[1, 2, di];

leftSum = Sum[f[i] di, {i, Most@intervals}];
rightSum = Sum[f[i] di, {i, Rest@intervals}];
middleSum = Sum[f[i + di/2] di, {i, Most@intervals}];
exact = Integrate[f@x, {x, 1, 2}];
N@{leftSum, rightSum, middleSum, exact}

(*{0.57585, 0.59085, 0.583325, 0.583333}*)

Edit

NIntegrate[] already provides the Left and Right Riemann sums:

Quiet@NIntegrate[f@x, {x, 1, 2}, Method -> {"RiemannRule", "Type"-> #, "Points"-> 25}, 
                 MaxRecursion -> 0] & /@ {"Left", "Right"}

(* {0.57585, 0.59085}*)

You could verify that these Riemann sums are the same that we calculated earlier by doing:

pts = Rationalize@First@Last@Reap@Quiet@
        NIntegrate[f@x, {x, 1, 2}, Method -> {"RiemannRule", "Type" -> #, "Points" -> 25}, 
         MaxRecursion -> 0, EvaluationMonitor :> Sow[x]] & /@ {"Left", "Right"}

Complement[intervals, #] & /@ pts
(*{{2}, {1}}*)

So showing that the last or first point are excluded depending on the type of sum.

Edit

Framed@GraphicsRow[Plot[f[x], {x, 1, 1.25}, PlotStyle -> {Red, Thick}, 
     Epilog -> {Opacity[.5], Blue, EdgeForm[Darker@Blue], 
       Rectangle @@@ (Transpose /@ Transpose[{Partition[intervals, 2, 1], 
       Partition[Riffle[f /@ #@intervals, 0, {1, -2, 2}], 2]}])}] & /@ 
                                        {Most, Rest,MovingAverage[#, 2] &}]

Mathematica graphics

share|improve this answer
    
Pending upvote (could you produce some output ?) –  eldo Sep 1 at 3:22
    
@eldo Since the function proposed by the OP is constant, all Riemann sums are equal. I didn't include the numerical results because I thought they could bring some confusion. –  belisarius Sep 1 at 3:25
    
@eldo Done. Perhaps I found a way to alleviate the confusion. –  belisarius Sep 1 at 3:53
1  
Now I can see. Unfortunately, I can't upvote twice :) –  eldo Sep 1 at 4:00
f[x_] := 1/4
int = N@FindDivisions[{1, 2}, 50];
dx = int[[2]] - int[[1]];

Then:

leftSum = Total[f[#] dx & /@ Most[int]]
rightSum = Total[f[#] dx & /@ Rest[int]]
middleSum = Total[f[# + dx/2] dx & /@ Most@int]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.