FF505 - Computational Science - MATLAB Section

Introduction

This short note aims at giving you directions for the installation of Matlab in your computer and at getting you started with the Matlab environment.

Installing Matlab

In BlackBoard go to Software, and then to MATLAB & Simulink. Copy the clipboard the activation key (it will be valid until March, hence in March you will have to redo this step). Follow the link to the "guide for students". Follow the instructions for installation from there. We will need only the MatLab 8.6 package. However, you are welcome to download and explore other packages like, for example, Simulink.

Starting Matlab

  1. Start Matlab. If you are on a Windows systems, Matlab should be under the "Start" menu or often there is a shortcut on your desktop. On a Linux/UNIX system, open a terminal and type matlab &. In a Linux with Unity press alt and the bar together to get a search dialogue where to type matlab. On a Mac, double click on the "MATLAB R2015b" icon located in your Matlab directory in Applications.
  2. No matter what system you are on, once Matlab starts, you will get exactly the same thing. In particular, you will get the Matlab Desktop made of the Command Window, and probably several other Matlab windows like Workspace and History.

Command Basics

  1. The Matlab command window is the center of the Matlab universe. The job of the command window is to sit there blinking, waiting for you to type something. Your job is to enter commands that tell Matlab what to do. This is very much like working from a Linux or DOS command prompt.
  2. Try a few commands, eg, 1+1 or some more complicated expressions. Matlab interprets parentheses in the usual, algebraic way. Compare the answers for 10-2*3 vs. (10-2)*3. What's different and why?
  3. Now, let's create some variables. Type ten=10 and hit return. You can now enter ten instead of 10 in the expressions above. Try it. Matlab has a few built-in constants like pi (3.1416).
  4. Variables like ten and pi are known as scalar variables. A more interesting class of variables are arrays. Enter arr1d=[1 5 10] at the command prompt. You have now created a table of numbers with 1 row and 3 columns and given it the name arr1d. To get numbers out of arr1d you use parentheses: arr1d (3) should return 10. As its name implies, arr1d is a one-dimensional array. 1D arrays are also known as vectors, and vectors can be both row vectors (like arr1d) or column vectors. To turn arr1d into a column vector, type arr1d=arr1d' ; The ; prevents Matlab from printing the output, this is useful for big arrays and if you already know the answer. To verify that arr1d is now a column, just type its name to see it printed.
  1. Let's create a 2D array: arr2d=[1 2 3; 9 8 7]. The semicolon ";" tells Matlab to start a new row. To access data in arr2d, we need to use two numbers, e.g. arr2d(j,k), where j specifies the row and k specifies the column. What do you need to type to access the 8 in arr2d? Try replacing the 8 with –8.
  2. In a typical Matlab session, you will create several variables, and it is important to know what variables are in your workspace and their sizes. To list the contents of your workspace, type whos. If you have a variable that you no longer need, you can delete the variable by typing clear = followed by the name of the variable (or variables you want to delete). Type =clear ten and then whos to make sure ten is gone. Type clear to clear everything.
  1. Quit Matlab by typing in the command window exit or quit.

Tutorials

In class you will be provided with a few basic concepts and you will be posed guiding exercises. Learning Matlab is personal enterprise. You should now continue learning with the following resources:

Printing source code

You should try to avoid printing code since you will not gain much from reading it in black and white on a paper rather than with color highlights on the screen. However, if you really need you can print source code by using the matlab editor or some text processing software. For a good result, consider using the command a2ps at your terminal prompt:

a2ps -Pd3 hello.m

a2ps is an abbreviation for "Anything to PostScript" and -Pd3 specifies that you want to use the printer "d3" outside the terminal room.

Date: 2016-01-14T14:05+0100

Author: marco

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0