Riemann Integration James K. Peterson Department of Biological Sciences and Department of Mathematical Sciences Clemson University February 2, 2017
Outline 1 Riemann Sums 2 Riemann Sums In MatLab 3 Graphing Riemann Sums
Riemann Sums To study the integration of a function f, there are two intellectually separate ideas: the primitive or antiderivative and the Riemann integral.
Riemann Sums To study the integration of a function f, there are two intellectually separate ideas: the primitive or antiderivative and the Riemann integral. We have 1 the idea of a Primitive or Antiderivative: f is any function F which is differentiable and satisfies F (t) = f (t) at all points in the domain of f. Normally, the domain of f is a finite interval of the form [a, b], although it could also be an infinite interval like all of R or [1, ) and so on. Note that an antiderivative does not require any understanding of the process of Riemann integration at all only what differentiation is! 2 The idea of the Riemann integral of a function.
Riemann Sums Riemann integration is far more complicated to setup than the process of guessing a primitive or antiderivative.
Riemann Sums Riemann integration is far more complicated to setup than the process of guessing a primitive or antiderivative. To define a Riemann integral properly, first, we start with a bounded function f on a finite interval [a, b]. This kind of function f need not be continuous!
Riemann Sums Riemann integration is far more complicated to setup than the process of guessing a primitive or antiderivative. To define a Riemann integral properly, first, we start with a bounded function f on a finite interval [a, b]. This kind of function f need not be continuous! Then select a finite number of points from the interval [a, b], {t 0, t 1,,..., t n 1, t n }. We don t know how many points there are, so a different selection from the interval would possibly gives us more or less points. But for convenience, we will just call the last point t n and the first point t 0. These points are not arbitrary t 0 is always a, t n is always b and they are ordered like this: t 0 = a < t 1 < t 2 <... < t n 1 < t n = b The collection of points from the interval [a, b] is called a Partition of [a, b] and is denoted by some letter here we will use the letter P.
Riemann Sums So if we say P is a partition of [a, b], we know it will have n + 1 points in it, they will be labeled from t 0 to t n and they will be ordered left to right with strict inequalities. But, we will not know what value the positive integer n actually is.
Riemann Sums So if we say P is a partition of [a, b], we know it will have n + 1 points in it, they will be labeled from t 0 to t n and they will be ordered left to right with strict inequalities. But, we will not know what value the positive integer n actually is. The simplest Partition P is the two point partition {a, b}. Note these things also: 1 Each partition of n + 1 points determines n subintervals of [a, b] 2 The lengths of these subintervals always adds up to the length of [a, b] itself, b a. 3 These subintervals can be represented as {[t 0, t 1 ], [t 1, t 2 ],..., [t n 1, t n ]} or more abstractly as [t i, t i+1 ] where the index i ranges from 0 to n 1. 4 The length of each subinterval is t i+1 t i for the indices i in the range 0 to n 1. 5 The largest subinterval length is called the norm of the partition and we denote it by the symbol P.
Riemann Sums Now from each subinterval [t i, t i+1 ] determined by the Partition P, select any point you want and call it s i.
Riemann Sums Now from each subinterval [t i, t i+1 ] determined by the Partition P, select any point you want and call it s i. This will give us the points s 0 from [t 0, t 1 ], s 1 from [t 1, t 2 ] and so on up to the last point, s n 1 from [t n 1, t n ].
Riemann Sums Now from each subinterval [t i, t i+1 ] determined by the Partition P, select any point you want and call it s i. This will give us the points s 0 from [t 0, t 1 ], s 1 from [t 1, t 2 ] and so on up to the last point, s n 1 from [t n 1, t n ]. At each of these points, we can evaluate the function f to get the value f (s j ). Call these points an Evaluation Set for the partition P. Let s denote such an evaluation set by the letter E.
Riemann Sums Now from each subinterval [t i, t i+1 ] determined by the Partition P, select any point you want and call it s i. This will give us the points s 0 from [t 0, t 1 ], s 1 from [t 1, t 2 ] and so on up to the last point, s n 1 from [t n 1, t n ]. At each of these points, we can evaluate the function f to get the value f (s j ). Call these points an Evaluation Set for the partition P. Let s denote such an evaluation set by the letter E. If the function f was nice enough to be positive always and continuous, then the product f (s i ) (t i+1 t i ) can be interpreted as the area of a rectangle.
Riemann Sums Then, if we add up all these rectangle areas we get a sum which is useful enough to be given a special name: the Riemann Sum for the function f associated with the Partition P and our choice of evaluation set E = {s 0,..., s n 1 }.
Riemann Sums Then, if we add up all these rectangle areas we get a sum which is useful enough to be given a special name: the Riemann Sum for the function f associated with the Partition P and our choice of evaluation set E = {s 0,..., s n 1 }. This sum is represented by the symbol S(f, P, E) where the things inside the parenthesis are there to remind us that this sum depends on our choice of the function f, the partition P and the evaluation set E. We have S(f, P, E) = n 1 i=0 f (s i ) (t i+1 t i )
Riemann Sums Definition The Riemann sum for the bounded function f, the partition P and the evaluation set E = {s 0,..., s n 1 } from P{t 0, t 1,,..., t n 1, t n } is defined by S(f, P, E) = n 1 i=0 f (s i ) (t i+1 t i ) It is pretty misleading to write the Riemann sum this way as it can make us think that the n is always the same when in fact it can change value each time we select a different P. So many of us write the definition this way instead S(f, P, E) = i P f (s i) (t i+1 t i ) and we just remember that the choice of P will determine the size of n.
Riemann Sums Example Let s look at an example of all this. Here we see the graph of a typical function which is always positive on some finite interval [a, b]. (a, f (a)) (b, f (b)) A generic curve f on the interval [a, b] which is always positive. Note the area under this curve is the shaded region. a b Figure: The Area Under The Curve f
Riemann Sums Example Continued Next, let s set the interval to be [1, 6] and compute the Riemann Sum for a particular choice of Partition P and evaluation set E. The partition (gray) is P = {1.0, 1.5, 2.6, 3.8, 4.3, 5.6, 6.0}. Hence, we have subinterval lengths of t 1 t 0 = 0.5, t 2 t 1 = 1.1, t 3 t 2 = 1.2, t 4 t 3 = 0.5, t 5 t 4 = 1.3 and t 6 t 5 = 0.4, giving P = 1.3. For the evaluation set (red) E = {1.1, 1.8, 3.0, 4.1, 5.3, 5.8} the Riemann sum is S(f, P, E) = 5 f (s i ) (t i+1 t i ) i=0 = f (1.1) 0.5 + f (1.8) 1.1 + f (3.0) 1.2 + f (4.1) 0.5 + f (5.3) 1.3 + f (5.8) 0.4
Riemann Sums Example Continued (1, f (1)) (6, f (6)) The partition points are in gray and the evaluation points are in red. 1 6 Figure: A Simple Riemann Sum
Riemann Sums Example Continued We can also interpret the Riemann sum as an approximation to the area under the curve. The partition (gray) is P = {1.0, 1.5, 2.6, 3.8, 4.3, 5.6, 6.0}. For the evaluation set (red) E = {1.1, 1.8, 3.0, 4.1, 5.3, 5.8} (1, f (1)) (6, f (6)) 1 6 Figure: The Riemann Sum As An Approximate Area
Riemann Sums Example Example We let f (t) = t 2 + 6t 8 on the interval [2, 4] with P = {2, 2.5, 3.0, 3.7, 4.0} and E = {2.2, 2.8, 3.3, 3.8}. Solution The partition determines subinterval lengths of t 1 t 0 = 0.5, t 2 t 1 = 0.5, t 3 t 2 = 0.7, and t 4 t 3 = 0.3, giving P = 0.7. For E and P, we have the Riemann sum S(f, P, E) = 3 i=0 f (s i) (t i+1 t i ). Thus S(f, P, E) = f (2.2) 0.5 + f (2.8) 0.5 + f (3.3) 0.7 + f (3.8) 0.3 = 37.995
Riemann Sums Example Example Let f (t) = 3t 2 on the interval [ 1, 2] with P = { 1, 0.3, 0.6, 1.2, 2.0} and E = { 0.7, 0.2, 0.9, 1.6}. Find the Riemann sum. Solution The partition determines subinterval lengths of t 1 t 0 = 0.7, t 2 t 1 = 0.9, t 3 t 2 = 0.6, and t 4 t 3 = 0.8, giving P = 0.9. For the evaluation set E the Riemann sum is S(f, P, E) = f ( 0.7) 0.7 + f (0.2) 0.9 + f (0.9) 0.6 + f (1.6) 0.8 = 8.739
Riemann Sums In MatLab Let s start with Riemann sums in Matlab. As you have seen, doing these by hand is tedious. Let s look at how we might do them using MatLab. Here is a typical MatLab session to do this. Let s calculate the Riemann sum for the function f (x) = x 2 on the interval [1, 3] using the partition P = {1, 1.5, 2.1, 2.8, 3.0} and evaluation set E = {1.2, 1.7, 2.5, 2.9}. First, set up our function. MatLab allows us to define a function inside the MatLab environment as follows >> f = @( x ) ( x. ˆ 2 ) ; This defines the function f (x) = x 2. If we had wanted to define g(x) = 2x 2 + 3, we would have used >> g = @( x ) ( 2 x. ˆ 2 + 3) ; Now define the vector X by >> X = [ 1 ; 2 ; 3 ] ;
Riemann Sums In MatLab To square X, we would write X.^2 to square each component creating a new vector with each entry squared. >> X. ˆ 2 ans = 1 4 9 The way we set up the function f = @(x) (x.^2); makes use of this. The variable X may or may not be a vector. So we write x.^2 so that if x is a vector, multiplication is done component wise and if not, it is just the squaring of a number. So for our function, to find f for all the values in X, we just type >> f (X) ans = 1 4 9
Riemann Sums In MatLab Now let s setup the partition with the command >> P = [ 1 ; 1. 5 ; 2. 1 ; 2. 8 ; 3. 0 ] P = 1. 0 0 0 0 1. 5 0 0 0 2. 1 0 0 0 2. 8 0 0 0 3. 0 0 0 0 The command diff in MatLab is applied to a vector to create the differences we have called the x i s. >> dx = d i f f (P) dx = 0. 5 0 0 0 0. 6 0 0 0 0. 7 0 0 0 0. 2 0 0 0
Riemann Sums In MatLab Next, we set up the evaluation set E. >> E = [ 1. 2 ; 1. 7 ; 2. 5 ; 2. 9 ] E = 1. 2 0 0 0 1. 7 0 0 0 2. 5 0 0 0 2. 9 0 0 0 Find f (E); a new vector with the values f (s i ) s. Use f (E). dx to create the new vector with components f (s i ) x i. >> g = f ( E). dx g = 0. 7 2 0 0 1. 7 3 4 0 4. 3 7 5 0 1. 6 8 2 0
Riemann Sums In MatLab Finally, we add all these components together to get the Riemann sum. In MatLab, we add up the entries of a vector g with the command sum(g). >> RS = sum ( g ) RS = 8. 5 1 1 0 Without the comments, the MatLab session is not too long. >> f = @( x ) ( x. ˆ 2 ) ; >> P = [ 1 ; 1. 5 ; 2. 1 ; 2. 8 ; 3. 0 ] ; >> dx = d i f f (P) ; >> E = [ 1. 2 ; 1. 7 ; 2. 5 ; 2. 9 ] ; >> g = f ( E). dx ; >> RS = sum ( g ) ;
Riemann Sums In MatLab Homework 9 For the given function f, partition P and evaluation set E, do the following: use Matlab to find S(f, P, E) for the partition P and evaluation set E. Create a new word document for this homework. Do the document in single space. Do matlab fragments in bold font. The document starts with your name, MATH 4540, Section Number, Date and Homework number.
Riemann Sums In MatLab Homework 9 Continued Then answer the problems like this: 1 State Problem 1. insert into your doc the matlab commands you use to solve the problem. Do this in bold. before each line of matlab add explanatory comments so we can check to see you know what you re doing. 2 State Problem 2. same stuff
Riemann Sums In MatLab Homework 9 For the given function f, partition P and evaluation set E, do the following. 1 Find S(f, P, E) for the partition P and evaluation set E. 2 Find P. 3 Sketch a graph of this Riemann sum as an approximation to the area under the curve f. Do a nice graph with appropriate use of color.
Riemann Sums In MatLab Homework 9 Continued 9.1 Let f (t) = t 2 + 2 on the interval [1, 3] with P = {1, 1.5, 2.0, 2.5, 3.0} and E = {1.2, 1.8, 2.3, 2.8}. 9.2 Let f (t) = t 2 + 3 on the interval [1, 3] with P = {1, 1.6, 2.3, 2.8, 3.0} and E = {1.2, 1.9, 2.5, 2.85}. 9.3 Let f (t) = 3t 2 + 2t on the interval [1, 2] with P = {1, 1.2, 1.5, 1.8, 2.0} and E = {1.1, 1.3, 1.7, 1.9} 9.4 Let f (t) = 3t 2 + t on the interval [1, 4] with P = {1, 1.2, 1.5, 2.8, 4.0} and E = {1.1, 1.3, 2.3, 3.2}
Riemann Sums In MatLab Homework 9 Example Jim Peterson MATH 4540, Section Number today s date and HW Number, Problem 1: Let f (t) = sin(5t) on the interval [1, 3] with P = {1, 1.5, 2.0, 2.5, 3.0} and E = {1.2, 1.8, 2.3, 2.8}. Find S(f, P, E), % 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 >> P = [ 1 ; 1. 5 ; 2. 0 ; 2. 5 ; 3. 0 ] ; % add e x p l a n a t i o n h e r e >> E = [ 1. 2 ; 1. 8 ; 2. 3 ; 2. 8 ] ; % add e x p l a n a t i o n h e r e >> dx = d i f f (P) ; % add e x p l a n a t i o n h e r e >> g = f ( E). dx ; % add e x p l a n a t i o n h e r e >> RS = sum ( g ) RS = 0. 1 2 3 9
Graphing Riemann Sums 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, y1) to (x2, y2). 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.
Graphing Riemann Sums 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, y1) to (x2, y2). 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. The command plot([x(i) x(i)], [0 f(s(i))]); plots a vertical line that starts on the x axis at x i and ends at the function value f (s i ).
Graphing Riemann Sums The command plot([x(i+1) x(i+1)], [0 f(s(i))]); plots a vertical line that starts on the x axis at x i+1 and ends at the function value f (s i ).
Graphing Riemann Sums The command plot([x(i+1) x(i+1)], [0 f(s(i))]); plots a vertical line that starts on the x axis at x i+1 and ends at the function value f (s 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]);.
Graphing Riemann Sums 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 so we can 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.
Graphing Riemann Sums This generates the rectangle we see below: Figure: Simple Rectangle
Graphing Riemann Sums To show the Riemann sum approximation as rectangles, we use a for loop in MatLab To put this all together, for an example of four rectangles, we use the loop: h o l d on % s e t h o l d to 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 top ] ) ; p l o t ( [ E( i ) E ( i ) ], [ bottom top ], r ) ; p l o t ( [ P( i +1) P( i +1) ], [ bottom top ] ) ; p l o t ( [ P( i ) P( i +1) ], [ 0 0 ] ) ; end h o l d o f f % s e t h o l d o f f
Graphing Riemann Sums 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.
Graphing Riemann Sums 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. bottom = 0 ; top = f (E( i ) ) ; i f f (E( i ) ) < 0 top = 0 ; bottom = f ( E ( i ) ) ; end
Graphing Riemann Sums All together, we have h o l d on % s e t h o l d to on [ s i z e P,m] = s i z e (P) ; f o r i = 1 : s i z e P 1 % graph 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 top ] ) ; p l o t ( [ E( i ) E ( i ) ], [ bottom top ], r ) ; p l o t ( [ P( i +1) P( i +1) ], [ bottom top ] ) ; p l o t ( [ P( i ) P( i +1) ], [ 0 0 ] ) ; end h o l d o f f ; We also want to place the plot of f over these rectangles.
Graphing Riemann Sums h o l d on % s e t h o l d to on [ s i z e P,m] = s i z e (P) ; f o r i = 1 : s i z e P 1 % graph 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 top ] ) ; p l o t ( [ E( i ) E ( i ) ], [ bottom top ], r ) ; p l o t ( [ P( i +1) P( i +1) ], [ bottom top ] ) ; 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 ), 101) ; p l o t ( y, f ( y ) ) ; x l a b e l ( x a x i s ) ; y l a b e l ( y a x i s ) ; 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 ) ; h o l d o f f ;
Graphing Riemann Sums We generate this figure: Figure: Riemann Sum for f (x) = x 2 for Partition {1, 1.5, 2.1, 2.8, 3.0}
Graphing Riemann Sums Typing these commands into Octave or MatLab will generate a nice Riemann Sum picture. You can download the code RiemannSum.m to your laptop and run this code. f = @( x ) s i n ( x ) ; P = [ 3. 3 ; 2. 1 ; 1. 4 ;. 5 ; 0. 2 ; 1. 4 ; 2. 6 ; 2. 9 ; 3. 6 ] ; E = [ 2. 9 ; 1. 7 ; 0. 8 ; 0. 0 ; 1. 0 ; 2. 0 ; 2. 7 ; 3. 3 ] ; RS = RiemannSum ( f, P, E ) ;