Harvard Wiki has been integrated with Group Services.
Wiki administrators: visit IT Help for an overview of the changes to managing groups in your wikis.
Skip to end of metadata
Go to start of metadata

Overview

This is a simple tutorial to get you started with Matlab. Matlab makes it easy to perform scientific computations without having to learn a programming language such as Fortran, C, or C++. This tutorial is developed keeping assignment_0.pdf for AM205 in mind. It can be used for other classes or self-learning as well.

Topics covered:

  • Matlab Basics: start up, simple commands, Matlab as calculator
  • Matlab Variables: Vectors and Matrices
  • Linear Equations
  • Simple programming: control of flow, functions, m-files
  • Plotting

References

You should make use of online search tools such as google to search for relevant matlab material. There is a lot of material on the web. Additional resources include:

Using Matlab

Opening Matlab:

This depends on your operating system. On Mac, it is usually installed in /Applications and you should see a Matlab icon with a MATLAB_<version number>. Double clicking the icon should open it. On Windows, there should either be a shortcut on the desktop or it can be found under programs in the start-up.

Matlab Development Environment (Matlab Desktop):

This can change with new releases. The following video gives a quick introduction: 

http://www.mathworks.com/videos/working-in-the-development-environment-69021.html?s_cid=learn_vid

The key items are "Command Window" (where you type commands in this tutorial), "Current Folder", and the "Workspace".

Getting Help

Note: In Matlab, any line beginning with % is a comment (not executed by matlab).  Lines without % are commands that are executed by matlab. You can copy and paste the matlab statements in this tutorial into matlab

Open matlab and in the command window, type:

This will open a separate window with documentation and a search box. There are other ways! For example, if you want to find out more about a specific command, you can do:

Preliminaries

 For space reasons, the results are not shown in the code block unless necessary.

Arrays

All matlab variables are arrays. Even single numbers are arrays of dimension 1. Below, we start experimenting with some arrays.

Element-wise operations on an array

Quick plotting

More on plotting later.

Vectors

Example – Dot product, angle between vectors:

(compare)

use:

format long

Matrices

Matrix example (Linear Equations)

We now solve the linear equation:

  2x1+4x2+3x3=4
    x1-2x2-2x3=0
-3x1+3x2+2x3=-7

Ax=B

For loops

For loop:

We will later consider the control of flow within a loop under functions.

More advanced topics

Plotting revisited

 complex plots (plot annotations, axis labels, etc.)

From: http://www.mathworks.com/help/techdoc/ref/plot.html

 

Some 3D plotting

Meshgrid:

 

Sinc function (https://en.wikipedia.org/wiki/Sinc_function)

Another example:

m-FILES (SCRIPTS AND FUNCTIONS)

Scripts:

A collection of matlab commands in a file named <name>.m.
Example: example.m

To run the script, save the script file (say example.m) in a directory accessible by matlab and just type the script name (without .m) in the command window. In this case it will be 'example'.

 

Functions:

Matlab functions are callable sub-procedures that are
put in a file named <function_name>.m

Example function (stat.m from matlab documentation) is as follows:

Put the above function in a file called stat.m in a directory which is in matlab's path. You can use this function as below:

More functions

The following are some functions relevant to assignment_0.pdf of AM205.

They are meant to get you started with the problems, but they are not "complete solutions".They can be useful for learning functions in matlab for other classes as well.

Function (cheby.m):

Function: sinTaylorSeries.m

Function: tanderiv.m

Image Processing (Quick Introduction)

Here are the images we will be using: baboon.png and Brain-MRI.jpg

Grayscale Images:


Additional tutorial material: fwdmatlabtutorialsetc.zip