\documentclass[11pt]{article}
\usepackage{times}
\pagestyle{empty}
\addtolength{\textwidth}{1.2in}
\addtolength{\textheight}{1.2in}
\addtolength{\oddsidemargin}{-.58in}
\addtolength{\evensidemargin}{-.58in}
\renewcommand{\baselinestretch}{1.0}
\parindent = 0cm
\parskip = .1cm
%\usepackage{graphicx}
%\usepackage{amsmath}
%\usepackage{amsfonts}
%\usepackage{amssymb}
%TCIDATA{OutputFilter=latex2.dll}
%TCIDATA{CSTFile=LaTeX article (bright).cst}
%TCIDATA{Created=Mon Aug 27 11:48:44 2001}
%TCIDATA{LastRevised=Fri Sep 21 10:59:46 2001}
%TCIDATA{}
%TCIDATA{}
%TCIDATA{Language=American English}
\newtheorem{theorem}{Theorem}
\newtheorem{acknowledgement}[theorem]{Acknowledgement}
\newtheorem{algorithm}[theorem]{Algorithm}
\newtheorem{axiom}[theorem]{Axiom}
\newtheorem{case}[theorem]{Case}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{conclusion}[theorem]{Conclusion}
\newtheorem{condition}[theorem]{Condition}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{criterion}[theorem]{Criterion}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{exercise}[theorem]{Exercise}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{notation}[theorem]{Notation}
\newtheorem{problem}[theorem]{Problem}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{remark}[theorem]{Remark}
\newtheorem{solution}[theorem]{Solution}
\newtheorem{summary}[theorem]{Summary}
\newenvironment{proof}[1][Proof]{\textbf{#1.} }{\ \rule{0.5em}{0.5em}}
\begin{document}
\begin{center}
{\Large Area Below the Graph of a Function\footnote{\copyright 2004 Winfried Just, Department Mathematics,
Ohio University. All rights reserved.}\\
MATLAB exercise for MATH263B}
\end{center}
\bigskip
While working through this MATLAB assignment, keep a notepad handy to write down the answers to the problems listed in the text.
These answers may be collected and graded.
In class you learned how to approximate areas below the graph of a function $f(x)$ by sums of areas of rectangles. In this exercise
you will learn \emph{why} for some functions $f(x)$ the area bounded by its graph can be approximated as closely as one desires.
Along the way you will learn how to define functions in \textsc{MatLab} and how to graph them.
Suppose we want to find an approximate value of the area $A$ below the graph of the function $f(x) = \cos x$ between $a = 0$ and $b = \frac{\pi}{2}$. In class you learned that you can find an approximate value by partitioning the interval
$[0, \pi]$ into $n$ consecutive subintervals $[x_{i-1}, x_i]$, taking a sample point $x^*_i$ from each subinterval,
and calculating $\sum_{i=1}^n (\cos x_i^*)\Delta x_i$, which is the sum of the areas of the rectangles $A_i$, where the height of the rectangle $A_i$ is
$\cos x^*_i$ and its base is the segment of the $x$-axis between $x_{i-1}$ and $x_i$.
One can look at the sum of the areas of these rectangles in a slightly different way: Let $g(x)$ be the function defined on $[0, \frac{\pi}{2}]$ that takes the value $\cos x^*_i$ throughout the interval $[x_{i-1}, x_i]$ for each $i = 1, 2, \ldots , n]$. Then the sum of the area of
the rectangles $A_i$ is equal to the area below the graph of $g(x)$ between $a = 0$ and $b = \frac{\pi}{2}$.
The meaning of the previous paragraph will become clearer if we graph the functions $f(x)$ and $g(x)$ on the same plot. For that, we first need to define these functions in \textsc{MatLab}.
There are several ways to define a function in \textsc{MatLab}. The easiest way is to create a so-called
\emph{inline function.} The word ``inline'' indicates that this type of function is defined right at the prompt in the command window rather than in a separate file. In order to define $f(x)$ as an inline function, enter
\smallskip
\noindent
\verb$>> f = inline('cos(x)')$
\smallskip
Make sure that you do not forget the single quotation marks, or you will get an error message.
The function \verb$f$ that we just defined behaves pretty much as expected, but not quite. Enter
\smallskip
\noindent
\verb$>> f(0)$
\noindent
\verb$>> f(pi/2)$
\smallskip
While \textsc{MatLab} tells you correctly that $f(0) = 1$, it does not give you the correct value of zero for
$f(\frac{\pi}{2})$, but only a number that is very close to zero. The reason is that all calculations of \textsc{MatLab} are done with finite precision and little rounding errors creep in.
\begin{problem}
Note that \textsc{MatLab} gives you the supposed value of \verb$ f(pi/2)$ in scientific notation. Write down this value both in the way \textsc{MatLab} gave it to you and in decimal notation.
\end{problem}
Let us try to graph the function $f(x)$ on the interval $[0,\frac{\pi}{2}]$. For this, you can use the \verb$ezplot$ command. Enter
\smallskip
\noindent
\verb$>> ezplot(f, [0, pi/2])$
\smallskip
The graph of the function appears in a separate window named ``Figure No.~1.'' Unless told otherwise, \textsc{MatLab} makes a new window for each plot. In this assignment, we will want to look at several graphs simultaneously. This can be accomplished by using the \verb$subplot$
command. Enter:
\smallskip
\noindent
\verb$>> figure(2)$
\noindent
\verb$>> subplot(2, 3, 1)$
\noindent
\verb$>> ezplot(f, [0, pi/2])$
\smallskip
These commands tell \textsc{MatLab} that Figure No.~2 will have two rows of three subplots each, and that the function \verb$f$ should be graphed in the first of these subplots.
Now let us try to define and plot the function $g(x)$. Let us take right endpoints as the sample points $x_i^*$. Then the value of $g(x)$ will be $\cos (\frac{\pi}{2n})$ for all $x$ in subinterval $[0, \frac{\pi}{2n}]$, will be $\cos (2\frac{\pi}{2n})$ for all $x$ in subinterval
$[\frac{\pi}{2n}, 2\frac{\pi}{2n}]$, and so on. In general, for all $x$ in the subinterval
$[(i-1)\frac{\pi}{2n}, i\frac{\pi}{2n}]$ we will have $g(x) = \cos (i\frac{\pi}{2n})$. We can define this function by using the \emph{ceiling} function, which rounds the argument \emph{up} to the nearest integer. \textsc{MatLab}'s command for the ceiling function is \verb$ceil$. Try:
\smallskip
\noindent
\verb$>> ceil(3.2)$
\noindent
\verb$>> ceil(-4.6)$
\smallskip
Of course, the endpoints of the subintervals in our partition will not be integers; we need to use a clever trick to define the function $g(x)$. For $n = 5$ subintervals in the partition, we would define:
\smallskip
\noindent
\verb$>> g5 = inline('cos(ceil(5*2*x/pi)*pi/(5*2))')$
\smallskip
Note that \textsc{MatLab} allows function and variable names that contain numbers, and here it is convenient to make use of this. To see that this function behaves as expected, calculate
\smallskip
\noindent
\verb$>> g5(0.15)$
\noindent
\verb$>> g5(0.3)$
\noindent
\verb$>> g5(0.45)$
\smallskip
\begin{problem}
Which answers does \textsc{MatLab} give you, and how do you explain these answers?
\end{problem}
Now let us visualize the situation by plotting \verb$f$ and \verb$g5$ on the same graph. To make these two functions appear in the same graph, enter
\smallskip
\noindent
\verb$>> hold on$
\noindent
\verb$>> ezplot(g5, [0,pi/2])$
\smallskip
Let us look at the resulting graph in Figure No.~2. The sum of the area of the rectangles
$\sum_{i=1}^5 \cos(x^*_i) \Delta x_i$ is the area below the graph of \verb$g5$ between $0$ and $\frac{\pi}{2}$. It gives an approximation (an underestimate) of the area below the graph of \verb$f$ between $0$ and $\frac{\pi}{2}$.
In order to improve our estimate, we may want to increase the number of intervals in the partition. Let us define appropriate functions for $n = 10$ and $n = 20$.
\smallskip
\noindent
\verb$>> g10 = inline('cos(ceil(10*2*x/pi)*pi/(10*2))')$
\noindent
\verb$>> g20 = inline('cos(ceil(20*2*x/pi)*pi/(20*2))')$
\smallskip
Let us plot each of these functions together with \verb$f$.
\smallskip
\noindent
\verb$>> subplot(2, 3, 2)$
\noindent
\verb$>> ezplot(f, [0, pi/2])$
\noindent
\verb$>> hold on$
\noindent
\verb$>> ezplot(g10, [0, pi/2])$
\smallskip
Look at the figure. It does seem that the area below the graph of \verb$g10$ is a better approximation to the area below the graph of \verb$f$ than the area below the graph of \verb$g5$; doesn't it? \verb$g20$ works even better:
\smallskip
\noindent
\verb$>> subplot(2, 3, 3)$
\noindent
\verb$>> ezplot(f, [0, pi/2])$
\noindent
\verb$>> hold on$
\noindent
\verb$>> ezplot(g20, [0, pi/2])$
\smallskip
While visual inspection gives us some intuition that the approximations get better and better, how can we convince ourselves that this is really the case? Look at the graph again and note one thing: The difference between the area below the graph of $f$ and the area below the graph of $g$ should be the same as the area below the graph of the difference $f - g$. Let us compare these differences by plotting them:
\smallskip
\noindent
\verb$>> h5 = inline('cos(x) - cos(ceil(5*2*x/pi)*pi/(5*2))')$
\noindent
\verb$>> h10 = inline('cos(x) - cos(ceil(10*2*x/pi)*pi/(10*2))')$
\noindent
\verb$>> h20 = inline('cos(x) - cos(ceil(20*2*x/pi)*pi/(20*2))')$
\noindent
\verb$>> subplot(2, 3, 4)$
\noindent
\verb$>> ezplot(h5, [0, pi/2])$
\smallskip
Why does the graph of \verb$h5$ not quite look as you might have expected? Note that \verb$ezplot$ automatically chooses an appropriate scale for the $y$-axis, and the one for the new subplot is different from the one for the subplot right above it. You can tell \textsc{MatLab} to use the same scale as in the previous graphs. First let us find out exactly what it is:
\smallskip
\noindent
\verb$>> subplot(2, 3, 1)$
\noindent
\verb$>> axis$
\smallskip
The answer tells you that on the first subplot, the numbers on the $x$-axis range from
$0$ to $1.5708$ (which is equal to $\frac{\pi}{2}$), and the numbers on the $y$-axis range from
$-0.1189$ to $1.1189$.
We can tell \textsc{MatLab} to use the same ranges in the fourth subplot by entering:
\smallskip
\noindent
\verb$>> subplot(2, 3, 4)$
\noindent
\verb$>> ezplot(h5, [0, pi/2, -0.1189, 1.1189]$
\smallskip
What can be seen from the figure is that the difference between \verb$f$ and \verb$g5$ is a nonnegative function that takes values not greater than $0.32$. The area below the graph of such a function between $0$ and $\frac{\pi}{2}$ will be no greater than $0.32\frac{\pi}{2} =
0.5027$, and thus the error of approximating the area below the graph of \verb$f$ by the area below the graph of \verb$g5$ does not exceed $0.5027$.
Now try
\smallskip
\noindent
\verb$>> subplot(2, 3, 5)$
\noindent
\verb$>> ezplot(h10, [0, pi/2, -0.1189, 1.1189])$
\smallskip
\begin{problem}
What estimate of the maximum error of approximating the area below the graph of \verb$f$ by the area below the graph of \verb$g10$ between $0$ and $\frac{\pi}{2}$ can we derive from this graph? What estimate of the maximum error of approximating the area below the graph of \verb$f$ by the area below the graph of \verb$g20$ between $0$ and $\frac{\pi}{2}$ can we derive in a similar manner?
\end{problem}
\end{document}