\documentclass[10pt]{article}
\textwidth 16cm
\textheight 23cm
\topmargin 0cm
\evensidemargin 0cm
\oddsidemargin 0cm
\parindent=0pt
\parskip=2pt
\begin{document}


\begin{center}
\Large \bf
Computational Methods and Data Analysis 2003\\
\large 
\end{center}
\vspace{0.4cm}
\noindent 
{\bf Ex.~1: Use of MATLAB}\\

This exercise is meant to learn to use matlab. It can also be found in my
dyrectory \\
$\sim$fasolino/CMDA/exer\_2003 \\
and in the home page of this course\\
http://www.snn.kun.nl/$\sim$tom/cmda2003.html. 

It is better if each of you 
works alone. Only typing yourself you get to know the use of a computer.  
Of course you can exchange questions, comments and successful solutions. 
Actually those who find this exercise very easy are encouraged to help the 
others. 

An interactive matlab course can be found at: \\
{\tt http://www.mines.utah.edu/gg\_computer\_seminar/matlab/matlab.html?} ,\\
a rather complete manual can be downloaded as pdf file at:\\
{\tt http://csp.tn.tudelft.nl/matlab/matlablong.pdf}.\\
If you open the latter as pdf file on your screen, you can easily search for 
a keyword, for instance the name of a function. The manual is in colour but a
print out in gray-scale is just as good and much cheaper. Use the option -Zg in
the lpr unix command.
 
It is useful to have all the 
programs of the course in one directory which is open for reading. To open a directory for reading use the unix command\\
{\tt mkdir CMDA}  (this creates the directory CMDA)\\
{\tt chmod 755 CMDA} (this gives reading access to everybody)

Within this directory you can create other subdirectories for each exercise.
Each directory has to be opened for reading.

To give reading access to a file myfile.m

{\tt chmod 644 myfile.m}\\
To start matlab type\\
{\tt matlab -nojvm}\\
the option -nojvm avoids the graphical (java) window which slows down the 
functioning. To close matlab you will type {\tt quit} or {\tt exit}.

When matlab starts type

{\tt helpwin}

This opens an extra window with help information. 

{\bf Working from keyboard}

Assign:
\begin {itemize}
\item scalars:
year=2003 ; conversion\_factor=2.2 ;  hbar=1.054e-34
\item row vectors: $v_1$= (1  5  2  1 0); $v_2$= (7 -1 1 0 1)
\item a $3 \times 3$ matrix with all elements equal 1 or all elements equal 
zero.
Which is the easiest way? Write the $3 \times 3$ unit matrix by use of a loop.\\
For the $2\times 2$ matrix \\
{\tt A=[1 2;3 4]} \\
calculate\\
{\tt A*A, A.*A , A$\hat{~}$2,  A.$\hat{~}$2,  A/A , A./A}. 

Check the meaning of each operation by calculating analytically the result.

\item calculate the scalar product $v_1 \cdot v_2$ by use of a loop or by
using the $.*$ operator of matlab. Check that the result is right.

\item find the minimum and maximum component of vector $v_1$. This can be done
 by using the built-in matlab functions {\tt min} and {\tt max}. 
Use the help to 
find out how to use them, namely type {\tt help min}. Here it is suggested 
to look at similar functions, for instance have a look at {\tt range}. 
In this way you come to learn other features of matlab. 
\end{itemize}

{\bf Writing a script}

Write a script (an m-file, you could call it basics.m) which performs the 
previous operations. When it is ready you just need to type the name of the 
file (without .m) to execute it. Make use of the function {\tt input} 
to read the vector 
components from the keyboard. To ask for input you can use  
{\tt input} with a string of characters. 
Put some comments in your script describing what you are doing. 
 By typing
{\tt help basics} the comments which are at the top of the script 
(before the first command) appear on the screen. 
It is good practice to write comments and to name variables in English since
international cooperation is likely to occur in your future.
It is also better to start such a script by clearing the memory and all files 
and figures with\\
{\tt clear all}\\
{\tt close all}\\

{\bf Reading and writing from a file}

To give all components of a vector by hand is boring and can lead to errors. 
It is often better to have the input on a file which is then read by matlab.
For instance one often uses matlab to plot experimental or calculated 
data which is stored in a file. 

Let's first create a file which contains the vectors \\
a=( 1 2 3 ... 10) and the
vector formed by taking the square of each component
b=( 1 4 9 .... 100). Instead of giving the components, find matlab commands 
which build these vectors.\\
The simplest way to write these vectors in a file is by use of the command {\tt save}, namely\\
{\tt save filename a b -ascii}. 
If you want to write with a specified format you can 
use {\tt fopen} to create a file called, for instance, data, 
and write with 
{\tt fprintf} the components, namely:\\
{\tt fid=fopen('data','w');\\
for i=1:10\\
\mbox{~}fprintf(fid,'\%5i \%5i$\backslash$n',a(i),b(i));\\
end\\
fclose(fid); }

which writes the vectors in the file data as follows\\
\begin{tabular}{r r }
1 &  1\\
2 &  4\\
3 &  9\\
..& ..  \\
10 & 100\\
\end{tabular}
 
Now create another script which  reads the 
two vectors which you will call {\tt x} and {\tt y} from the file data. 
This can be done by use 
of {\tt load} in several ways. For instance
 
{\tt load 'data'\\
$\left[  \mbox{\tt nrow, ncol}  \right]$ = size(data)\\
x=data(:,1)\\
y=data(:,2)}

Check that the 
vectors have been read correctly. Check the size,  with {\tt size(x)}, 
the first component x(1), the last x(end). Plot them by using:
{\tt plot(x,y)}, {\tt plot(x,y,'*')},  {\tt plot(x,y,'g')},
{\tt plot(x,y,'-', x,y,'+')} ,  {\tt semilogx(x,y)}, {\tt semilogy(x,y)},  
{\tt loglog(x,y)}.
Plot also z=x+y in the same graph, by using {\tt hold on}.\\
Try and label the axis, by learning to use {\tt xlabel, ylabel, axis, legend.}\\

\end{document}
