You are on page 1of 13

Riemann Sums In Matlab

James K. Peterson

Department of Biological Sciences and Department of Mathematical Sciences


Clemson University

September 18, 2013

Outline

Graphing Riemann Sums

Uniform Partition Riemann Sums


Abstract

This lecture is going to talk about the new method of integration


Integration by Parts again. Then we go back and talk about
Riemann sums in Matlab again so we can learn how to draw a
picture that represents the Riemann sum.

If we want to graph the Riemann sums, we need to graph those


rectangles we draw by hand. To graph a rectangle, we graph 4
lines. The MatLab command plot([x1 x2], [y1 y2]) plots a
line from the pair (x1, y 1) to (x2, y 2).
I So the command
plot([x(i) x(i+1)],[f(s(i)) f(s(i))]); plots the
horizontal line which is the top of our rectangle.
I The command plot([x(i) x(i)], [0 f(s(i))]); plots a
vertical line that starts on the x axis at xi and ends at the
function value f (si ).
I The command plot([x(i+1) x(i+1)], [0 f(s(i))]);
plots a vertical line that starts on the x axis at xi+1 and ends
at the function value f (si ).
I To plot rectangle, for the first pair of partition points, first we
set the axis of our plot so we will be able to see it. We use
the axis command in Matlab – look it up using help! If the
two x points are x1 and x2 and the y value is f (s1) where s1
is the first evaluation point, we expand the x axis to
[x1 − 1, x2 + 1] and expand the y axis to [0, f (s1)]. This
allows our rectangle to be seen. The command is
axis([x1-1 x2+1 0 f((s1))+1]);.

The code so far Putting this all together, we plot the first
rectangle like this:
>> h o l d on
% s e t a x i s s o we c a n s e e r e c t a n g l e
>> a x i s ( [ P ( 1 ) −1 P ( 2 ) +1 0 f ( E ( 1 ) ) +1])
% p l o t top , LHS , RHS and bottom o f r e c t a n g l e
>> p l o t ( [ P ( 1 ) P ( 2 ) ] , [ f ( E ( 1 ) ) f ( E ( 1 ) ) ] ) ;
>> p l o t ( [ P ( 1 ) P ( 1 ) ] , [ 0 f ( E ( 1 ) ) ] ) ;
>> p l o t ( [ P ( 2 ) P ( 2 ) ] , [ 0 f ( E ( 1 ) ) ] ) ;
>> p l o t ( [ P ( 1 ) P ( 2 ) ] , [ 0 0 ] ) ;
>> h o l d o f f

We have to force Matlab to plot repeatedly without erasing the


previous plot. We use hold on and hold off to do this. We start
with hold on and then all plots are kept until the hold off is used.
This generates the rectange we see below:

Figure: Simple Rectangle

To show the Riemann sum approximation as rectangles, we use a


for loop in MatLab To put this all together,
h o l d on % s e t h o l d t o on
f o r i = 1:4 % graph r e c t a n g l e s
bottom = 0 ;
top = f (E( i ) ) ;
p l o t ( [ P( i ) P( i +1) ] , [ f ( E ( i ) ) f ( E ( i ) ) ] ) ;
p l o t ( [ P( i ) P( i ) ] , [ bottom t o p ] ) ;
p l o t ( [ E ( i ) E ( i ) ] , [ bottom t o p ] , ’ r ’ ) ;
p l o t ( [ P( i +1) P( i +1) ] , [ bottom t o p ] ) ;
p l o t ( [ P( i ) P( i +1) ] , [ 0 0 ] ) ;
end
hold o f f % set hold o f f
I Of course, we don’t know if f can be negative, so we need to
adjust our thinking as some of the rectangles might need to
point down. We do that by setting the bottom and top of
the rectangles using an if test.
I
bottom = 0 ;
top = f (E( i ) ) ;
i f f (E( i ) ) < 0
top = 0 ;
bottom = f ( E ( i ) ) ;
end

All together, we have


h o l d on % s e t h o l d t o on
[ s i z e P ,m] = s i z e (P) ;
f o r i = 1 : s i z e P −1 % g r a p h a l l t h e r e c t a n g l e s
bottom = 0 ;
top = f (E( i ) ) ;
i f f (E( i ) ) < 0
top = 0 ;
bottom = f ( E ( i ) ) ;
end
p l o t ( [ P( i ) P( i +1) ] , [ f ( E ( i ) ) f ( E ( i ) ) ] ) ;
p l o t ( [ P( i ) P( i ) ] , [ bottom t o p ] ) ;
p l o t ( [ E ( i ) E ( i ) ] , [ bottom t o p ] , ’ r ’ ) ;
p l o t ( [ P( i +1) P( i +1) ] , [ bottom t o p ] ) ;
p l o t ( [ P( i ) P( i +1) ] , [ 0 0 ] ) ;
end
hold o f f ;

We also want to place the plot of f over these rectangles.


h o l d on % s e t h o l d t o on
[ s i z e P ,m] = s i z e (P) ;
f o r i = 1 : s i z e P −1 % g r a p h a l l t h e r e c t a n g l e s
bottom = 0 ;
top = f (E( i ) ) ;
i f f (E( i ) ) < 0
top = 0 ;
bottom = f ( E ( i ) ) ;
end
p l o t ( [ P( i ) P( i +1) ] , [ f ( E ( i ) ) f ( E ( i ) ) ] ) ;
p l o t ( [ P( i ) P( i ) ] , [ bottom t o p ] ) ;
p l o t ( [ E ( i ) E ( i ) ] , [ bottom t o p ] , ’ r ’ ) ;
p l o t ( [ P( i +1) P( i +1) ] , [ bottom t o p ] ) ;
p l o t ( [ P( i ) P( i +1) ] , [ 0 0 ] ) ;
end
y = l i n s p a c e (P ( 1 ) ,P( s i z e P ) , 1 0 1 ) ;
plot (y , f (y) ) ;
xlabel ( ’ x axis ’) ; ylabel ( ’ y axis ’) ;
t i t l e ( ’ Riemann Sum w i t h f u n c t i o n o v e r l a i d ’ ) ;
hold o f f ;

We generate this figure:

Figure: Riemann Sum for f (x) = x 2 for Partition {1, 1.5, 2.1, 2.8, 3.0}
To save typing, let’s learn to use a Matlab function.
I In Matlab’s file menu, choose create a new Matlab function
which gives
f u n c t i o n [ value1 , value2 , . . . ] =
MyFunction ( arg1 , arg2 , . . . )
% s t u f f in here
end

I [value1, value2,...] are returned values the function


calculates that we want to save.
I (arg1, arg2,...) are things the function needs to do the
calculations. They are called the arguments to the function.
I MyFunction is the name of the function. This function must
be stored in the file MyFunction.m.

Our function returns the Riemann sum, RS, and use the
arguments: our function f , the partition P and the Evaluation set
E . Since only one value returned [RS] can be RS.
f u n c t i o n RS = RiemannSum ( f , P , E )
% comments a l w a y b e g i n w i t h a %
matlab l i n e s here
end

The name for the function RiemannSum must be used as the file
name: i.e. we must use RiemannSum.m as the file name.
The Riemann sum function:

1 f u n c t i o n RS = RiemannSum ( f , P , E )
% f i n d Riemann sum
dx = d i f f (P) ;
RS = sum ( f ( E ) . ∗ dx ) ;
[ s i z e P ,m] = s i z e (P) ; %g e t s i z e o f P a r t i t i o n
6 c l f ; % c l e a r the old graph
h o l d on % s e t h o l d t o on
f o r i = 1 : s i z e (P) −1 % g r a p h r e c t a n g l e s
% p l o t r e c t a n g l e code . . .
end
11 % p l o t f u n c t i o n code . . .
y = l i n s p a c e (P ( 1 ) ,P( s i z e P ) , 1 0 1 ) ;
hold o f f ;
end

Now to see graphically how the Riemann sums converge to a finite


number, let’s write a new function: Riemann sums using uniform
partitions and midpoint evaluation sets.

1 f u n c t i o n RS = RiemannUniformSum ( f , a , b , n )
% s e t up a u n i f o r m p a r t i t i o n w i t h n+1 p o i n t s
d e l t a x = ( b−a ) / n ;
P = [ a : d e l t a x : b ] ; % makes a row v e c t o r
f o r i =1: n
6 s t a r t = a+( i −1) ∗ d e l t a x ;
s t o p = a+ i ∗ d e l t a x ;
E ( i ) = 0 . 5 ∗ ( s t a r t+s t o p ) ;
end
% s e n d i n t r a n s p o s e o f P and E s o we u s e
column v e c t o r s
11 % b e c a u s e o r i g i n a l RiemannSum f u n c t i o n u s e s
columns
RS = RiemannSum ( f , P ’ , E ’ ) ;
end
We can then generate a sequence of Riemann sums for different
values of n. We generate a sequence of figures which converge to a
fixed value.
>> f = @( x ) s i n ( 3 ∗ x ) ;
>> RS = RiemannSumTwo ( f , − 1 , 4 , 1 0 ) ;
>> RS= RiemannSumTwo ( f , − 1 , 4 , 2 0 ) ;
>> RS = RiemannSumTwo ( f , − 1 , 4 , 3 0 ) ;
>> RS= RiemannSumTwo ( f , − 1 , 4 , 4 0 ) ;

Figure: The Riemann sum with a uniform partition P10 of [−1, 4] for
n = 10. The function is sin(3x) and the Riemann sum is −0.6726.
Figure: Riemann sum with a uniform partition P20 of [−1, 4] for n = 20.
The function is sin(3x) and the Riemann sum is −0.6258.

Figure: Riemann sum with a uniform partition P40 of [−1, 4] for n = 40.
The function is sin(3x) and the Riemann sum is −0.6149.
Figure: Riemann sum with a uniform partition P80 of [−1, 4] for n = 80.
The function is sin(3x) and the Riemann sum is −0.6122.

Homework 27

For the given function f , interval [a, b] and choice of n, you’ll


calculate the corresponding uniform partition Riemann sum using
the functions RiemannSum in file RiemannSum.m and
RiemannUniformSum in file RiemannUniformSum.m. You can
download these functions as files from the class web site. Save
them in your personal class directory.
I Create a new word document in single space with matlab
fragments in bold font.
I The document starts with your name, MTHSC 111, Section,
HW number and the date.
Homework 27 Continued

I Create a new word document for this homework.


I Do the document in single space.
I Do matlab fragments in bold font.
I The document starts with your name, MTHSC 111, Section
Number, Date and Homework number.
I For each value of n, do a save as and save the figure with a
filename like HW#Problem#a[ ].png where [ ] is where you
put the number of the graph. Something like HW17a.png,
HW#Problem#b.png etc.
I Insert this picture into the doc resizing as needed to make it
look good. Explain in the doc what the picture shows.

Homework 27 Continued
Something like this:
Jim Peterson MTHSC 111, Section Number
today’s date and HW Number,
Problem 1:
Let f (t) = sin(5t) on the interval [−1, 4] and find the Riemann
Uniform sum approximations for n = 10, 20, 40 and 80.
% add e x p l a n a t i o n h e r e
>> f = @( x ) s i n ( 5 ∗ x ) ;
% add e x p l a n a t i o n h e r e
>> RS = RiemannUniformSum ( f , −1 ,4 ,10)
% add e x p l a n a t i o n h e r e
>> RS = RiemannUniformSum ( f , −1 ,4 ,20)
% add e x p l a n a t i o n h e r e
>> RS = RiemannUniformSum ( f , −1 ,4 ,40)
% add e x p l a n a t i o n h e r e
>> RS = RiemannUniformSum ( f , −1 ,4 ,80)
Homework 27 Continued

26.1 Let f (t) = t 2 − 2t + 3 on the interval [−2, 3] with


n = 8, 16, 32 and 48.
26.2 Let f (t) = sin(2t) on the interval [−1, 5] with n = 10, 40, 60
and 80.
26.3 Let f (t) = −t 2 + 8t + 5 on the interval [−2, 3] with
n = 4, 12, 30 and 50.

You might also like