%
% lab2: Matlab Functions
%
\documentclass[12pt]{article}
\pagestyle{empty}
\newcommand{\Hrule}{\rule{\linewidth}{1mm}}
\setlength{\parindent}{0mm}
\setlength{\parskip}{0mm}
\setlength{\topmargin}{0.25in}
\setlength{\headheight}{0mm}
\setlength{\headsep}{0mm}
\setlength{\textheight}{9.25in}
\begin{document}
\Hrule
\vspace{0.10in}
\centerline{MATLAB Functions}
\Hrule
\noindent In this lab, we will learn to write MATLAB script files and
MATLAB functions.
\begin{enumerate}
\item Please study the following sections of the MATLAB tutorial:
\begin{enumerate}
\item ``Scripting Files:"
{\tt http://www.isr.umd.edu/\~{}austin/ence202.d/matlab.html\#sec8}
\item ``Functions and Function Files:"
{\tt http://www.isr.umd.edu/\~{}austin/ence202.d/matlab.html\#sec9}
\end{enumerate}
There is no need to print the tutorial. The most effective
way to read it is to start MATLAB and Netscape and then type in the
examples in MATLAB as you read the tutorial in Netscape. If something
in the tutorial is confusing, type it into MATLAB to see exactly what
it does.
\item {\bf Script Files:}
\begin{enumerate}
\item Using the \emph{text} editor of your choice,
create a file named {\tt example1.m}. This will be a MATLAB
script file. A script file is very simple: it contains
ordinary MATLAB commands that you will (later) execute in
MATLAB. The script file you create should contain the
following MATLAB commands:
\begin{verbatim}
x = -5:0.01:5;
y = exp(-abs(x)).*cos(2*pi*x);
plot(x,y);
xlabel('time');
ylabel('amplitude');
title('f(x)=exp(-|x|)cos(2*pi*x)');
\end{verbatim}
It is very important that the filename you created end in
``{\tt .m}" and that you use a \emph{text} editor. If you are
unsure what a text editor is, use {\tt Notepad} on the PC's
or {\tt pico} or {\tt emacs} or {\tt vi}
on Unix machines. Both are easy to learn
and both will produce text files that MATLAB can read.
On the PC's, save the file to \verb+ g:\\example1.m+. On the
UNIX machines, save it to {\tt example1.m}.
\item Start MATLAB. At the MATLAB prompt, type in the name of
the file you created above, except for the ``{\tt .m}" part:
\begin{verbatim}
>> cd g: (PC's only)
>> example1
\end{verbatim}
If you typed it in correctly, you should see a plot of
$$
f(x) = e^{-|x|}\cos(2\pi x), \ \ \ x \in [-5,5].
$$
If you get errors, fix them in the file ({\tt example1.m}),
save the file,
and then rerun the script file until you get the
plot. This is the technique for writing things in MATLAB
that you want to save and run later: save the commands in
script files.
\end{enumerate}
\item {\bf Function Files:}
\begin{enumerate}
\item MATLAB functions are very similar to MATLAB script files.
In this section, we will create a \emph{text} file that will be
a MATLAB function. The function file you create should be named
{\tt example2.m}. This file should contain the following MATLAB
commands:
\begin{verbatim}
function y=example2(x)
%
% example2: this is where the help entry goes
%
y = exp(-abs(x)).*sin(2*pi*x);
return;
\end{verbatim}
\item Start MATLAB and enter the following commands at the MATLAB
prompt:
\begin{verbatim}
>> help example2
>> t=0:.01:6;
>> plot(t,example2(t));
\end{verbatim}
If you did everything correctly, you should see the help
text in response to ``{\tt help example2}." Notice that
we've now added a new command to MATLAB that can be used as if
it were a built-in function. If you get errors, you must
correct the file {\tt example2.m}, save it, and then rerun
the commands above.
The MATLAB on-line help system has a nice write-up of
functions and how to handle various things like returning
more than one value, checking the number of arguments, etc.
Please type the following commands and read the online help:
\begin{verbatim}
>>> more on
>>> help function
>>> help script
\end{verbatim}
\end{enumerate}
\newpage
\item Create new MATLAB functions for each of the following functions:
\begin{enumerate}
\item {\tt step(t)} function:
$$
\mbox{\tt step}(t) = \left\{ \begin{array}{ll}
1 & \mbox{if $t \ge 0$} \\
0 & \mbox{if $t < 0$} \\
\end{array}
\right.
$$
Your {\tt step(t)} function should take a single argument, which
may be either scalar- or vector-valued. Note: you will need to
be clever in order to handle vector-valued inputs. The
MATLAB command {\tt find} may be useful in writing this function.
Alternatively, you may decide to write a loop inside your function.
See the MATLAB {\tt for} command for help on writing loops. Other
commands that may be useful: {\tt length}, {\tt size}, {\tt zeros},
and {\tt ones}. These are explained in the MATLAB online help.
\label{step}
\item {\tt ramp(t)} function:
$$
\mbox{\tt ramp}(t) = \left\{ \begin{array}{ll}
t & \mbox{if $t \ge 0$} \\
0 & \mbox{if $t < 0$} \\
\end{array}
\right.
$$
Your {\tt ramp(t)} function should take a single argument, which
may be either scalar- or vector-valued. The same comments as for
the {\tt step(t)} command apply here as well. Note: MATLAB commands
may call each other, so a clever way to implement the {\tt ramp}
function would be to construct it from the {\tt step} function you
wrote in part~\ref{step}.
\item To test your functions, plot the following:
$$
f(t) =
5r(t+3) - r(t+1) -3r(t) + 5r(t-1) + 3u(t-2)
$$
for $t\in[-5,5]$. In MATLAB, use {\tt t=-5:.01:5} for the time-axis,
use {\tt ramp(t)} for $r(t)$ and {\tt step(t)} for $u(t)$.
\label{plot}
\end{enumerate}
\item \underline{For your report, turn in the following:}
\begin{itemize}
\item File {\tt step.m} containing the function {\tt step(t)}.
\item File {\tt ramp.m} containing the function {\tt ramp(t)}.
\item The graph produced in part~\ref{plot}. All axes of the
graph should be appropriately labeled.
\end{itemize}
\end{enumerate}
\end{document}