Introduction

Matlab is a rapid development enviroment for solving numerical and engineering problems. We strongly suggest you use MATLAB to do your assignments in CS450. It will make your life much easier, especially on the coming homework assignments.

For a good tutorial on how to get started on MATLAB, this is a good reference you might want to walk through.

You can download a copy of the handout I've been providing to people here.

Here is how I suggest you do your assigments:

  • Type all of your commands in an M-file (select new->M-file). Never use the command window unless you are looking up help or figuring out a feature you don't understand
  • Put a ';' behind every command
  • Run the script you are writing after every change so you know if you've broken something
  • Put all your plotting commands in the script
  • To debug, remove ';' from lines to view their output
  • Once you are finished, print your script file and all the plots it generated.

If you follow the above and give us the results, we will have everything we will need for every assignment.

Lab notes

The rest of this page contains the simple tutorial that I went through on 2007-01-25. By reading these comments and pasting these commands into your window, you should begin to get an idea of how you can get started using MATLAB.

% This is a MATLAB comment.  Anything after this comment is not
% interpreted by matlab.  Try pasting some of these lines into your 
% command window now.

% In the command window, you can press up to scroll through your previous commands.
% you can also drag and drop commands from your history window on the side into the 
% command window.

% When trying to learn MATLAB, remember these two tips:
% 1.) The documentation is wonderful
help plot
% 2.) Try it out, figure out how it works
plot([0:0.1:1], [0:0.2:2])

% How to create/use arrays, the ':' operator
% arrays go in brackets
row = [ 1 2 3 4 ]
column = [1; 2; 3; 4]
row + column    % error, dimensions don't match
row' % ' ==  transpose
row'+column
mat = [11 12; 21 22; 31 32; 41 42]

% Indexing:
% MATLAB's indexing begins at 1
mat(1,2)
row(1,3)

% For vectors, you can use a simpler notation:
row(3)
row(0) % error, indexing begins at 1

% There are plenty of utility routines that let you create
% default vectors and matrices
zeros(4,3)
eye(4,4)
zeros(4) % not what you expect, be careful!
zeros(4,1)

% you can also get the size of matricies and vectors.  
% This will be important in later assignments.
size(mat)
size(row)
length(row)

% : is the range operator
mat
mat(2:3, 1)
mat(:, 1)
mat(2:4, :)

% The format is start:stop.  If start=beginning and stop=end, then you can emit both.

% The : operator can also be used to make quick arrays.  
2:6

% you can also specify a skiP: start:skip:stop
% if you leave out the skip, 1 is assumed.

0:3:9
5:-1:1
[5:-1:1]' % quick column
% you can also use linspace to achieve the same effect.
linspace(0, 1, 11)

% Arithmetic operations

2+2
exp(1)
sqrt(2)

% basic functions also work on matrices
sqrt(column)
10*exp(column)+sqrt(column)

mat
mat(:, 1)+column

% a . infront of an operation means that it is an element by element operation.
% this is most important for ^, \, and *
row*column
mat(:, 1) .* column

column^2
column.^2

% Plotting, the hold command, labels, complex graphics
x = linspace(0, 1, 100)'
y = sqrt(x)
plot(x, y)
plot(x, y, '.')
title "Title"
title 'Title'
xlabel 'dependent'
ylabel 'independent'
axis([0 1.5 0 2])

% by default, another plot command rewrites the current figure
plot(x, x.^2)

% to plot two or more things at once, use the hold command
hold on
plot(x, y)
hold off

% Scripting 
x
x;  % ; suppresses output.  Always use in scripts!
    % That way, you can remove ; to see intermediate products 
x + ... %line continuation
x

% Functions (coming soon)
global n
n=4;
rob_plot(@sqrt, 0, 1, '.')
hold on
n=32;
rob_plot(@sqrt, 0, 1, 'o')
hold off
Topic revision: r3 - 24 Apr 2008 - 16:46:09 - RobBlake
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback