Learn MATLAB
Learn MATLAB
094
Introduction to Programming in MATLAB
Danilo Šćepanović
IAP 2010
Course Layout
• Lectures
¾ 1: Variables, Scripts and Operations
¾ 2: Visualization and Programming
¾ 3: Solving Equations, Fitting
¾ 4: Images, Animations, Advanced Methods
¾ 5: Optional: Symbolic Math, Simulink
Course Layout
• Prerequisites
¾ Basic familiarity with programming
¾ Basic linear algebra, differential equations, and
probability
Outline
• On Athena
» add matlab
» matlab &
Current directory
Workspace
Command Window
Command History
• Click the ‘Make New Folder’ button, and change the name of the
folder. Do NOT use spaces in folder names. In the MATLAB
folder, make two new folders: IAPMATLAB\day1
• help
¾ The most important function for learning MATLAB on
your own
• To get info on how to use a function:
» help sin
¾ Help lists related functions at the bottom and links to
the doc
• To get a nicer version of help with examples and easy-to-
read descriptions:
» doc sin
• To search for a function by specifying keywords:
» doc + Search tab
Outline
• Scripts are
¾ collection of commands executed in sequence
¾ written in the MATLAB editor
¾ saved as MATLAB files (.m extension)
Help file
Comments
• COMMENT!
¾ Anything following a % is seen as a comment
¾ The first contiguous comment becomes the script's help file
¾ Comment thoroughly to avoid wasting time later
» disp('Hello World!');
» disp('I am going to learn MATLAB!');
Outline
• Variable names
¾ first character must be a LETTER
¾ after that, any combination of letters, numbers and _
¾ CASE SENSITIVE! (var1 is different from Var1)
• Command window:
• Workspace:
• Command window:
• Workspace:
⎡1 2⎤
• Element by element a=⎢ ⎥
» a= [1 2;3 4]; ⎣3 4 ⎦
» d = [a;b];
» e = [d c];
» f = [[e e];[a b a]];
» str = ['Hello, I am ' 'John'];
¾ Strings are character vectors
save/clear/load
• Use save to save variables to a file
» save myFile a b
¾ saves variables a and b to the file myfile.mat
¾ myfile.mat file is saved in the current directory
¾ Default working directory is
» \MATLAB
¾ Make sure you’re in the desired folder when saving files. Right
now, we should be in:
» MATLAB\IAPMATLAB\day1
» help clock
» start=clock;
» size(start)
» help datestr
» startString=datestr(start);
» save startTime start startString
Exercise: Variables
» load startTime
» disp(['I started learning MATLAB on ' ...
startString]);
Outline
• Exponentiation (^)
» 4^2
» (3+4*j)^2
» secPerDay=60*60*24;
» tau=1.5*secPerDay;
» endOfClass=5*secPerDay
» knowledgeAtEnd=1-exp(-endOfClass/tau);
» disp(['At the end of 6.094, I will know ' ...
num2str(knowledgeAtEnd*100) '% of MATLAB'])
Transpose
• For vectors of real numbers .' and ' give same result
Addition and Subtraction
⎡ 4⎤ ⎡1 1 1 ⎤ ⎡1 2 3⎤ ⎡ 1 2 3⎤
⎢ 2 2 2 ⎥ .* ⎢1 2 3⎥ = ⎢ 2 4 6 ⎥
[1 2 3] .* ⎢⎢ 2⎥⎥ = ERROR ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎣ 1 ⎥⎦ ⎢⎣ 3 3 3 ⎥⎦ ⎢⎣1 2 3⎥⎦ ⎢⎣ 3 6 9 ⎥⎦
⎡1 ⎤ ⎡ 4⎤ ⎡ 4⎤ 3 × 3.* 3 × 3 = 3 × 3
⎢ 2 ⎥ .* ⎢ 2 ⎥ = ⎢ 4 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎣ 3 ⎥⎦ ⎢⎣1 ⎥⎦ ⎢⎣ 3 ⎥⎦
⎡1 2 ⎤ ⎡12 22 ⎤
3 ×1.* 3 ×1 = 3 ×1 ⎢3 4 ⎥ .^ 2 = ⎢ 2 2⎥
⎣ ⎦ ⎣ 3 4 ⎦
Can be any dimension
Operators: standard
⎡ 4⎤ ⎡1 2 ⎤ ⎡1 2 ⎤ ⎡1 2 ⎤ ⎡1 1 1⎤ ⎡1 2 3⎤ ⎡3 6 9 ⎤
[1 2 3]* ⎢⎢ 2⎥⎥ = 11 ⎢3 4 ⎥ ^ 2 = ⎢3 4 ⎥ * ⎢ 3 4 ⎥
⎣ ⎦ ⎣ ⎦ ⎣ ⎦
⎢2 2 2⎥ * ⎢1 2 3⎥ = ⎢6 12 18 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢⎣1 ⎥⎦ Must be square to do powers ⎢⎣3 3 3⎥⎦ ⎢⎣1 2 3⎥⎦ ⎢⎣9 18 27⎥⎦
1× 3* 3 ×1 = 1×1 3 × 3* 3 × 3 = 3 × 3
Exercise: Vector Operations
» secPerMin=60;
» secPerHour=60*secPerMin;
» secPerDay=24*secPerHour;
» secPerMonth=30.5*secPerDay;
» secPerYear=12*secPerMonth;
» secondConversion=[secPerYear secPerMonth ...
secPerDay secPerHour secPerMin 1];
» currentTime=clock;
» elapsedTime=currentTime-start;
» t=secondConversion*elapsedTime';
Exercise: Vector Operations
» currentKnowledge=1-exp(-t/tau);
» disp(['At this time, I know ' ...
num2str(currentKnowledge*100) '% of MATLAB']);
Automatic Initialization
• Picking submatrices
» A = rand(5) % shorthand for 5x5 matrix
» A(1:3,1:2) % specify contiguous submatrix
» A([1 5 3], [1 4]) % specify rows and columns
Advanced Indexing 1
⎡12 5 ⎤
c=⎢ ⎥
⎣ −2 13 ⎦
» d=c(1,:); d=[12 5];
» e=c(:,2); e=[5;13];
» c(2,:)=[3 6]; %replaces second row of c
Advanced Indexing 2
» [val,ind]=min(abs(knowledgeVec-0.5));
» halfTime=tVec(ind);
» disp(['I will know half of MATLAB after ' ...
num2str(halfTime/secPerDay) ' days']);
Outline
• Example
» x=linspace(0,4*pi,10);
» y=sin(x);
1 1
10 x values: 0.8
0.6
1000 x values:
0.8
0.6
0.4 0.4
0.2 0.2
0 0
-0.2 -0.2
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
-1 -1
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14
Exercise: Plotting
» figure
» plot(tVec/secPerDay, knowledgeVec);
End of Lecture 1
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.