Overview

Course Handout: (last update 27 January 2014)


These notes may be found at http://www.dartmouth.edu/~rc/classes/intro_matlab. The online version has many links to additional information and may be more up to date than the printed notes

Introduction to Matlab


Covered in this class:
Click here to download the examples used in the class.





Table of Contents

1.Resources for Learning Matlab
2.MATLAB Desktop
3.MATLAB Help
4.Command Line Calculations
5.Working With Matrices
6.Entering Matrices
7.Matrix Operations
8.Review of Matrix Multiplication
9.Array Multiplication
10.Matrix and Array Operators
11.Array Operations
12.Matrix Indexing
13.Array Subscripting and Indexing
14.String Arrays
15.Loading and Saving Data
16.Import, Analyze and Plot Data
17.2D Plotting
18.Matlab Subplots
19.Alternative Scales for Axes
20.Specialized Plotting Routines
21.3-D Surface Plotting
22.Matlab Resources

(1)

Resources for Learning Matlab


(2)

MATLAB Desktop






Getting Help in MATLAB





(4)

Calculations at the Command Line

Try:

>> -5/(3.45+2.75)^2
ans =
-0.1301
>> (2+6i)*(2-6i)
ans =
40
>> sin(pi/4)
ans =
0.7071
>> exp(acos(0.3))
ans=3.547
>> a= factorial(12)
>> b= a* cos(.76);
>> b

Useful keys to remember:

;                       Suppress the output of results to the screen
up-arrow         scroll through previously typed commands
???up-arrow     scroll through commands beginning with ??? (command completion)
Esc                  Clear command line
Ctrl+C            Quit current operation and return control to the command line











(5)

 Working With Matrices






(6)

Entering Matrices

Square Brackets
Use of Colons
Parentheses
   
Try:
>> x=[1 5; 10 15]
x =
       1      5
       10     15

>> y  = [ 4,5,6; 7,8,9]
y =  
      4     5     6
      7      8    9

>>  z= [ .4   cos(pi/3)  6/7^2]
z =
       0.400     0.500      0.1224

>> z(2,3) =.9
z =
      0.400     0.500      0.1224
              0            0      0.9000


     
Try:
>> t= 0:2:10
t =
      0   2   4   6   8   10

>>v =20:25
v =
      20 21 22 23 24 25


(7)

 Matrix Operations


Try:
>> a=[2 4; 6 8] +10
a  =
          12     14
          16      18
>>  b = [2 4; 6 8] * 3
b =
            6      12
           18      24


Try:
>> c = [1 2; 3 4]
c =
            1      2
            3      4

>> cc = [ c c]
cc =
            1      2       1        2
            3      4       3        4

Try:
>>  a=[2 4; 6 8];
>>  b = [ 1 3; 5 7];
>> c= a+b
c   =          
                    3      7
                   11     15
>> d = a*b
d    =  
                   22     34
                   46     74


>> e = a.*b
e   =  
                   2     12
                  30     56


>> f = a^2
f       =
                  28     40
                  60     80


>> g= a.^2
g        =
                   4     16
                  36     64




(8)

A Review of Matrix Multiplication


Rules of Matrix Multiplication

Matrix multiplication example

Try:
>> a  = [  1 2 3; 4 5 6]
a =
        1      2      3
        4      5      6
>>  b= ones (3,3)
b =
         1      1      1
         1      1      1
         1      1      1
>>  c= a*b
c  =
          6       6       6
         15      15      15






(9)

Array Multiplication (.*)


Rules of Array Multiplication

Example of Array multiplication

Try:
>> a = [ 1 2 3 ; 4 5 6]
a =
      1   2    3
      4   5    6
>> b=[1:3; 1:3]
b =
       1    2    3
       1    2    3
>> c = a.*b
 c =
       1      4     9
       4     10    18






(10)

Matrix and Array Operators (listed in order of precedence)



Matrix Operators
Array Operators
( ) parentheses

'   complex conjugate transpose
.' array transpose
^  raise to a  power
.^ array power
* multiplication
.* multiplication
/ division
./ array division
\ left division

+ addition

- subtraction





(11)

Array Operations


Matlab uses matrix calculations
You do not have to use loops to perform matrix calculations

Example:  if you have 10000 samples of a,b  and c = a*b

Most efficient matrix command:
>> c= a.*b;

Less efficient to use a loop
>> for i=1:10000
            c(i)= a(i)*b(i);


Try:
>> array_examp



(12)

Indexing into a Matrix (Array) in MATLAB


Two different ways of indexing:
Example of a matrix

Try:
>> first_mat
>> A(2,4)
ans =
          4
>>A(17)
ans  =
           4



(13)

More Array Subscripting and Indexing in MATLAB



Array indices must be integers and can refer to:

Example of Matrix Indexing

Try:
>> first_mat
>> A([ 1 7 2; 3 6 2])

ans =  4.000      1.200      8.000
         7.2          10.0000    8.000

>> A(1:5,5)
>> A(:,5)
>> A(21:25)
>> A(1:end,end)
>> A(:,end)
>>A(21:end)'
>> A(4:5,2:3)
>>A([9 14; 10 15])

>>B= find(A>5)
>> C=size(A)


Useful Subscripting commands:













(14)

String Arrays




Try:
>>str1='Hi there.   '
str2='How are you?'
str3= 'Bye'
c1= [str1,' ',str2]  % join two strings
c2 = [str1;str2]  % vertical concatenation (must be same length)
c3=strvcat(str1,str2,str3)   % vertically concatenate (matrix)











(15)

Load and Save Functions



save
save filename
save filename x y z
load filename data*
save filename -ascii


load
load filename
load filename x y z
load filename data*
load filename


Try:
>>clear
>>a= rand(3,2);
b=ones(3,2);
c = a.^2+b;
save mydata
clear

>>load mydata
save -ascii mydata_ascii
clear

>> load mydata_ascii






(16)

Import, Analyze and Plot Data


See gasprices.m

(17)

2-D Plotting



Plotting commands:

Try:

>> plot2D

plot of blue sine wave
echo on
x = 0:0.1:2*pi;
y=sin(x);
plot(x,y)
grid on
hold on
plot(x,exp(-x),'r:*');
axis ([ 0 2*pi 0 1])
title('2-D Plots');
xlabel('Time');
ylabel('Sin(t)');
text(pi/3,sin(pi/3),'<--sin(\pi/3)')
legend('Sine Wave','Decaying Exponential');
echo off









 Subplots



subplot - display multiple plots in the same window
subplot (nrows,ncols,plot_number)


Try:
>> subplotex

 

picture of 4 plots made using subplot command

x=0:.1:2*pi;
subplot(2,2,1);
plot(x,sin(x));
subplot(2,2,2);
plot(x,cos(x));
subplot(2,2,3)
plot(x,exp(-x));
subplot(2,2,4);
plot(peaks);









(19)

Alternative Scale for Axes




try:
>> other_axes

Pick of Plots With Laternative Axes











(20)

Specialized Plotting Routines



Picture of Matlab Special Plots 

try: spec_plots















(21)

3-D Surface Plotting



Examples of 3D surface plots

Try:
surf_3d



















Matlab Resources


Dartmouth Matlab Site License
   Dartmouth Matlab Download
   http://caligari.dartmouth.edu/downloads/matlab

   Support:
  Matlab Tutorial Videos on the Web:

Mathworks Intro to Matlab Video:
http://www.mathworks.com/company/events/webinars/wbnr31351.html?id=31351&p1=961663781&p2=961663799

List of Mathworks Tutorial Webinars:
http://www.mathworks.com/company/events/webinars/index.html









Overview: Course Handout
[an error occurred while processing this directive] (last update   27 January 2014)  ©Dartmouth College     http://www.dartmouth.edu/~rc/classes/intro_matlab