Matlab is an interactive system used for numerical calculations and visualization in almost every field of technical and scientific research. Matlab excels in its ease of use, good graphics and high efficiency.
This introduction explains the basics mandatory to perform simple numerical calculations. First, we use Matlab as a command line interpreter. Entering of single commands will follow a prompt, here `>>`. An important command is `help`. This command returns descriptions and syntax for functions and commands. For example
`help sin`
shows information regarding the sine function. Also a link to the detailed documentations can be found, that can directly be called alternatively with the command `doc`. Short descriptions of commands, that match certain search pattern, can be obtained by `lookfor`.
Further useful commands include `quit` and `exit`, with which the Matlab session is terminated.
Variables are introduced by assignment and do not have to be declared or defined. Names of the variables can be chosen freely. The only forbidden things are the use of special characters and digits at the beginning of the name. A distinction between uppercase and lowercase letters is not made. Examples:
| | |
| ------------- |:-------------:|
|`a = 3;`| 3 is assigned to a (no output)|
|`a`| result output|
|`b = -2.5` |-2,5 is assigned to b (with output)|
Note that Matlab uses a point to separate pre-decimal places and post-decimal places, following the conventions of the English language.
A major advantage of Matlab in comparison to a conventional programming language is the possibility to directly work with vectors. This concept shall only be touched briefly here, since it will be extensively treated in a separate chapter:
The single elements $C_{kl}$ of a matrix $C$ can be accessed by C(k,l). Here, $k$ denotes the $k$-th row and $l$ the $l$-th column of the matrix.
Interestingly, all variables that are used by Matlab are matrices, which is how the name is derived ("Matrix Laboratory").
The following table lists useful commands to manage variables.
|||
|---|---|
|`who`| List of all used variables|
|`whos`| List of all used variables with respective size|
|`clear`| Variable Delete a variable|
|`clear all`| Delete all variables|
|`load 'filename'`| Load variable(s) from a file|
|`save 'filename'`| variable Save variable to a file|
|`!`| execute a Linux/MSDOS-command|
Some specific variables are predefined:
|||
|---|---|
|`ans`| Last result|
|`pi`| $\pi$|
|`i`| Imaginary unit $\sqrt{-1}$|
|`Inf`| Infinity (z.B. $1/0$)|
|`NaN`| "Not-a-Number" (e.g. $0/0$)|
Note that the predefined variable can also be overwritten. Sometimes this happens unintendedly and leads to unpredictable results.
## Operations
The basic arithmetic operations like addition (+), subtraction (-), multiplication (*), division (/) and powers (^) are defined in a natural manner. Examples using the earlier defined variables are
`z = 10-8;` assigns the value $2$ to $z$
`t = b*[1;2;3];` assigns the scalar product $-6 = 0+6-12$ to $t$
The colon is used in Matlab to generate sequences of numbers, e.g
```matlab
x=1:10;
```
generates a row vector $\vec{x} = (1,2,\ldots,10)$, which holds the numbers from 1 to 10 in increments of 1. The increment can be set between the start and the end value:
```matlab
x=0:0.1:1;
```
The result is a row vector $\vec{x} = (0,0.1,\ldots,1)$.
The colon-operator is especially handy to access defined elements of vectors, e.g.
```matlab
x(3:6)
```
gives the elements $(0.2,0.3,0.4,0.5)$. When the colon-operator stands on its own, e.g. $\texttt{x(:)}$, all elements of the vector are referenced. This becomes especially interesting in the case of matrices. As a case in point we consider the matrix
from which we want to determine the first and the third column.
```matlab
A = [1,-1,2;3,0,1];
a = A(:,1);
b = A(:,3);
```
The result is $\vec{a} = \left(\begin{array}{c}1\\ 3\\\end{array}\right)$ and $\vec{b} = \left(\begin{array}{c}2\\ 1\\\end{array}\right)$.
## Mathematical Functions
Matlab provides a wealth of mathematical and numerical functions. All functions are applied to the whole vector respectively matrix. The following tables summarize the basic functions.
|`atan2(y,x)`| arc tangent of $y/x$ in the interval $[0,2\pi)$|
|`exp(x)`| exponential function|
|`log(x)`|, log10(x) natural logarithm, logarithm to the basis 10|
|`besselj(n,x)`| Bessel function $n$-th order|
|`rem(x,y)`| rest after division (z.B. rem(10.3,4) = 2.3)|
|`round(x)`| rounding (z.B. round(3.2) = 3)|
|`floor(x)`| round down (z.B. floor(3.2) = 3)|
|`ceil(x)`| round up (z.B. ceil(3.2) = 4)|
|`norm(x)`| norm (length) of a vector|
|`sum(x)`| sum over vector elements|
|`prod(x)`| product over vector elements|
|`max(x)`, `min(x)`| maximum, minimum of vector elements|
|`length(x)`| dimension of a vector|
|`mean(x)`| mean|
|`std(x)`| standard deviation|
|`size(A)`| dimension of the matrix $A$|
|`det(A)`| determinant of the matrix $A$|
|`rank(A)`| rank of the matrix $A$|
|`rand(N)`| uniformly distributed random numbers between $[0,1)$. $N\times N$ matrix|
|`randn(N)`| Gaussian distributed random numbers with mean = 0 and variance = 1.|
## Complex Numbers
An important feature of Matlab is that it provides a variable type for complex numbers. As long as it is not already used in another context, like a loop, the variable i is predefined as the complex unit. In general, complex numbers can be defined by invoking the command complex with two arguments, that represent the real- respectively the imaginary part of the number, e.g.:
In the imaginary plane, the complex number can be represented as a vector with a length amp, that starts in the origin and points in the direction phi. These quantities can be derived from the complex numbers through the commands abs and angle. The commands
Programming in Matlab is carried out via script files and function files. The filenames end in ".m", which is why these files are also named "M-files". You should not use blank spaces, umlauts or mathematical operators (e.g. -) in the file names. One should also avoid naming the files after a build-in Matlab command, since this command will then be overwritten.
Simply put, script and function files contain a list of commands. These commands will then simply be executed in succession. More complex program structures are realized with loops and bifurcations. Essentially, there are the following possibilities to control the flow of a program -- These constructs are found in every programming language, not only in Matlab.
Reoccurring operations are executed within loops. If it is desired to execute a specific set of commands $n$-times in a row, the use of a so called for-loop is advisable. As an example, we want to create a vector $\vec{p} = (p_1,p_2,p_3,p_4,p_5)$ with $p_k = k^2$, where $k=1,2,3,4,5$. The symbol $\texttt{\%}$ induces commentaries:
The result is a row vector $\vec{p} = (1,4,9,16,25)$.
Note that frequently, loops in Matlab can be replaced by the use of the colon-operator, here p = [1:5].^ 2. The latter is definitely preferable, since the execution is faster. In analogy to the colon-operator, an increment different form 1 can be set, e.g.
The difference between the the while-loop and the for-loop, where the number of times which the body of the loop is executed has to be known beforehand, is that the execution of the commands in the while-loop is tied to a condition: prior to every iteration this condition is tested. If the test turn out positive, the loop will be run through. Otherwise the loop will be left (or not even entered in the first place), and the commands following the loop will be executed. Example:
Conditional execution of commands is also mediated by if-expressions. The result of the bifurcation depends on the result of the result of the if-statement.
If $0 <x$and$x<1$,thensetstatus =1,elsesetstatus =0.Thecommandelsethusallowstoreacttoaunfulfilledcondition.Ifthisshallbecoupledtoafurthercondition,thecommandelseif(hastobewrittenasoneword)canbeused.
Here, the first number after the percent symbol sets the minimal total number of digits or characters that shall be printed. The second number is the desired number of post-decimal places.
example.dat is the name of the data being opened and 'w' indicates that we want to write something into the data (=write). Upon successful execution of the command, Matlab returns a so called file handle. This can be understood as a pointer when writing into the file, which tells the computer where the data is supposed to go.
Similar to printing to the screen, data can now be written into the file:
The read-in of formatted text works along the same lines; However, when using fopen, the option 'r' for reading of data (=read) has to be set, and for read-in, the command fscanf has to be utilized with a suitable format.
Data can also be written and read in a binary format. Here, the data of the computer is written into a file in its internal binary representation. An advantage of this type of data storage is its usually low memory demand. A drawback is that the files produced in this manner can not be opened and the contents inspected by a conventional text editor. In addition, it can occur that the data transfer between two computers with different machine architecture is hampered, in case they utilize different representations of floating-point numbers and integers.
The respective commands to read and write binary data are fread and fwrite. The files are opened and closed as usual via fopen and fclose.
Additionally, Matlab provides a third possibility to store data: the commands save and load. The syntax of these commands is
The variables with the names var1, var2 until varN are saved respectively loaded. The suffix of a Matlab-file should always be .mat. Loss-free compression and system-wide compatibility are the advantages of this method. Unfortunately, files created by different Matlab versions are sometimes incompatible.
Note: if no variable names are specified, all variables that momentarily reside in the working memory will be saved.
The following script file probes the orthogonality of two vectors $\vec{a}$ and $\vec{b}$, i.e. it is tested whether the scalar product of the two vectors is zero. Please try to understand the program with the help of the given commentaries.
Matlab provides an own editor, that facilitates the writing of programs. In our case, the script file would be named'orthog.m'. Given that the file is in the correct folder, the program can be invoked with the command orthog.
New, own functions can be defined as a function file. In the same way it is possible to superimpose existing implementations of Matlab-functions with own functions. Assuming that we want to implement the Gaussian bell-curve
The parameter $y$ is an output parameter, while $x$ and $\sigma$ are input parameters. Note that the respective file name only differs from the name of the function the file defines by the suffix '.m'. The function gauss is thus to be defined in 'gauss.m', nowhere else.
The self-defined function gauss will hence be used just like a built-in Matlab-function. For example with
the function can be plotted. Even the help functions help gauss and lookfor gauss work. Printed on the screen are in this case the comment lines preceding the first command in the file 'gauss.m'.
Naturally, new functions can also be provided with several output parameters. For this a vector-valued output parameter is used, which components hold the actual output parameters.
Self-defined functions can also be called within any script or function file. This can be used to structure programs into a main program and sub-program to increase the clarity and the readability of the code ( see e.g. fig.3.2. Conveniently, variables defined within a function are always local, i.e. unknown to other program parts. Hereby, unwanted 'side effects' are avoided.
Figure 3.2.: Schematic example of the subdivision of a program (Calculation and illustration of two oscillation modes of a physical system) into a main program and various sub-programs.
In many applications, one may want to not only give parameters to sub-programs, but also functions. The evaluation of the function passed to the sub-program is done with the command feval. Consider an example of a sub-program which plots a given function in a predefined interval including annotation of the axes, etc:
Recursive functions constitute an elegant method to integrate iterative calculations in a simple function expression, that is capable of solving complex tasks. A recursive function calls itself during the sequence of execution of its program code. As can easily be guessed, programming recursive functions involves the risk that a function calls itself over and over again, but never returns to the calling program. Thus, a suiting termination criterion has to be provided, that ensures that after a specific iteration, no further recursion is initiated.
As an example we consider a program implementing a nested intervals method to evaluate the square root of a number x. We begin with an interval in which the sought for square root will certainly be, thus for example $[0,x]$. Now we divide the interval in half and check in which half the square root has to be. This half we take as the new interval and call the function anew. The procedure is repeated until the interval is smaller than a given numerical accuracy and the square root is thus determined with sufficient precision: