by Dr Lam A.
Cheah
Topic 1 – MATLAB
Lesson 2 – Basics
1
Outline
Introduction
MATLAB Environment
MATLAB Basics
M-files (Script vs Function file)
Plotting
Flow Control & Loops
Graphical User Interface (GUI)
2
Outline
Introduction
MATLAB Environment
MATLAB Basics
M-files (Script vs Function file)
Plotting
Flow Control & Loops
Graphical User Interface (GUI)
3
Variables
Simple arithmetic computations are the easiest thing to do in MATLAB. For
example addition, multiplication, division, and powers are the standard
symbols.
For example if you type:
into the command line and press “enter”.
First MATLAB prints:
it also adds a variable called ans to the ‘Workspace.’
If you look over at the workspace table in the top right of the page it will
have a variable ans with a value -4.0191e+05.
The variable ans always contains the result of the last computation.
4
Variables
5
Variables
6
Variables – Maths
7
Variables – Maths
8
Variables – Maths
9
Variables
10
Variables
11
Variables
12
Variables
13
Arrays
Matrix e.g. 2×2 Vector e.g. 3×1
14
Arrays – Row & Column
row
column
row column
column
All rows
All columns
row
15
Arrays – Indexing
16
Arrays – Indexing
17
Arrays
merge or concatenate
18
Arrays – Colon
19
Arrays – Colon
What commands would you type to make the following
lists:
All odd numbers from 1 to 99?
All numbers divisible by 8 from 0 to 800?
A backwards list from 100 to 0?
The list [0, .1, .2, …, 9.8, 9.9, 10]?
20
Arrays
21
Arrays
22
Arrays
23
Arrays – Maths
24
Arrays – Maths
25
Arrays – Maths
26
Arrays – Linear Algebra
The system of equations shown can be written as the product of a
matrix of the coefficients A and a column vector of the unknowns x.
27
Arrays – Linear Algebra
We have a matrix multiplication equation of the form Ax = b, and we
want to solve for the unknowns x. This can be accomplished as
follows:
28
Arrays – Linear Algebra
29
Arrays – Linear Algebra
30