Professional Documents
Culture Documents
James K. Peterson
Outline
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
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
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
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
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