Menu

[r5337]: / trunk / py4science / workbook / trapezoid.lyx  Maximize  Restore  History

Download this file

234 lines (176 with data), 4.6 kB

#LyX 1.4.3 created this file. For more info see http://www.lyx.org/
\lyxformat 245
\begin_document
\begin_header
\textclass amsbook
\begin_preamble
\input{preamble.tex}
\end_preamble
\language english
\inputencoding auto
\fontscheme default
\graphics default
\paperfontsize default
\spacing single
\papersize default
\use_geometry true
\use_amsmath 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\leftmargin 1.3in
\topmargin 1in
\rightmargin 1.3in
\bottommargin 1in
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 2
\paperpagestyle default
\tracking_changes false
\output_changes false
\end_header

\begin_body

\begin_layout Section
Trapezoidal rule
\end_layout

\begin_layout Standard
In this exercise, you are tasked with implementing the simple trapezoid
 rule formula for numerical integration.
 If we want to compute the definite integral 
\begin_inset Formula \begin{equation}
\int_{a}^{b}f(x)dx\end{equation}

\end_inset

we can partition the integration interval 
\begin_inset Formula $[a,b]$
\end_inset

 into smaller subintervals, and approximate the area under the curve for
 each subinterval by the area of the trapezoid created by linearly interpolating
 between the two function values at each end of the subinterval.
 This is graphically illustrated in Figure\InsetSpace ~

\begin_inset LatexCommand \ref{fig:trapezoid}

\end_inset

, where the blue line represents the function 
\begin_inset Formula $f(x)$
\end_inset

 and the red line represents the successive linear segments.
\end_layout

\begin_layout Standard
The area under 
\begin_inset Formula $f(x)$
\end_inset

 (the value of the definite integral) can thus be approximated as the sum
 of the areas of all these trapezoids.
 If we denote by 
\begin_inset Formula $x_{i}$
\end_inset

 (
\begin_inset Formula $i=0,\ldots,n,$
\end_inset

 with 
\begin_inset Formula $x_{0}=a$
\end_inset

 and 
\begin_inset Formula $x_{n}=b$
\end_inset

) the abscissas where the function is sampled, then 
\begin_inset Formula \begin{equation}
\int_{a}^{b}f(x)dx\approx\frac{1}{2}\sum_{i=1}^{n}\left(x_{i}-x_{i-1}\right)\left(f(x_{i})+f(x_{i+1})\right).\label{eq:trapzf}\end{equation}

\end_inset

The common case of using equally spaced abscissas with spacing 
\begin_inset Formula $h=(b-a)/n$
\end_inset

 reads simply 
\begin_inset Formula \begin{equation}
\int_{a}^{b}f(x)dx\approx\frac{h}{2}\sum_{i=1}^{n}\left(f(x_{i})+f(x_{i+1})\right).\label{eq:trapzf2}\end{equation}

\end_inset

One frequently receives the function values already precomputed, 
\begin_inset Formula $y_{i}=f(x_{i}),$
\end_inset

 so equation\InsetSpace ~
(
\begin_inset LatexCommand \ref{eq:trapzf}

\end_inset

) becomes 
\begin_inset Formula \begin{equation}
\int_{a}^{b}f(x)dx\approx\frac{1}{2}\sum_{i=1}^{n}\left(x_{i}-x_{i-1}\right)\left(y_{i}+y_{i-1}\right).\label{eq:trapz}\end{equation}

\end_inset


\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Standard
\align center
\begin_inset Graphics
	filename fig/Composite_trapezoidal_rule_illustration.png
	lyxscale 10
	width 3in

\end_inset


\end_layout

\begin_layout Caption
\begin_inset LatexCommand \label{fig:trapezoid}

\end_inset

Illustration of the composite trapezoidal rule with a non-uniform grid (Image
 credit: Wikipedia).
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Listing\InsetSpace ~

\begin_inset LatexCommand \ref{code:trapezoid_skel}

\end_inset

 contains a skeleton for this problem, written in the form of two incomplete
 functions and a set of automatic tests (in the form of 
\emph on
unit tests
\emph default
, as described in the introduction).
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Standard


\backslash
lstinputlisting[label=code:trapezoid_skel,caption={IGNORED}]{skel/trapezoid_skel.
py}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
In this exercise, you'll need to write two functions, 
\family typewriter
trapz
\family default
 and 
\family typewriter
trapzf
\family default
.
 
\family typewriter
trapz
\family default
 applies the trapezoid formula to pre-computed values, implementing equation\InsetSpace ~
(
\begin_inset LatexCommand \ref{eq:trapz}

\end_inset

), while 
\family typewriter
trapzf
\family default
 takes a function 
\begin_inset Formula $f$
\end_inset

 as input, as well as the total number of samples to evaluate, and computes
 eq.\InsetSpace ~
(
\begin_inset LatexCommand \ref{eq:trapzf2}

\end_inset

).
\end_layout

\end_body
\end_document
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.