## Matlab/Octave Examples

#### Example 1.

Begin by opening Matlab/Octave. The window that appears first by default is referred to as the "command window". Lets create two column vectors "a" and "b" and define a scalar "s" by typing in the command window "a=[1 ; 2]", "b=[3 ; 4]", and "s=5".

Scalar Multiplication.
Simply write, for example, "a*s".

Vector Multiplication.
To multiply "a" and "b", we need to transpose one of the vectors, say b. We may do so by defining the row vector "c=[3 4]". Or we can simply write "b' ". Then we can either write "a*c" or "a*b' " to get the same result.

Element-Wise Multiplication.
To multiply the vectors "a" and "b" element-wise, write "a.*b". Using the dot in front of the operators evokes an element-wise operation in general.

You can look at screenshots of example 1 using Matlab and using Octave.

#### Example 2.

Let's look at a tiny program example2.m. It helps to create a directory where you store all your m-files.

To run the m-files that you have stored in that directory, you need to make sure that Matlab/Octave can find them. Type in "addpath" and the directory that you created. For example, "addpath C:\macro" (or "addpath /Users/computername/macro" on a Mac). Then you can just type in the name of the m-file (e.g. "example2"), hit enter, and Matlab/Octave will execute the program.

#### Editors.

Matlab as well as Octave for Windows come along with their own editor. To open the file that you saved in the prespecified directory, simply type in, for example, "edit example2". Then "example2" will open in the editor. You can, of course, modify m-files in any editor you wish to use; they are merely text documents.

In Linux, try typing "edit example2" into Octave's command window. By default, it will wish to use Emacs. If you like, just install Emacs and it will be used in the future by Octave. You can also tell Octave which editor to use by writing " edit editor "myfavoriteeditor %s" " into the command window and replacing myfavoriteeditor with the name of your favorite editor. Gedit should be preinstalled on Linux, so if you type " edit editor "gedit %s" ", you should be good to go. Gedit has syntax highlighting for Matlab/Octave which is quite nice.
If you like to change your personal settings so that Octave always uses Gedit, write " edit editor "gedit %s" " in a text file and save it in your home directory.
To find the errors that you make, it's good to see the line numbers: Edit -> Preferences -> View -> Display line numbers
You can also use the terminal from within Gedit: Edit -> Bottom Pane. However, you may need to enable a plugin first.

Gedit is also available for MacOSX.

#### Hints.

It is considered good Matlab programming practice to end every line with a semicolon. In case you want to know what happens in every single step, remove the semicolons, save the m-file, and run the program again. This way, every step will be displayed in the Matlab/Octave command window.

Given that we defined entries such as "z.firm=firm", it is useful to call the program in the command window by writing, for example, "a=example2". Then we can call the entries by typing "a.firm" into the command window, once we have executed the program in Matlab/Octave.

If you don't know what, for example, the function "rand" does, type "help rand" in the command window and Matlab/Octave will return a description of the particular function.

"close (all)": close (all) figures.
"cd C:\macro": switch to another directory
"pwd": display the current directory you are in.
": to get rid off everything you had asigned (e.g. that "a" no longer refers to "example2").