
\begin{frame}
  \maketitle
\end{frame}


%\section{CS}



%\begin{frame}\frametitle<presentation>{Lecture structure}
%  \tableofcontents
%\end{frame}


%%%%BEGIN  What, where, how %%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{First steps}
\Huge{Meet the IDE\\ Getting Help\\ Basic scalar variables}.
\end{frame}

\begin{frame}[fragile]
\frametitle{1. What, where, how}

  \begin{itemize}  
  \item
  \textbf{Mat}rix \textbf{lab}oratory  [Cleve Moler,  Mathworks inc.]

%  \begin{itemize}
 %  \item Perustietorakenne: Matriisi/taulukko
 % \end{itemize}
 % \item  Numeerisen laskennan ty\"oskentely-ymp\"arist\"o
 \item  Language and tool for numerical computation
  %\begin{verbatim}
  %  >> quad(@(x) exp(-x.^2),0,1) 
    
  %  ans = 
  %             0.7468
  %\end{verbatim}
 
 \item
Large number of mathematical and other functions.
% Suuri joukko matemaattisia ja muita funktioita, joita k\"aytt\"aj\"a voi m\"a\"aritell\"a (ohjelmoida)
% lis\"a\"a.
 
 \item Functional programming language, user can extend Matlab by defining 
(programming) own functions.
 
 \item
 Application specific toolboxes
 \item
\href{http://se.mathworks.com/help/matlab/index.html}{http://se.mathworks.com/help/matlab/index.html}
 \item 
   \href{http://www.mathworks.se/academia/}{http://www.mathworks.se/academia/}
 % Mathworks's study materials
   % \begin{itemize}  
   \item
   \href{http://se.mathworks.com/help/matlab/examples/basic-matrix-operations.html?prodcode=ML}{http://se.mathworks.com/help/matlab/examples/basic-matrix-operations.html?prodcode=ML} 
   %\end{itemize}
% \href{https://math.aalto.fi/~apiola/matlab/opas/lyhyt/}{https://math.aalto.fi/$\sim$apiola/matlab/opas/lyhyt/} 
\item \alert{google:} \textcolor{blue}{learn matlab, matlab <keyword>}
%\item\href{https://math.aalto.fi/opetus/Mattie/MattieO/matlab.html}{https://math.aalto.fi/opetus/Mattie/MattieO/matlab.html}
 \end{itemize}
\end{frame}
%%%% END  What, where, how %%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile]
%\frametitle{2. References}
%\begin{enumerate}
%\item \href{http://se.mathworks.com/help/releases/R2016b/pdf_doc/matlab/index.html?s_cid=doc_ftr}{http://se.mathworks.com/help/releases/R2016b/ \\ pdf\_doc/matlab/index.html?s\_cid=doc\_ftr}  Mathworks: PDF Documentation for MATLAB
%\item \href{http://se.mathworks.com/help/}{http://se.mathworks.com/help/}
%\item More on webpage
%\end{enumerate}
%\end{frame}
%\begin{frame}{Matlab desktop}
%\includegraphics[width=10cm]{img/desktop.png}
%\end{frame}
%\maketitle

%\begin{frame}
%{\color{NavyBlue!80} A B C}
%\end{frame}
%\begin{frame}
%\frametitle{Two Column Output}
%\begin{columns}[c]
%\column{1.5in}
%Practical \TeX\ 2005\\
%Practical \TeX\ 2005\\
%Practical \TeX\ 2005
%\column{1.5in}
%\framebox{\includegraphics[width=1.5in]{DSC_0277.JPG}}
%\end{columns}
%\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{help,doc,lookfor}

\begin{itemize}
\item help, doc
   \begin{itemize}
   \item \verb_>> doc_ starts help system, same as \fbox{?}  
   \item \verb_ >> help name  >> doc name_ \\
    \textbf{help} is faster, \textbf{doc} is more comprehensive. \\
  \item Some search words for help/doc: \\
    \emph{elfun} -- \alert{el}ementary \alert{fun}ctions \\
    \emph{general, ops, elmat}, ...
    More on next slide         
   \end{itemize}
%\end{itemize}
%\end{frame}
%\end{document}
 
% \item \verb_ >> doc _+ "Search tab "  $\implies$ hakutoiminto \\
% Esim:\\
% \verb_     >> doc_ \fbox{fmin},  \fbox{poly},  \fbox{ode}  \\

 
\item \textbf{lookfor}\\
\mcode{>> lookfor sum, lookfor solve}\\ 
\mcode{>> lookfor optimize, lookfor equation}\\

\textbf{Beware:} Some searches may give too many hits.
\item google Matlab,<keywords, phrases> 


 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Some help-keywords  >>help}

\begin{verbatim}
general        - General purpose commands
ops            - Operators and spec. chars
lang           - Programming language constructs
elmat          - Elementary matrices
elfun          - Elementary functions
specfun        - Special functuons
matfun         - Matrix functions
datafun        - Data analysis and Fourier transform
graph2d        - 2d graphics
graph3d        - 3d graphics
graphics       - Handle graphics
imagesci       - Image and scientific data
demos          - Examples and demo's
\end{verbatim}
%\item 
\href{https://math.aalto.fi/opetus/MatOhjelmistot/2016syksySCI/Lectures/helpdoc.html}{Comprehensive set of keywords} %More help/doc keywords.
% \href{helpdoc.html}{helpdoc.html} 
%keywords: {helpdoc.html in this dir})


\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile]
%\frametitle{Course plan}

%\begin{itemize}
%\item .
%\end{itemize}

%\begin{verbatim}
%\end{verbatim}

%\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}
%\frametitle{Matlabin "desktop:n" ulkon\"ak\"o}

%\framebox{\includegraphics[width=1.5in]{kuva.jpg}}
%Kuva

%\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%\frametitle{First steps and concepts} begin%%%

\begin{frame}[fragile]
\frametitle{First steps and concepts}
\begin{columns}[c]
\column{2in}

\begin{itemize}
\item Workspace, command window
   \begin{itemize}
   \item Matrices and other datatypes are stored in memory, contents 
are shown in 
\textbf{workspace.}.
   \item \texttt{>> who, whos} 
   \end{itemize}
\end{itemize}

\begin{itemize}
\item Commands (functions) are applied to variables in the workspace.
   \begin{itemize}
%   \item Matlab tulkkaa ja palauttaa tuloksen (tai virheilmon) ty\"otilaan.
   \item Matlab \emph{interprets} and returns the result(s) in the workspace. 
(Or displays an error message) 


   \end{itemize}
\end{itemize}

\pause

\column{2in}
\begin{enumerate}
\item Start Matlab 

  % \begin{itemize}
   %\item Matlab tulkkaa samantien ja palauttaa tuloksen (tai virhelmon) ty\"otilaan.
   %\end{itemize}

\item Create a working directory: Either File-menu or command \\
\mcode{>> mkdir mydir} \footnote{Some Unix/Linux-commands can be given \\ 
in the Matlab command window})
\item \mcode{>> cd mydir}
\item Create variable:\\
\mcode{ >> x=5}\\
\item Do:  \mcode{>> y=exp(x)}
\item Try \mcode{>> who, whos}
\end{enumerate}

\end{columns}

\begin{verbatim}
\end{verbatim}

\end{frame}
%%% END %%%%%\frametitle{First steps and concepts} end%%%

%%%%%%%%\begin{frame}[fragile]{Working in the command window}%%%%%%%%
\begin{frame}[fragile]{Working in the command window}

\begin{itemize}
\item
``Undoc'' command window (or make it large enough)
\item 
Here's a possible first session, try yourself!
\begin{lstlisting}
>> 3/4
ans =
    0.7500
>> 4*ans
ans =
     3
>> r=3/4;  % Supress output
>> r       % Show result
r =
    0.7500
>> Area=pi*r^2
Area =
    1.7671

\end{lstlisting}
\end{itemize}
\end{frame}
%%%%%%%%%%%\end{frame} working in the command window



\begin{frame}[fragile]{Arithmetic operations, examples}

\begin{itemize}
\item
Multiplication  and division from left to right, equal 
precedence.
\item
Ordinary precedence rules. \textbf{Use parentheses} for clearity ! \\
\end{itemize}

\begin{lstlisting}

>> 6/3*2                 >> 6/3/2  
   ans = 4                  ans = 1
>> 6/(3*2)               >> 6/(3/2)   
   ans = 1                  ans = 4
\end{lstlisting}

\end{frame}

\begin{frame}{Exercise}
  Make the following variables:
  \begin{itemize}
  \item a = 10
  \item  b = 2.5$10^{23}$

  \item  c = $2+3i$ ($i$ being the imaginary unit)
  \item  d =$e^{\frac{2}{3}\pi i}$
  \end{itemize}
\end{frame}


\begin{frame}[fragile]{Arithmetic, precedence}

\begin{block}{Scalar arithmetic operations}
\begin{center}
\begin{tabular}{|c|c|c|c|}\hline      %1tabular1taulukko1latex
Symbol & Name & Math & Matlab \\ \hline 
   \verb_+,-_ & add/subtract & $a \pm b$ & \verb_a+b,a-b_ \\ \hline   
  \verb_*_   &  multiply    & $a b$    & \verb_a*b_  \\ \hline    
  \verb_/_ & Righ divide & $\frac{a}{b}$ & \verb_a/b_ 
\footnote{Recommendation:Use this for scalar division}   
\\ \hline     
  \verb_\_ & Left divide & $\frac{b}{a}$ & \verb_a\b_ 
\footnote{Recommendation:Use this for ``matrix division''}   
\\ \hline       
  \verb_^_ & power       & $a^b$         & \verb_ a^b_\\ \hline        
\end{tabular} 
\end{center}
\end{block}
\end{frame}

%\end{document}

\begin{frame}
\frametitle{Command window, history, create script}

\textbf{Command window:}
\begin{itemize}
\item
Use the up-arrow key to scroll back through
the commands.
\item
Use the down-arrow key to scroll forward
\item
Edit a
line using the left- and right-arrow keys.
\item
Press the Enter key to execute the command
\end{itemize}
\textbf{Create script from command history:}
\begin{itemize}
\item
Choose commands from the history with \fbox{CTR + mouse left}.
Mouse right lets you choose ``create script''. (More on scripts soon.)
\item Execute commands from the editor: \fbox{CTR-Enter}. 

\end{itemize}
\end{frame} 
%%%%%%%%%%% HERE %%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Little scalar task, work together}
\begin{itemize}

\item
%W.J. Palm 1.1 p. 11  \\
The volume of a circular cylinder of height h
and radius r is given by 
$V=\pi r^2 h$.
A particular cylindrical tank is 15 m high and has a radius of 8 m. We want to construct another
cylindrical tank with a volume 20 percent greater but having the same height. 
How large must its radius be?
\end{itemize}

%\textbf{Solution}


%Solve for r:
%$$ r=\sqrt{\frac{V}{\pi h}}$$

%\pause
\end{frame}

\begin{frame}[fragile]
\frametitle{Solution, command history, make script}
Here's the Matlab-session:
\begin{lstlisting}
>>r = 8;
>>h = 15;
>>V = pi*r^2*h;
>>V = 1.2*V;         % 20% increase in V
>>r = sqrt(V/(pi*h))
r =
8.7636
\end{lstlisting}
Use \fbox{$\uparrow$} for command history. With CTR+Mouse left paint 
commands you want to save, press mouse right and choose ``make script''.
 
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Scripts, publish}
You can perform operations in MATLAB in two ways:
\begin{itemize}
\item
In the interactive mode, in which all commands are entered directly in the
     Command window.
\item
By running a MATLAB program stored in a script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time—at the Command window prompt. You can run the file by typing its name at the Command window prompt.
\item The script file commands can also be executed directly from Matlab's editor window either by parts or all of them.
\item \mcode{publish} produces a well structured document of running the script.
\end{itemize}
\end{frame}



\begin{frame}[fragile]
\frametitle{Examples of expressions}

\begin{lstlisting}
>> 6*sqrt(2)+pi^2   
   ans=18.3549
>> one=sin(pi/3)^2 + cos(pi/3)^2  
  one =  1
>> 1==sin(pi/3)^2 + cos(pi/3)^2    %  Equal?
  ans = 1                         %  Logical: true
>> exp(i*pi)                      %  Not e^x !!
>> 1.0/0.0 ->  Inf
>> -4/Inf  ->  0
>> 0/0     ->  NaN  % "Not-a-number".
>> format long      % Show max number of digits.
>> [1+eps,1+3*eps]  % eps: Limit of rel. accuracy.
>> format short     % Back to default display.
>> clc              % Clean display.
>> clear            % Remove all variables from ws.
 \end{lstlisting}

%\begin{itemize}
%\end{itemize}

\end{frame}

\begin{frame}[fragile]
\frametitle{Workspace}

\begin{itemize}
\item
Variables are stored in the memory and accessed in the workspace
\item Commands for managing the workspace are called here ``system commands'', perhaps a little ``unofficially''. For instance \alert{who, whos} show variables in the workspace, latter with sizes.
\item \alert{clear} erases all variables from the workspace (memory),
\alert{clear var1 var2} erases these variables.
\item The syntax of ``system commands'' differs from computational and other 
\alert{functions}. System commands don't use parenteheses or commas. 
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Some ``system commands''}
\begin{block}{Some commands for managing the workspace}

\begin{center}
\begin{tabular}{|l|l|}\hline
\textbf{Matlab command} & \textbf{Description} \\ \hline

\verb_clc_ & Clear command window (visually).\\ \hline
\verb_clear_& Clear all variables (from memory).\\ \hline
\verb_clear var1 var2_ & Clear these variables.\\ \hline
\verb_who_ & List variables in memory\\ \hline
\verb_whos_ & List variables with sizes in memory\\ \hline
\mcode{format} & Display format of numbers\\ \hline
\verb_clf_ & Clear current graphics window.\\ \hline
\verb_close all_ & Close all graphics windows.\\ \hline
\verb_shg_ & Show Graphics.\\ \hline
\end{tabular} 
\end{center}
\end{block}
\end{frame}


\begin{frame}[fragile]
\frametitle{Comparison, relations, scalar case}
\begin{itemize}
\item Remember: \mcode{name = expression} means assignment of the value of expression to variable \mcode{name}.
\item \mcode{lhs == rhs} Returns 1 if equal,0 if not.
%\item \mcode{<, <=, >, >=},$\sim$= are other arithmetic comparisons.
\item \verb_<, <=, >, >=_, $\sim=$ are other arithmetic comparisons.
\item The value of a comparison is true (1) or false (0).
\item Precedence of arithmetics is higher than that of comparisons
\end{itemize}
\begin{lstlisting}
>> 1==0                 % --> ans = 0
>> E = 1.733>tan(pi/3)  % --> E = 1
\end{lstlisting}
What are the results ? :  \mcode{>> E=4>5-2} ,  \mcode{(4>5)-2} 
\end{frame}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Expression, variable, special variable \emph{ans}}
\begin{itemize}
%\item
%Kirjoita Matlablauseke komentoikkunan kehotteeseen \verb_>>_
%\item
%Tulos lasketaan ja sijoitetaan muuttujaan \verb_ans_
\item
An expression consists of numbers, variables, functions, operators such as
 \verb_+,-,*,/,^,(), sin, cos, exp, abs, ... _\\
\item
help/doc ops,elfun [See previous slide for more searchwords.]
\item \verb_>> var=expression_ \\
assigns the value of \emph{expression} to variable \emph{var}. 
\item
If the expression is written without an assignment, the result is assigned to the special variable \alert{ans}.\\ 
\textbf{Note:} \alert{ans} holds just the \alert{previous result}, the next such computation overwrites it.

\end{itemize}

\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\frametitle{Variable names and types}

\textbf{Variable names:}
  \begin{itemize}
    \item Start with a letter, then letters, numbers, underscore( \_) 
    \item Other special characters not allowed, especially minus (\verb_-_)
        is not possible, as it means subtraction.
    \item CASE SENSITIVE! ( var1 is different from Var1)
  \end{itemize}
%\begin{itemize}
\textbf{NOTE:} Matlab help texts: old style (from 1980's) of
capitalized NAME meaning \textbf{name}, Let's abandon this usage.
%\textbf{NOTE:} Matlab documentation uses the old style (from 1980's) of
%capitalized NAMES (meaning \textbf{boldface}) even though the object names 
%(functions, variables) are not. So copy/paste from help texts lead to errors. I%n this course, I recommend not to use this convention in user defined functions%' help texts.

\begin{lstlisting}
  >> number=-2.345     
  >>        % Note: period (.), not comma (,)
  >> complex_number=3+4*i       
  >> n=1;n=n+1;
  >> string=['This is trial nr. ' num2str(n)]
  >> length(string)
    ans =  19
\end{lstlisting}



%\begin{lstlisting}
%\end{lstlisting}
\end{frame}




\begin{frame}[fragile]
\frametitle{Variable names and types}

\begin{itemize}
\item No need to initialize or define a variable, if efficiency is not an issue (return to this later).
%\item Muuttujia ei tarvitse alustaa eik\"a tyyppi\"a m\"a\"aritell\"a.
\item Default type is 64 bits floating point number ("double"), about $16$ decimal digits.\\
%\item Lukujen oletustyyppi on 64:n bitin liukuluku (``double''), n. 16 desimaalinumeroa.\\
\verb_      >> 2.345_
\item Characters are of type 'char' (16 bits)
%\item Merkkien tyyppi on 16 bitin 'char'\\
\verb_      >> 'this is a character string'_
%\item
%Useimmat kohtaamamme muuttujat ovat edellisist\"a koostuvia matriiseja.
\item Change numeric data into character  \\
\verb_      >> num2str(2.3)_ \\
\verb_      >> str2num(ans)    % and back_
\item Other tyes: \texttt{logical, single,int-types} \\ 
\verb_help datatypes_\\
\href{https://se.mathworks.com/help/matlab/numeric-types.html}{https://se.mathworks.com/help/matlab/numeric-types.html}

\end{itemize}
\end{frame}


%%%%%%\begin{frame}[fragile]{Complex numbers} %%%%%
\begin{frame}[fragile]
\frametitle{Complex numbers}
\begin{itemize}
\item
All arithmetic in Matlab works on complex numbers as well.
Matlab has special variables \mcode{i} and \mcode{j} for $\sqrt{-1}$.
\item All special variables can be overwritten, so:
\begin{lstlisting}
>> 2+3*i
ans =
   2.0000 + 3.0000i
>> i=1;
>> 2+3*i
ans =
     5
>> clear i
>> i
ans =
   0.0000 + 1.0000i
\end{lstlisting}


\end{itemize}



\end{frame}

\begin{frame}[fragile]
\frametitle{Complex numbers continued}
%\begin{itemize}
%\item

\begin{lstlisting}
>> sqrt(-1)
ans =
   0.0000 + 1.0000i
>> 4 + 6*j;
>> 4 + 6j;   % Correct, I don't recommend:
>> 4+j6   % -> Undefined function or variable 'j6'
>> x=1;y=2;x+y*i
>> x+yi;           % Same error.
>> C=1 - 2*i;
>> real(C), imag(C)
>> abs(C)
>> angleDegrees=angle(C)*180/pi
>> exp(i*pi)  % Matlab meets Euler!
ans =
  -1.0000 + 0.0000i
\end{lstlisting}


%\end{itemize}

\end{frame}

