Pty 302 L1 - Spring - 2018
Pty 302 L1 - Spring - 2018
• Lectures
¾ 1: Variables, Scripts and Operations
¾ 2: Visualization and Programming
¾ 3: Solving Equations, Fitting
¾ 4: Images, Animations, Advanced Methods
¾ 5: Optional: Symbolic Math, Simulink
Lecture 1: Variables, Scripts,
and Operations
Outline
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
USING HELP
datestr
Convert date and time to string format
Syntax
DateStringArray = datestr(DateVectorArray)
DateStringArray = datestr(DateNumberArray)
DateStringArray = datestr(DateTimeArray, FieldSpecOut)
DateStringArray2 = datestr(DateStringArray1, FieldSpecOut, PivotYear)
DateStringArray2 = datestr(datenum(DateStringArray1, FieldSpecIn), FieldSpecOut)
DateStringArray = datestr(..., 'local'
------------------------------------------------------------
Examples :
1. Convert date vector v to a date string:
v = [2009, 4, 2, 11, 7, 18];
datestr(v)
ans =
02-Apr-2009 11:07:18
2. Return the current date and time in a string using the default format, 0:
datestr(now)
ans =
28-Mar-2005 15:36:23
3. Format the current date in the mm/dd/yy format. Note that you can specify this format either by number or by string.
datestr(now, 2) -or- datestr(now, 'mm/dd/yy')
ans =
03/28/05
Exercise: Variables
» load startTime
» disp(['I started learning MATLAB on ' ...
startString]);
Outline
• Exponentiation (^)
» 4^2
» (3+4*j)^2
floor ceil
Round to nearest integer
Round toward negative
Round toward positive infinity Syntax
infinity
Syntax Y = round(X)
Syntax
B = floor(A) B = ceil(A)
Exercise: Scalars
» 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
>> A = rand(5)
>> A(1:3,1:2)
A=
ans =
0.4898 0.2760 0.4984 0.7513 0.9593
0.4898 0.2760
0.4456 0.6797 0.9597 0.2551 0.5472
0.4456 0.6797
0.6463 0.6551 0.3404 0.5060 0.1386
0.6463 0.6551
0.7094 0.1626 0.5853 0.6991 0.1493
ans =
0.4898 0.7513
0.7547 0.8909
0.6463 0.5060
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
• 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