\documentclass[11pt]{article}
\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 Integrating functions with \textsc{MatLab}\footnote{\copyright 2004 Winfried Just, Department Mathematics,
Ohio University. All rights reserved.}\\
\textsc{MatLab} exercise for MATH263B}
\end{center}
\bigskip
While working through this \textsc{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 this exercise, you will learn how you can use \textsc{MatLab} to integrate functions; both
symbolically and numerically. Along the way, you will learn about some of the pitfalls of using \textsc{MatLab} for these purposes.
In order to perform symbolic integration on functions, you need
to declare the variables that occur in these functions as so-called \emph{symbolic variables.}
Let us declare two variables $a$ and $x$ this way.
\smallskip
\noindent
\verb$>> syms x a$
\smallskip
Now let us ask \textsc{MatLab} to find the indefinite integral $\int x^2 \, dx$
\smallskip
\noindent
\verb$>> int(x^2)$
\smallskip
The answer is almost as expected, but not quite: Note that \textsc{MatLab} does not add the constant of integration to the result (of course \textsc{MatLab} doesn't have to take any tests
in my MATH263B class).
Now let us try to compute the integral $\int \frac{a}{x} \, dx$, where $a$ is a constant. Enter
\smallskip
\noindent
\verb$>> int(a/x)$
\smallskip
\begin{problem}
How does \textsc{MatLab}'s answer differ from the correct answer that you learned in class?
\emph{Hint:} \textsc{MatLab} uses \verb$log(x)$ for $\ln x$; this is \emph{not} the problem.
\end{problem}
In the above example, although the $dx$-notation is not part of the \textsc{MatLab} command for symbolic integration, the program guessed (correctly) that $x$ was supposed to be the variable
of integration and $a$ was supposed to be a constant.
\begin{problem}
Which command should you enter in order to calculate $\int \frac{a}{x} \, da$, where $x$ is
treated as a constant? \emph{Hint:} Enter
\verb$>> help int$
to find out more about the syntax of the command \verb$int.$
\end{problem}
Now let us use \textsc{MatLab} to find $\int \frac{x}{(x+3)(x-1)(x^2+1)} \, dx$.
Enter
\smallskip
\noindent
\verb$>> int(x/(x+3)/(x-1)/(x^2+1))$
\smallskip
\textsc{MatLab}'s answer indicates that the program performed a partial fraction decomposition.
You can easily find out what the decomposition is by letting the program symbolically differentiate the answer.
\smallskip
\noindent
\verb$>> diff(ans)$
\smallskip
\begin{problem}
What is the partial fraction decomposition of $\frac{x}{(x+3)(x-1)(x^2+1)}$?
\end{problem}
You can recover the original form of the integrand by entering
\smallskip
\noindent
\verb$>> simplify(ans)$
\smallskip
Theoretically, if $f(x)$ is the simplest formula for any function that can be symbolically integrated, then entering the sequence of commands
\smallskip
\noindent
\verb$>> int(f(x))$
\noindent
\verb$>> diff(ans)$
\noindent
\verb$>> simplify(ans)$
\smallskip
\noindent
should give you the original formula for $f(x)$.
However, this works not always as expected. Enter
\smallskip
\noindent
\verb$>> int(sin(x)^3*cos(x)^4)$
\noindent
\verb$>> diff(ans)$
\noindent
\verb$>> simplify(ans)$
\smallskip
\begin{problem}
Which simplified answer does \textsc{MatLab} give you? How do you explain the discrepancy
from what you expected? \emph{Hint:} Recall that $\sin^2 x = 1 - \cos^2 x$.
\end{problem}
Some definite integrals just cannot be expressed in terms of the elementary functions that you
have learned about in class. If you ask \textsc{MatLab} to integrate such functions, you may
get answers that contain symbols for exotic functions that are defined in terms of integrals.
For example, when you enter
\smallskip
\noindent
\verb$>> int(exp(x^2))$
\smallskip
\noindent
then your answer is expressed in terms of the so-called \emph{error function} \verb$erf.$
At other times, \textsc{MatLab} will give you an incomplete answer that contains another
integral.
\begin{problem}
Try to symbolically integrate $\sqrt{1 + x^3 - x^5}$. Write down \textsc{MatLab}'s answer in
ordinary mathematical notation. \emph{Hint:} \textsc{MatLab}'s command for $\sqrt{x}$ is
\verb$sqrt(x)$.
\end{problem}
\textsc{MatLab} has a built-in command \verb$quad$ for approximate integration using Simpson's Rule.
As you learned in class, $\int_0^\pi \sin x \, dx = 2$. Let us see what we get from approximate integration:
\smallskip
\noindent
\verb$>> quad('sin(x)', 0, pi)$
\smallskip
Note that the expression for the function now needs to be enclosed in quotation marks. The reason is that for approximate integration,
\textsc{MatLab} needs to actually evaluate the function for may arguments rather than treating its formula
as a symbolic expression. In fact, you can find out how many function evaluations \textsc{MatLab} performed
in order to compute the result by entering
\smallskip
\noindent
\verb$>> [q, n] = quad('sin(x)', 0, pi)$
\smallskip
The answer has two components: \verb$q$ is the approximation of the integral, and \verb$n$ is the number of
function evaluations.
\textsc{MatLab}'s answer seems to be the exact value of the integral, but this is not quite true. Enter
\smallskip
\noindent
\verb$>> format long$
\noindent
\verb$>> [q, n] = quad('sin(x)', 0, pi)$
\smallskip
Now you can see that the result is accurate only up to eight decimal places
(in fact, \textsc{MatLab}'s answer
is \emph{guaranteed} to be accurate only up to six decimal places, but in this particular example we get a somewhat better-than-hoped-for accuracy). You can force \textsc{MatLab} to give you answers
with greater precision, but this may slow down the calculation. In order to make sure that the answer is correct
up to ten decimal places, enter
\smallskip
\noindent
\verb$>> [q, n] = quad('sin(x)', 0, pi, 1.e-10)$
\smallskip
\begin{problem}
What is the error this time? How does it compare to the maximum allowed error tolerance of
$10^{-10}$?
What happened to the number of function evaluations?
\end{problem}
When using the \verb$quad$ command, some extra care needs to be taken when entering the formula of the function. As mentioned above, the formula for $f(x)$ is not treated as a symbolic expression. Instead, it is treated as a
\emph{vector} of numbers. \textsc{MatLab} uses slightly different notations for multiplication, division, and exponentiation for individual numbers and for numbers that are part of a vector. In the latter case, the symbols
\verb$.*, ./, .^$ are used. For example, in order to approximate
$\int_1^3 \frac{x^2\ln x}{x+1} \, dx$ you should enter
\smallskip
\noindent
\verb$>> quad('x.^2.*log(x)./(x+1)', 1, 3)$
\smallskip
\begin{problem}
What is \textsc{MatLab}'s answer?
\end{problem}
When using a computer algebra system to numerically integrate a function, you should always
make sure beforehand that the integral that you are trying to evaluate is proper.
For example, let us try to approximate the improper integral
$\int_{-1}^1 \frac{1}{x^2} \, dx$.
\smallskip
\noindent
\verb$>> quad('1./x.^2', -1, 1)$
\smallskip
\textsc{MatLab} will complain that you asked it to divide by zero and give you the
answer \verb$NaN$, which stands for ``not a number.'' Since $\int_{-1}^1 \frac{1}{x^2} \, dx
= \infty$, you might have expected an answer of ``\verb$inf$,'' but this is not what you get.
Now let us try to use \verb$quad$ for evaluating $\int_{-\pi}^{2\pi} \frac{1}{x^2} \, dx$. Let us also keep track of the number of function evaluations performed by \textsc{MatLab}.
\begin{problem}
What command do you enter for this? How many function evaluations does \textsc{MatLab} go through? Why does \textsc{MatLab} give you a numerical answer now; whereas it did not give you
a numerical answer in the previous example? \emph{Hint:} The last question requires some thought. You will need to remind yourself how Simpson's rule works and use your imagination to
second-guess the steps taken by \textsc{MatLab} in executing your commands.
\end{problem}
\end{document}