How do I plot left Riemann sum?

조회 수: 4 (최근 30일)
oletj
oletj 2017년 11월 20일
편집: Nolan Canegallo 2019년 10월 15일
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