Instructor: Xiaofeng Zhou
Introduction to MATLAB
◦ MATLAB Environment
◦ Constants
◦ Variables and Assignment Statements
◦ Operators
Built-in function
Script File/M-File
@X.Zhou
Intro to Analytical Programming
2
What is MATLAB?
◦ Short for MATrix LABoratory
◦ Capable of solving any technical problems: Very powerful software
package
◦ Typical uses:
Data analysis, exploration and visualization, Scientific and engineering graphics,
simulation, Computation, Algorithm development, Modeling, etc.
◦ https://www.mathworks.com/discovery/what-is-matlab.html
◦ https://www.mathworks.com/products/matlab.html
Many built-in functions
Programming constructs
Many mathematical and graphical applications
MATLAB computing environment:
◦ Can use interactively in the Command Window, or write your own programs
The company: Mathworks
◦ www.mathworks.com
@X.Zhou
Intro to Analytical Programming
3
MATLAB
◦ User: engineers and scientists
◦ Analyze data, develop algorithms, construct models
◦ https://www.mathworks.com/products/matlab.htm
l?s_tid=hp_ff_p_matlab
Simulink
◦ Design and simulate your system in Simulink
before moving to hardware.
◦ https://www.mathworks.com/products/simulink.ht
ml?s_tid=hp_ff_p_simulink
◦ Not covered in this course
@X.Zhou
Intro to Analytical Programming
4
MATLAB for Student
◦ https://www.mathworks.com/products/matlab/st
udent.html?s_tid=hp_ff_p_student
Learn with MATLAB tutorials
◦ https://www.mathworks.com/support/learnwith-matlab-tutorials.html
Course and Training
◦ Self-Paced Online Courses - MATLAB & Simulink
◦ MATLAB Fundamentals - MATLAB & Simulink
@X.Zhou
Intro to Analytical Programming
5
MATLAB for York University:
◦ https://www.mathworks.com/academia/tahportal/york-university-40713236.html
◦ Register account with your YORKU Email
◦ Download and install to your local drive
◦ List of available product:
https://www.mathworks.com/products.html
Alternatively, using MATLAB online:
◦ https://www.mathworks.com/products/matlabonline.html
@X.Zhou
Intro to Analytical Programming
6
https://matlab.info.yorku.ca/
@X.Zhou
Intro to Analytical Programming
7
Change your current folder
Current
folder
Current
Workspace
@X.Zhou
Command window
Intro to Analytical Programming
8
Start MATLAB
>>: prompt
Example:
m = 10
n = 100
m+n
Textbook: Intro with
App
◦ Page 12: Tutorial 1-1
@X.Zhou
Intro to Analytical Programming
9
pi: , 3.1415926…
i/j:
−1
inf: infinity
NaN: stands for “not a number”, such as 0/0
e: known as Euler's number
◦ built function exp()
eps:
◦ Double precision: 2^(-52)
@X.Zhou
Intro to Analytical Programming
10
Variable name
rules
◦ must begins with a letter, followed by any combination of
letters, digits and underscores. No space allowed.
◦ Case sensitive
Age and age are two variables
◦ Reserved words/key words/names can not be used as
variable name
◦ Avoid use the names of built-in function
◦ Be mnemonic
◦ Maximum length: 63 (R2012a): namelengthmax
Create variable: assignment statement
◦ Format: variable name = expression
Assignment operator
◦ If a variable already exists, a variable‘s value is over
written when assign a value
@X.Zhou
Intro to Analytical Programming
11
Putting a semicolon at end of a statement
suppresses the output
ans: default variable name used by MATLAB if
an expression is typed at the prompt and it is
not assigned to a variable
Retyping command:
The Workspace Window shows the variables that
have been created in the current Command Window
and their values
The following commands relate to variables:
◦ who , whos ,clear all , clear variable list
@X.Zhou
Intro to Analytical Programming
12
Every expression, or variable, has a type associated
with it: Class
For float or real numbers: single and double
◦ MATLAB uses a floating-point representation
For integers: int8, int16, int32 and int64
For unsigned integers: uint8, uint16, uint32, and
uint64
◦ Can only be positive number or 0
The range can be found using intmin(‘ ’) and intmax(‘ ‘)
The type char is used to store either single characters
or strings, strings are sequences of characters. Both
characters and strings are enclosed in single quotes.
The type logical is used to store true/false values
@X.Zhou
Intro to Analytical Programming
13
Expressions can be created using values,
variables, operators, built-in functions,
parentheses
◦ Example: x= a + b * 3 – max (n, m)
◦ Note: a, b, n, and m have to be assigned value before
use
The result will be displayed with 4 decimal
places if it has decimal point by default
Numerical value: scientific or exponential
notation
◦ 20000 -> 2e4
@X.Zhou
Intro to Analytical Programming
14
format long: change the format to long, i.e.
15 decimal places
format short: change the format to short, i.e.
4 decimal places
format loose (the default) and format
compact are used to control the spacing
between the MATLAB command/expression
and result
@X.Zhou
Intro to Analytical Programming
15
Two types: unary and operand/binary
Math operators
◦ +, -, *, /, \, ^
Relational operators
◦ <, >, <=, >=, ==, ~=
Logical operators
◦ ~ not, || or, && and; & and, | or (elementwise) (to be
introduced in next class)
Function: xor, is the exclusive or function. It
returns logical true if one (and only one) of the
arguments is true.
Continuation operator/ellipsis
◦ Three or more period
◦ Example: >> 1 + 2 + 3 + 4 + 5 …
+ 6 +7
@X.Zhou
Intro to Analytical Programming
16
@X.Zhou
Intro to Analytical Programming
17
Associativity : Within a given precedence level, the
expressions are evaluated from left to right
Nested parentheses are parentheses inside of others;
the expression in the inner parentheses is evaluated
first
Precedence
@X.Zhou
Intro to Analytical Programming
18
There are many built-in functions
help can be used to identify MATLAB functions
Example: >>help elfun
@X.Zhou
Intro to Analytical Programming
19
Call a function
◦ Function name(argument/arguments)
◦ Example: abs(-4)
Commonly used functions
◦ sin( )/sind(), abs( ), fix( ), floor( ), ceil( ), round( ),
rem( ), sign( ), log(), log2(), log10(), exp()
◦ Use help documentation to find the functionality
of above built-in functions
Textbook: Intro with App
◦ Page 14: Tutorial 1-2
@X.Zhou
Intro to Analytical Programming
20
Characters are represented using single quotes
‘ ‘.
◦ Example: ‘x’, ‘!’ and ‘5’ are all characters, 5 is a
number
Character set include all letters of alphabet,
digits and punctuation marks, special
characters, such as Enter key.
Character encoding: Characters are put in an
order, and given equivalent integer values
◦ Example: ASCII
@X.Zhou
Intro to Analytical Programming
21
Numeric function can convert a character to
its equivalent numerical value, e.g. double(‘a’)
->97, int32(‘a’), double(‘abcd’)
Function char; convert number to the
equivalent character. E.g. char(97)
deg2rad/rad2deg: conversion betwwen
degrees and radians
Example: use whos to show the type of
variables/number of bytes used
@X.Zhou
Intro to Analytical Programming
22
A script file is a sequence of MATLAB commands – Program
Create script file – using MATLAB editor.
◦ Create a script: File→New→Script,
Script files can be typed and edited in any text editor and
then pasted into the MATLAB editor.
Script files are also called M-files because the extension .m is
used when they are saved.
The rules for file names are the same as for variables
When a script file runs (is executed), MATLAB executes the
commands in the order they are written, just as if they were
typed in the Command Window.
In the Command Window, the contents of the script can be
displayed and executed.
>>type WCF %display the WCF.m script file
>>WCF % Execute WCF.m
@X.Zhou
Intro to Analytical Programming
23
@X.Zhou
Editor window
The commands in the script file are typed line
by line
Intro to Analytical Programming
24
It is very important that all scripts be
documented well: comments
In MATLAB, a comment is anything after a %
Longer comments/comment blocks: %{ and %}
Comments are completely ignored when the
script is executed
To put in a comment, simply type the % symbol
at the beginning of a line, or select the
comment lines, then click Text→Comment
>> help WCF %the first block of comments will
be displayed
@X.Zhou
Intro to Analytical programming
25
The Run icon
Comments
Define 2 variables
Calculate and display the WCF
WCF
@X.Zhou
Run script file using command window
Intro to Analytical Programming
26
Textbook: Intro with App
◦ Page 27: 1.10 PROBLEMS
@X.Zhou
Intro to Analytical Programming
27
Textbook: Intro with App
◦ Page 27: 1.10 PROBLEMS
3, 5, 7
16, 22, 29
@X.Zhou
Intro to Analytical Programming
28
Reference book: MATLAB: Introduction with Applications by Amos Gilat
@X.Zhou
Intro to Analytical Programming
29
(C2C3)2 = (C1C2)2 + (C1C3)2 -2 (C1C2) (C1C3) cosγ1
Reference book: MATLAB: Introduction with Applications by Amos Gilat
@X.Zhou
Intro to Analytical Programming
30
Reference book: MATLAB: Introduction with Applications by Amos Gilat
@X.Zhou
Intro to Analytical Programming
31
Reference book: MATLAB: Introduction with Applications by Amos Gilat
@X.Zhou
Intro to Analytical Programming
32
Get started quickly with the basics of
MATLAB:
◦ https://www.mathworks.com/help/matlab/gettingstarted-with-matlab.html
@X.Zhou
Intro to Analytical Programming
33
MATLAB An Introduction with Applications
◦ Chapter 1
@X.Zhou
Intro to Analytical Programming
34