Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Riemann Sums in MATLAB, Schemes and Mind Maps of Matlab skills

m. function value=rsum1(f,a,b,n) %RSUM1: Computes a Riemann Sum for the function f on %the interval [a,b] with a regular partition of n points. %The points on ...

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/27/2022

gustavott
gustavott 🇬🇧

3.9

(14)

254 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Riemann Sums in MATLAB
Definition. Let f(x) be a function on an interval [a, b], and suppose this interval is parti-
tioned by the values a=x0< x1< ... < xn1< xn=b. Any sum of the form
R=
n
X
k=1
f(ck)xk,
where xk=xkxk1and ck[xk1, xk] is referred to as a Riemann sum of f.
If we let Pdenote our choice of partition (the choice of values x0,x1, ..., xn), and we let
kPk:= max(x1,x2, ..., xn)
denote the norm of this partition, then we say fis Riemann integrable if
lim
kPk→0
n
X
k=1
f(ck)xk
exists. Following Leibniz, our notation for this limit is
Zb
a
f(x)dx = lim
kPk→0
n
X
k=1
f(ck)xk.
Example 1. As our first example, we will consider the case in which ckis always chosen as
the right endpoint of the interval [xk1, xk]. If we take a regular partition with nintervals,
then each interval has length x=ba
n, and the kth endpoint is
xk=a+kx.
The Riemann sum becomes
R=
n
X
k=1
f(a+kx)x.
Suppose we would like to approximate the integral
Z2
0
ex2dx
with n= 4. We have x=20
4=.5 and the values
x0= 0
x1=.5
x2= 1
x3= 1.5
x4= 2.0.
1
pf3
pf4

Partial preview of the text

Download Riemann Sums in MATLAB and more Schemes and Mind Maps Matlab skills in PDF only on Docsity!

Riemann Sums in MATLAB

Definition. Let f (x) be a function on an interval [a, b], and suppose this interval is parti- tioned by the values a = x 0 < x 1 < ... < xn− 1 < xn = b. Any sum of the form

R =

∑^ n

k=

f (ck)△xk,

where △xk = xk − xk− 1 and ck ∈ [xk− 1 , xk] is referred to as a Riemann sum of f. If we let P denote our choice of partition (the choice of values x 0 , x 1 , ..., xn), and we let

‖P ‖ := max(△x 1 , △x 2 , ..., △xn)

denote the norm of this partition, then we say f is Riemann integrable if

lim ‖P ‖→ 0

∑^ n

k=

f (ck)△xk

exists. Following Leibniz, our notation for this limit is

∫ (^) b

a

f (x)dx = lim ‖P ‖→ 0

∑^ n

k=

f (ck)△xk.

Example 1. As our first example, we will consider the case in which ck is always chosen as the right endpoint of the interval [xk− 1 , xk]. If we take a regular partition with n intervals,

then each interval has length △x = b− na , and the kth endpoint is

xk = a + k△x.

The Riemann sum becomes

R =

∑^ n

k=

f (a + k△x)△x.

Suppose we would like to approximate the integral

∫ (^2)

0

e−x

2 dx

with n = 4. We have △x = 2 − 4 0 = .5 and the values

x 0 = 0 x 1 =. 5 x 2 = 1 x 3 = 1. 5 x 4 = 2. 0.

The Riemann sum is

R =

∑^4

k=

f (0 +. 5 k).5 = .5(e−.^5

2

  • e−^1

2

  • e−^1.^5

2

  • e−^2

2 ) =. 6352.

More generally, we can write a MATLAB function M-file that carries out this calculation for any function f (defined as an inline function), endpoints a and b and regular partition with n points. See rsum1.m.

function value=rsum1(f,a,b,n) %RSUM1: Computes a Riemann Sum for the function f on %the interval [a,b] with a regular partition of n points. %The points on the intervals are chosen as the right endpoints. value = 0; dx = (b-a)/n; for k=1:n c = a+kdx; value = value + f(c); end value = dxvalue;

We run this in MATLAB with the following lines in the Command Window.

f=inline(’exp(-xˆ2)’) f = Inline function: f(x) = exp(-xˆ2) rsum1(f,0,2,4) ans =

rsum1(f,0,2,10) ans =

rsum1(f,0,2,100) ans =

rsum1(f,0,2,1000) ans =

To four decimal places, the correct value is .8821. △ One interesting aspect of the Riemann sum is that the points ck need not be chosen in the same place on each interval. That is, suppose we partition the interval [0, 1] with 0 = x 0 < x 1 = 12 < x 2 = 1. In this case, a possible Riemann sum is

f (0)

  • f (1)

Assignments

  1. Alter the M-file rsum1.m so that it computes Riemann sums of the given function by taking the values ck as the left endpoints of each interval. Use your M-file to estimate ∫ (^2)

0

e−x

2 dx

for regular partitions with n = 10, 100 , 1000.

  1. Alter the M-file rsum1.m so that it computes Riemann sums of the given function by taking the values ck as the midpoints of each interval. Use your M-file to estimate ∫ (^2)

0

e−x

2 dx

for regular partitions with n = 10, 100 , 1000.