Mathematica Help with Right and Left Riemann Sums

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 6K views
jcharles513
Messages
21
Reaction score
0

Homework Statement


I am given a left riemann sum program module in Mathematica and need to convert it into the right riemann sum. The program takes values for x and f/x and the partition and graphs on a certain interval provided.

leftRiemannGraph[f_, a_, b_, n_] := Module[{expr},
expr[1] =
Table[{{x, f[x]}, {x + (b - a)/n, f[x]}}, {x, a, b, (b - a)/n}];
expr[2] = Flatten[expr[1], 1];
expr[3] = Drop[expr[2], -2];
plt[1] = ListLinePlot[expr[3], Filling -> Axis];
plt[2] = Plot[f[x], {x, a, b}];
Show[plt[1], plt[2]]]

Homework Equations


Knowledge about right and left riemann sums is necessary. Also partitions of the rectangles is (b-a/n).



The Attempt at a Solution


I attempted this problem for several hours. The only thing I really figured out is that expr[3]=Drop[expr[2],-2]; needs to be changed to expr[3]=Drop[expr[2],2]; to drop the first 2 not the last two. Also I know that the 3rd line is the one that needs changed. I'm just not sure what needs to be changed on it, in order to graph the right riemann sum. This has been racking my brain for the last week. Please help.
 
Physics news on Phys.org
The part where expr[1] is set creates a table of line segments. Letting x run through [itex]x_i = a + i \times (b - a)/n[/itex], a line segment is created from [itex](x_i, f(x_i))[/itex] to [itex](x_{i+1}, f(x_i))[/itex]. How would this change for a right Riemann sum?
 
It would be {o, f[x]} for the beginning I'm just not sure how to get this to work I tried taking out the x but that wasn't right.
 
I know what should change the right uppermost point of the bar should be on the line and an overestimate should occur. This would cause a shift leftward in the graph. I'm just not sure how to implement it and its stressing me out, because this is due tomorrow and I can't figure it out.
 
So let's consider the i'th segment, which runs from [itex]a + i * (b - a) / n[/itex] to [itex]a + (i + 1) * (b - a) / n[/itex].
In a left (or lower) Riemann sum, the segment would be at height f(a + i (b - a) / n). So if we let x run through (a, a + (b - a)/n, a + 2(b - a)/n, ..., a + (n - 1)(b - a) / n) then the segment would be at a height f(x).
Now where would it be for an upper Riemann sum?
 
It would be instead of i+ just start at 0
 
Huh? What do you mean?

I am starting at x = a, then each time adding (b - a) / n so in the n'th step I will be at x = b.

So suppose I am at some x and have to draw a line segment at height y which extends to the right for one step (so for length (b - a) / n, i.e. from x to x + (b - a) / n). Then what would y have to be for left and right Riemann sums?