Eng1060 computing for engineers hd
Computing For Engineers (Monash University)
StuDocu is not sponsored or endorsed by any college or university
Downloaded by IAN NJUGUNA ()
, lOMoARcPSD|5967629
EN G 1 0 6 0
M ATLAB ba sics
- 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 Commented [tr1]: Must start with letter, is case sensitive
- No blank spaces e.g car acceleration Commented [tr2]: car_accel is acceptable
- Command window
- Treat like calculator
- Does not save work Commented [tr3]: Do work in m-files
- 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 Commented [tr4]: Built-in variables
e.g. Imaginary number, Euler’s number, infinity, pi, NaN, etc.
– 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 Commented [tr5]: help <function name> or
doc <function name> for MATLAB help
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
Downloaded by IAN NJUGUNA ()
, lOMoARcPSD|5967629
M a t r ice s
• A matrix is a variable with multiple values
• Also called arrays
- Creating 1D matrices (vectors) Commented [tr6]: Use apostrophe/transpose operator ‚
to convert row to column or visa versa
- Square brackets [ ] to create matrices
- Row vectors are horizontal
Syntax: A = [1 2 3 4] or A = [1, 2, 3, 4]
- Column vectors are vertical
Syntax: B = [5; 6; 7; 8]
- The Colon Operator Commented [tr7]: Use colon if given step size
- Creates a vector with equally spaced values using a specified spacing
- Syntax: start_value : step : end_value Commented [tr8]: Default step of 1 if step is omitted
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 Commented [tr9]: Use linspace if given no. of values
- 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 Commented [tr10]: Rows then columns
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)
Downloaded by IAN NJUGUNA ()