Riemann surface
matlab

Example 1. As our first example, we will consider the case in which $c_k$ is always chosen as the right endpoint of the interval $\left[x_{k-1}, x_k\right]$. If we take a regular partition with $n$ intervals, then each interval has length $\triangle x=\frac{b-a}{n}$, and the $k^{\text {th }}$ endpoint is
$$x_k=a+k \Delta x$$
The Riemann sum becomes
$$R=\sum_{k=1}^n f(a+k \Delta x) \triangle x .$$
Suppose we would like to approximate the integral
$$\int_0^2 e^{-x^2} d x$$
with $n=4$. We have $\Delta x=\frac{2-0}{4}=.5$ and the values
\begin{aligned} & x_0=0 \ & x_1=.5 \ & x_2=1 \ & x_3=1.5 \ & x_4=2.0 . \end{aligned}

To four decimal places, the correct value is .8821 .
One interesting aspect of the Riemann sum is that the points $c_k$ 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=\frac{1}{2}<x_2=1$. In this case, a possible Riemann sum is
$$f(0) \frac{1}{2}+f(1) \frac{1}{2}$$Here $\Delta x_1=\triangle x_2=\frac{1}{2}$, and we have chosen $c_1$ as the left endpoint of the interval $\left[0, \frac{1}{2}\right]$ and $c_2$ as the right endpoint of the interval $\left[\frac{1}{2}, 1\right]$.

The Riemann sum is
$$R=\sum_{k=1}^4 f(0+.5 k) .5=.5\left(e^{-.5^2}+e^{-1^2}+e^{-1.5^2}+e^{-2^2}\right)=.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 $=$ rsum $1(\mathrm{f}, \mathrm{a}, \mathrm{b}, \mathrm{n})$
$\%$ RSUM1: Computes a Riemann Sum for the function $\mathrm{f}$ on
$\%$ the interval $[\mathrm{a}, \mathrm{b}]$ with a regular partition of $\mathrm{n}$ points.
\% The points on the intervals are chosen as the right endpoints.
value $=0 ;$
$\mathrm{dx}=(\mathrm{b}-\mathrm{a}) / \mathrm{n} ;$
for $\mathrm{k}=1: \mathrm{n}$
$\mathrm{c}=\mathrm{a}+\mathrm{k}^* \mathrm{dx} ;$
value $=\mathrm{value}+\mathrm{f}(\mathrm{c}) ;$
end
value $=\mathrm{dx}$; value;
function value $=\operatorname{rsum} 1(f, a, b, n)$
\%RSUM1: Computes a Riemann Sum for the function $f$ on
\% the interval $[\mathrm{a}, \mathrm{b}]$ with a regular partition of $\mathrm{n}$ points.
\%The points on the intervals are chosen as the right endpoints.
value $=0$
$\mathrm{dx}=(\mathrm{b}-\mathrm{a}) / \mathrm{n} ;$
for $\mathrm{k}=1: \mathrm{in}$
$\mathrm{c}=\mathrm{a}+\mathrm{k}^* \mathrm{dx}$
value $=$ value $+f(c)$
end
value $=\mathrm{dx}^*$ value;
We run this in MATLAB with the following lines in the Command Window.
$>>\mathrm{f}=\operatorname{inline}\left(\exp \left(-\mathrm{x}^{\wedge} 2\right)^{\prime}\right)$
$\mathrm{f}=$
Inline function:
$\mathrm{f}(\mathrm{x})=\exp \left(-\mathrm{x}^{\wedge} 2\right)$
$>>\mathrm{rsum} 1(\mathrm{f}, 0,2,4)$
ans $=$
0.6352
$>>\mathrm{rsum} 1(\mathrm{f}, 0,2,10)$
ans $=$
0.7837
$>>\mathrm{rsum} 1(\mathrm{f}, 0,2,100)$
ans $=$
0.8723
$>>\mathrm{rsum} 1(\mathrm{f}, 0,2,1000)$
ans $=$
0.8811
$>>\mathrm{f}=\operatorname{inline}\left(\exp \left(-\mathrm{x}^{\wedge} 2\right)^{\prime}\right)$
$\mathrm{f}=$
Inline function:
$\mathrm{f}(\mathrm{x})=\exp \left(-\mathrm{x}^{\wedge} 2\right)$
$>>\operatorname{rsum} 1(\mathrm{f}, 0,2,4)$
ans $=$
0.6352
$>>\operatorname{rsum} 1(\mathrm{f}, 0,2,10)$
ans $=$
0.7837
$>>\operatorname{rsum} 1(\mathrm{f}, 0,2,100)$
ans $=$
0.8723
$>>\operatorname{rsum} 1(\mathrm{f}, 0,2,1000)$
ans $=$
0.8811

Example 2. As our second example, we will consider the case in which $c_k$ is randomly selected on the interval $\left[x_{k-1}, x_k\right]$. In this case, we revise rsum1.m into rsum2.m.
function value $=\mathrm{rsum} 2(\mathrm{f}, \mathrm{a}, \mathrm{b}, \mathrm{n})$
\%RSUM2: Computes a Riemann Sum for the function $f$ on
\%the interval $[\mathrm{a}, \mathrm{b}]$ with a regular partition of $\mathrm{n}$ points.
\% The points on the intervals are chosen randomly.
value $=0$;
$\mathrm{dx}=(\mathrm{b}-\mathrm{a}) / \mathrm{n} ;$
for $\mathrm{k}=1: \mathrm{n}$
$\mathrm{c}=\mathrm{dx}$ rand $+\left(\mathrm{a}+(\mathrm{k}-1)^* \mathrm{dx}\right) ;$
value $=$ value $+\mathrm{f}(\mathrm{c})$;
end
value $=\mathrm{dx}^*$ value;
The only tricky line here is the one that defines $c_k$ as a random number on the interval $\left[x_{k-1}, x_k\right]$. In order to avoid a digression on the simulation of random variables, let’s simply accept that this line works. ${ }^1$ The implementation is as follows (assuming $f(x)=e^{-x^2}$ has already been defined as an inline function).
\begin{aligned} & >>\operatorname{rsum} 2(\mathrm{f}, 0,2,10) \ & \text { ans }= \ & 0.8665 \ & >>\operatorname{rsum} 2(\mathrm{f}, 0,2,100) \ & \text { ans }= \ & 0.8818 \ & >>\operatorname{rsum} 2(\mathrm{f}, 0,2,1000) \ & \text { ans }= \ & 0.8821 \end{aligned}
Notice that your values may vary slightly from these due to the random nature of the program. Nonetheless, you should find that Riemann sums based on random values are actually converging more rapidly to the correct solution than did Riemann sums based on right endpoints.

E-mail: help-assignment@gmail.com  微信:shuxuejun

help-assignment™是一个服务全球中国留学生的专业代写公司