% Graphing Functions with MATLAB
% MATH 266A Exercise
\documentclass[12pt]{article}
\usepackage{times}
%%%\pagestyle{empty}
\pagestyle{myheadings}%%%
\markright{MATH266A \hfill Graphing Functions with \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
\begin{document}
\begin{center}
{\Large Graphing Functions with \textsc{MatLab}
\footnote{Copyright \copyright 2004 Winfried Just, Department of Mathematics,
Ohio University. All rights reserved.}}
\end{center}
\bigskip
One of the nice features of \textsc{MatLab}
is that this computer algebra system allows you to easily define functions. For example,
consider the linear function $u(t) = 0.6t + 1.2$. We are now going to define this
function in \textsc{MatLab} by entering:
\smallskip
\verb$>> u = inline('0.6*t + 1.2')$
\smallskip
The word ``inline'' indicates that the function has been defined right in the command
window rather than in a separate file.
This command specifies the formula for the function as well as the independent variable.
Do \emph{NOT} forget any of the single quotes or the $*$ for the multiplication operation;
if you do, \textsc{MatLab} will give you an error message. If you did everything correctly,
\textsc{MatLab} will show what function has been defined.
\medskip
Now we can let \textsc{MatLab} evaluate the function $u(t)$ for various arguments.
To find the value of this function at $1$, enter
\smallskip
\verb$>> u(1)$
\smallskip
To find the $y$-intercept, enter
\smallskip
\verb$>> u(0)$
\smallskip
Now let \textsc{MatLab} evaluate the value of $u$ for $t = 2.67$.
\medskip
Another nice feature of \textsc{MatLab} is that it allows us to plot graphs of functions.
The simplest way to do this is by using the command \verb$ ezplot$. Let us use
this tool to draw the graph of the function $f(x) = 1.2x$ on the interval
$[0, 2]$. For that, you enter
\smallskip
\verb$>> ezplot('1.2*x', [0,2])$
\smallskip
Don't forget the single quotation marks. If you did everything right, a new window
with the graph of the function should appear. Note that the \verb$ ezplot $ command
automatically chooses an appropriate scale for the $y$-axis. Now you can label your graph
as follows: From the ``tools'' menu on your graph, choose ``edit plot.''
Then click on the A icon in the second bar of your graph. Then click anywhere on your graph.
A grey shaded area should appear. Write in this area ``$f(x) = 1.2x$.''
Notice that you can drag around the label until the whole picture looks nice.
\medskip
Now let us see what happens to the graph of $f(x)$ if we add a constant to it;
let us say if we plot the graph of $g(x) = 1.2x + 0.9$. Since you want to
make the two graphs appear on the same plot, you first enter
\smallskip
\verb$>> hold on$
\smallskip
Next you enter
\smallskip
\verb$>> ezplot('1.2*x + 0.9', [0,2])$
\smallskip
and label the graph of the second function as $g(x) = 1.2x + 0.9$ in the same
way as you labeled the graph of the previous function. The label for the previous function may now
be in the wrong spot; make sure to move it next to the graph of the first function. Now let us see, on the same
picture, what happens if we make the slope smaller. For that,
use \verb$ ezplot $ to draw the graph of the function $h(x) = 0.5x + 1.2$ on the
interval $[0, 2]$. Label the new graph. \emph{Be sure to move any old labels to
appropriate places so that you can see which graph is which.}
\medskip
Finally, let us plot the graph of a linear function with a negative slope; please
enter the appropriate command to plot the graph of the function $j(x) = -0.4x + 2$
on the interval $[0, 2]$, and label the graph of your new function.
\emph{Be sure to move all old labels where they should be.}
\medskip
Now we are ready to put the finishing touches on the nice picture you have produced. Enter
\smallskip
\verb$>> hold off$
%%%\verb$>> title '(your name)$ \textsc{MatLab} \verb$Exercise Number 2'$
\verb$>> title '(your name)$ \textsc{MatLab} \verb$Exercise on Graphing'$
\smallskip
%%%You may be required to submit it as part of
%%%your first quiz.
If this \textsc{MatLab} exercise is being counted in your grade,
then print this figure for submission.
You can print the figure if you go to the ``file'' menu
in the upper left corner of the window with the graphs, and choose ``Print.''
If you cannot print from the computer you are working at, then you need to save
your figure to a file. You can do this in one of two ways. Go to the file menu.
If you use the ``Save As'' option, \textsc{MatLab} will save the picture as a
.fig file that can be read only by \textsc{MatLab}. If you want to print it
eventually from a computer that does not have \textsc{MatLab} installed, then
you should save your picture in a format that your other computer can understand,
for example as a .jpg image file. For that, you need to use the ``Export'' option
in the file menu and choose an appropriate file type. Either way, \textsc{MatLab}'s
default option is to save the file in the ``work'' subdirectory of the \textsc{MatLab}directory. You may choose a different directory by moving up and down the directory
tree. The current directory is shown in the ``Save in'' field. Once you have saved
the figure in a file, you should be able to e-mail it to yourself for future printing.
\medskip
Let us look at the picture you have produced. Somewhere in the middle of it,
there is a point where the graphs of the functions $h(x) = 0.5x + 1.2$ and
$j(x) = -0.4x + 2$ intersect. We can use \textsc{MatLab} to find the
$x$- and $y$-coordinates of this point. Note that the $x$-coordinate must satisfy the equation
$0.5x + 1.2 = -0.4x + 2$. We can find this $x$ by using the following command:
\smallskip
\verb$>> solve('0.5*x + 1.2 = -0.4*x + 2')$
\smallskip
\textsc{MatLab} gives you the answer to this in a funny format, with many places
after the decimal point. Now we want to find the $y$-coordinate of the point of
intersection. For this, we should substitute the $x$-coordinate either into
$0.5x + 1.2$ or into $-0.4x + 2$. However, you probably do not want
to type all those digits \textsc{MatLab} gave you. Here is an economic way
of computing the $y$-coordinate with \textsc{MatLab}: Enter
\smallskip
\verb$>> 0.5*ans + 1.2$
\smallskip
Note that this substitutes the answer to the previous problem in place of ``ans.''
\medskip
Now finish your first \textsc{MatLab} exercise by finding the $x$- and
$y$-coordinates of the point where the graphs of the functions
$g(x) = 1.2x + 0.9$ and $j(x) = -0.4x + 2$ intersect.
\medskip
Now finish your \textsc{MatLab} session by entering
\smallskip
\verb$>> quit$
\end{document}