MATLAB Answers

How to plot Left - Rigth - Mid riemman sum of this code?

1 view (last 30 days)
David Araujo
David Araujo on 20 Oct 2020
Answered: Monisha Nalluru on 30 Oct 2020
I have this code, its for the left riemman sum
how can i plot this?
x1 = 1;
x2 = 4;
f=@(x)x;
n=500;
dx=(x2-x1)/n;
leftSum=0.0;
for i=1:n
leftSum=leftSum+f(x1+dx*(i-1));
end
leftSum=leftSum*dx;
x = linspace(0,11);
y = linspace(0,11);
xa = linspace(0,10,11);
z=integral(f,x1,x2);

  0 Comments

Sign in to comment.

Answers (1)

Monisha Nalluru
Monisha Nalluru on 30 Oct 2020
You can use bar and plot function and achieve the required output
As an example
lowerlimit = 1;
upperlimit = 4;
bins = 10;
x = linspace(lowerlimit,upperlimit); % points for function
y = x;
xa = linspace(lowerlimit,upperlimit,bins); % for bar plot points
ya = xa;
bar(xa,ya,'histc')
hold on
plot(x,y);
hold off
You can change the xa,ya values according to your function handle inputs!

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by