How do I plot left Riemann sum?
15 views (last 30 days)
Show older comments
I want to plot a left Riemann sum like this: https://en.wikipedia.org/wiki/Riemann_sum#/media/File:LeftRiemann2.svg
I implemented it like this:
x = linspace(0,11);
y=((0.2*x).^3-0.2*x+0.5).*cos(0.13*x)
xa = linspace(0,10,11)
xb = xa +1;
yab = ((0.2*xa).^3-0.2*xa+0.5).*cos(0.13*xa);
plot(x,y);
i=1;
hold on;
for i=1:11
xz = [xa(i),xa(i),xb(i),xb(i)];
yz = [0,yab(i),yab(i),0];
plot(xz,yz,'-b');
i=i+1;
end
Is there a more easy and beautiful way to do it, e.g. by using bar()? With bar() I only found a way to plot Riemann midpoint rule. TY for your help.
0 Comments
Answers (1)
Nolan Canegallo
on 15 Oct 2019
Edited: Nolan Canegallo
on 15 Oct 2019
I think that your loop solution is the best solution, though a few of your lines are unnecessary.
i=1;
i=i+1;
If you do want to use the bar graph, I would use a midpoint Riemann sum, though I do not know how to remove the outline along the bottom of the boxes.
If you want to implement this idea:
x = linspace(0,11);
y =((0.2*x).^3-0.2*x+0.5).*cos(0.13*x);
xa = linspace(0,10,11);
xb = xa +1;
yab = ((0.2*xa).^3-0.2*xa+0.5).*cos(0.13*xa);
g = bar(xa,yab,'histc'); % Moves labels to the left
g.FaceColor = 'none'; % Changes the fill of bars
g.EdgeColor = 'b'; % Changes edge color of bars
hold on
plot(x,y);
axis tight % Sets axis to just fit the data
The output looks like this:
If you use the loop way, there will be no line along the x axis, though there is less to type here.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!