How do I plot left Riemann sum?

17 次查看(过去 30 天)
oletj
oletj 2017-11-20
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.

回答(1 个)

Nolan Canegallo
Nolan Canegallo 2019-10-15
编辑:Nolan Canegallo 2019-10-15
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.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by