ENG 1060
MATLAB basics
- Defining variables- Syntax:variable_name = value(s) - No special characters , . / : ; ~ ^ ! ? \
- No foreign characters Γ Ψ Ω Πᾢ안 녕 하 세 요Ӫ Ɲũ Ậ
- Cannot start the variable name with a numerical character 0 1 2 3 4 5 6 7 8 9 - No blank spaces e.g car acceleration
- Command window - Treat like calculator - Does not save work - Operator precedence
1. Parentheses ()
2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^) 3. Unary plus (+), unary minus (-), logical negation (~)
4. Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)
5. Addition (+), subtraction (-) 6. Colon operator (:)
7. Less than (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)
8. Element-wise AND (&) 9. Element-wise OR (|) 10. Short-circuit AND (&&) 11. Short-circuit OR (||) - Built-in functions
– sqrt, mod, rem – log, log10, exp – sin, cos, tan
– round, ceil, floor = round to nearest, round down, round up – min, max, sign
- Built-in commands
clc: Clears the command window clear all: Clears all variables close all: Closes all figure windows
fclose all: Closes all files opened by MATLAB who: Lists existing variables
whos: Lists existing variables and other properties dir: Lists everything in the current directory
Commented [tr1]:Must start with letter, iscase sensitive Commented [tr2]:car_accel is acceptable
Commented [tr3]:Do work in m-files
Commented [tr4]:Built-in variables
e.g. Imaginary number, Euler's number, infinity, pi, NaN, etc.
Commented [tr5]:help <function name>or doc <function name>for MATLAB help
Matrices
A matrix is a variable with multiple values
Also called arrays
- Creating 1D matrices (vectors)
-Square brackets [ ] to create matrices - Row vectors are horizontal
Syntax:A = [1 2 3 4]orA = [1, 2, 3, 4]
- Column vectors are vertical
Syntax:B = [5; 6; 7; 8]
- The Colon Operator
-Creates a vector with equally spaced values using a specified spacing - Syntax:start_value : step : end_value
A = [6 7 8 9 10] or A = 6 : 1 : 10 or A = 6 : 10 B = [6 11 16 21 26] or B = 6 : 5 : 26
C = [6 4 2 0 -2 -4] or C = 6 : -2: -4 -Colon operator to create column vectors?
Use round brackets with apostrophe:A = (1 : 20)' - The linspace Function
-Create a vector with equally spaced values using a specified number of points - Syntax:linspace(start_value, end_value, num_of_points)
A = [1 2 3 4 5 6] or linspace(1, 6, 6)
B = [3.8510 4.5158 5.1807 5.8455 6.5103 7.1752 7.84] or B = linspace(3.851, 7.84, 7)
- Creating 2D matrices
-A 2-dimensional matrix contains multiple rows and columns - Use square brackets [ ] to create a 2 dimensional matrix
MATLAB refers to the rows first, then the columns
A = [1 2 3; 4 5 6] (2 × 3 matrix) or using colon operator A = [1:3; 4:6]
- Matrix concatenation & functions
-Matrices can be concatenated to make larger matrices - Matrices can only join if one of their dimensions are the same - Matrices can be created using
A =zeros(rows, columns) B =ones(rows, columns)
C =eye(rows, columns) identity matrix I D =rand(rows, columns)
- Matrix properties can be obtained using Longest_side =length(matrix) [rows, columns] =size(matrix)
Commented [tr6]:Use apostrophe/transpose operator ‘ to convert row to column or visa versa
Commented [tr7]:Use colon if givenstep size Commented [tr8]:Default step of 1 if step is omitted
Commented [tr9]:Use linspace if givenno. of values
Commented [tr10]:Rows then columns
- Matrix addressing: Vectors
-All values in a vector or matrix are assigned an address - To address elements in a vector
Syntax:A(index)
- Example:A = [5 10 15 20 25 30 35 40]
A(1)→5 (first element) A(4)→20 (fourth element) A(end)→40 (end element) -What if we want multiple values from a vector?
A = [10, 20, 30, 40, 50, 60]
Individually… A(1) = 10, A(4) = 40, A(5) = 50 and A(6) = 60 - Alternatively, we can use an index that is a vector
A([1 4 5 6]) = [10, 40, 50, 60]
-E.g.
A = [6, 9, 15, 20, 98, 241, 259]
A([1, 3:5, 7])= [6, 15, 20, 98, 259]
- Matrix addressing: 2D matrices -A 2D matrix has rows and columns
Syntax:A(row_index, column_index)
- A colon (:) by itself tells MATLAB to return either all rows or all columns
Commented [tr11]:REMEMBER Square brackets [ ] are used to create matrices Round brackets ( ) are used to address matrices