\documentclass[12pt]{article}
\usepackage{times}
%%%\pagestyle{empty}
\pagestyle{myheadings}%%%
\markright{MATH266A \hfill Finding limits in \textsc{MatLab}\hfill}
\thispagestyle{empty}
\addtolength{\textwidth}{1.2in}
\addtolength{\textheight}{1.2in}
\addtolength{\oddsidemargin}{-.58in}
\addtolength{\evensidemargin}{-.58in}
\renewcommand{\baselinestretch}{1.0}
\parindent = 0cm
\parskip = .1cm
\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 Finding limits in \textsc{MatLab}
\footnote{Copyright \copyright 2004 Winfried Just, Department of Mathematics, Ohio University. All rights reserved.}}
\end{center}
\bigskip
If this \textsc{MatLab} exercise is being counted in your grade,
then please record your answers to all the questions on a separate sheet
for submission.
%%
%While working through this exercise, please record your answers to all the questions on a separate sheet for possible submission with your next test.
%
In this exercise, you will learn how to use \textsc{MatLab} to find limits of functions.
All the limits we will calculate in this exercise will be of the form $\lim_{x \rightarrow c} f(x)$ (where $c$ may be a number, a ``one-sided number,'' or an infinity symbol). In order to work with these expressions, you want to declare $x$ a \emph{symbolic variable:}
\smallskip
\noindent
\verb$>> syms x$
\smallskip
Using a symbolic variable allows us to construct symbolic expressions for functions. Here is one
that defines $f(x) = \cos x$.
\smallskip
\noindent
\verb$>> f = cos(x)$
\smallskip
Now we can evaluate $\lim_{x \rightarrow \pi} \cos x$ by entering:
\smallskip
\noindent
\verb$>> limit(f, pi)$
\smallskip
The result is as expected. Curiously enough, \textsc{MatLab}'s way of finding limits eliminates some of the surprises that its way of calculationg functions springs on us.
Compare:
\smallskip
\noindent
\verb$>> limit(f, pi/2)$
\noindent
\verb$>> cos(pi/2)$
\smallskip
While the former gives the exact result for $\lim_{x \rightarrow \frac{\pi}{2}} \cos x$, the latter gives $\cos(\frac{\pi}{2})$ with a slight error.
\medskip
Instead of defining functions symbolically, you also can enter directly their formulas into
the \verb$limit$ command. For example, $\lim_{x \rightarrow 1} e^x$ can be calculated like this:
\smallskip
\noindent
\verb$>> limit(exp(x), 1)$
\smallskip
Curiously enough, \textsc{MatLab} tells you that the answer to this one is the function value
of the exponential function $e^x$ at $1$, but it does not give you a numerical answer. You can force the program to give you a numerical value by entering one of the following two commands:
\smallskip
\noindent
\verb$>> double(ans)$
\smallskip
or
\smallskip
\noindent
\verb$>> exp(1)$
\smallskip
The first of these commands tells \textsc{MatLab} to convert the answer to the previous question into a numerical (\emph{double} precision) format; the second one simply instructs
\textsc{MatLab} to compute $e^1$.
\begin{problem}
How do you force \textsc{MatLab} to show the number $e$ with a precision of 15 places after the decimal point? Please record the command you enter and the actual value of $e$ with this precision. \emph{Hint:} If you have forgotten how to do this, you may want to consult the handout ``Entering formulas in \textsc{MatLab.}
\end{problem}
Now let us see what happens if we ask \textsc{MatLab} to calculate some nonexisting limits. Enter:
\smallskip
\noindent
\verb$>> limit(1/x^2, 0)$
\noindent
\verb$>> limit(1/x^3, 0)$
\smallskip
While in the first case \textsc{MatLab} tells you that $\lim_{x \rightarrow 0} \frac{1}{x^2} =
\infty$, in the second case the answer is \verb$NaN$, which stands for ``Not a Number.''
As you know, in the second case we have $\lim_{x \rightarrow 0^+} \frac{1}{x^3} =
\infty$ and $\lim_{x \rightarrow 0^-} \frac{1}{x^3} =
-\infty$. Here is how we can get \textsc{MatLab} to calculate these one-sided limits.
\smallskip
\noindent
\verb$>> limit(1/x^3, x, 0, 'right')$
\noindent
\verb$>> limit(1/x^3, x, 0, 'left')$
\smallskip
The \verb$x$ in the second argument of the \verb$limit$ command tells \textsc{MatLab} that the limit is taken as the variable $x$ approaches $0$. In theory, this argument is optional and it should be possible to leave it out (we have been doing so up to this point), but for some reason my version of \textsc{MatLab} bitterly complains if I do leave it out. You may want to give it a try and see how your machine reacts to leaving out the argument.
In a similar way, we can investigate what happens to the function $f(x) = \frac{|x|}{x}$ as $x$ approaches zero.
\smallskip
\noindent
\verb$>> limit(abs(x)/x, 0)$
\noindent
\verb$>> limit(abs(x)/x, x, 0, 'right')$
\noindent
\verb$>> limit(abs(x)/x, x, 0, 'left')$
\begin{problem}
Use \textsc{MatLab} to investigate the existence of
$\lim_{x \rightarrow 1} \frac{x^2 -1}{|x-1|}$,
$\lim_{x \rightarrow 1^+} \frac{x^2 -1}{|x-1|}$, and
$\lim_{x \rightarrow 1^-} \frac{x^2 -1}{|x-1|}$. Indicate the commands you enter and
\textsc{MatLab}'s answers, as well as the interpretations of these answers in your own words.
\end{problem}
\textsc{MatLab} can also be instructed to calculate limits at infinity. For example, the following commands give $\lim_{x \rightarrow \infty} \frac{4x^3 - 2x + 6}{-x^3 + x^2 - 1}$ and
$\lim_{x \rightarrow -\infty} e^x$.
\smallskip
\noindent
\verb$>> limit((4*x^3 - 2*x + 6)/(-x^3 + x^2 - 1), inf)$
\noindent
\verb$>> limit(exp(x), -inf)$
\smallskip
\begin{problem}
Try to find $\lim_{x \rightarrow \infty} \sin x$ with \textsc{MatLab}. What command do you type; which answer does \textsc{MatLab} give you, and how do you interpret this answer?
\end{problem}
Sometimes \textsc{MatLab} gives you misleading answers. For example, the expression
$\lim_{x \rightarrow 0} \sqrt{x}$ is meaningless from our point of view, since $\sqrt{x}$ is not a real number for $x < 0$; only $\lim_{x \rightarrow 0^+} \sqrt{x}$ makes sense. However,
if you enter
\smallskip
\noindent
\verb$>> limit(sqrt(x), 0)$
\smallskip
\textsc{MatLab} will give you the answer $0$. What is going on here? While for negative arguments $x$ the square root of $x$ is not a real number, it is defined as a so-called
\emph{complex number.} Let us calculate a few square roots of negative numbers in \textsc{MatLab:}
\eject
\noindent
\verb$>> sqrt(-1)$
\noindent
\verb$>> sqrt(-0.1)$
\noindent
\verb$>> sqrt(-0.01)$
\noindent
\verb$>> sqrt(-0.0001)$
\smallskip
\textsc{MatLab} gives you answers of the form $a + bi$, where $a, b$ are real numbers and $i$ is the symbol for the \emph{imaginary number} $\sqrt{-1}$. In the above examples, $a$ is always zero. The number $a$ is called the \emph{real part} of the complex number $0 + bi$, and $b$ is the \emph{imaginary part.} If you want to know a little more about complex numbers, you may wish to read Section 1.1.6 of your textbook. However, even if you know only as much about complex numbers as I have just told you, it seems clear that as $x$ approaches $0$ from the left, the complex numbers $\sqrt{x}$ get closer and closer to zero. Thus from
\textsc{MatLab}'s point of view, $\lim_{x \rightarrow 0^-} \sqrt{x} = 0$, and thus
\textsc{MatLab} tells you (mistakenly from our point of view) that
$\lim_{x \rightarrow 0} \sqrt{x} = 0$.
Let us look at one more example of this kind.
\smallskip
\noindent
\verb$>> limit(log(x), x, 0, 'left')$
\smallskip
Now \textsc{MatLab} is telling you that $\lim_{x \rightarrow 0^-} \ln x = - \infty$; which does not make sense from our point of view, since $\ln x$ is not a real number for $x < 0$. As you may have guessed, \textsc{MatLab} calculates $\ln x$ as a complex number for $x < 0$, and looks at the behavior of these numbers as $x$ approaches $0$ from the left.
Let us examine what pattern \textsc{MatLab} ``sees'' by looking at these numbers:
\smallskip
\noindent
\verb$>> log(-0.1)$
\noindent
\verb$>> log(-0.0001)$
\noindent
\verb$>> log(-0.00000001)$
\smallskip
\begin{problem}
What appears to happen to the real parts of $\ln x$ as $x$ approaches zero from the left?
What appears to happen to the imaginary parts of $\ln x$ as $x$ approaches zero from the left?
How would you interpret, in your own words, the meaning of \textsc{MatLab}'s answer
\verb$-inf$ to your question about $\lim_{x \rightarrow 0^-} \ln x$?
\end{problem}
\end{document}