Math 485 (Sect 2) -- Mathematical Modeling
[ Tutorial page | Course home page ]
Section 1: MATLAB basics
Matrix notation:>> [1 2; 3 4; 5 6]gives
ans = 1 2 3 4 5 6a 3x2 matrix.
A vector is just an Nx1 (column) or 1xN matrix:
>> [1 2 3] ans = 1 2 3 >> [1;2;3] ans = 1 2 3Operations on matrices include
- Addition:
>> [1 2; 3 4] + [1 3 ; 5 7] ans = 2 5 8 11
- Multiplication:
>> [1 2;3 4] * [-1 0;0 -1] ans = -1 -2 -3 -4
- We can multiply any size matrix, not just square ones:
>> [1 2;3 4] * [1 ; 1] ans = 3 7
Variables
You can store matrices and numbers in variables, like so:>> a=[1 2; 3 4] a = 1 2 3 4 >> b=[1 3 ; 5 7] b = 1 3 5 7 >> a+b ans = 2 5 8 11
Functions
MATLAB's built-in functions can apply to numbers or matrices (or vectors):
>> cos(pi) ans = -1 >> cos([-pi 0 pi]) ans = -1 1 -1Aside from basic functions like sin, cos, exp, MATLAB also has a number of important operations on matrices.
- Eigenvalues:
>> a=[0 1; -1 -1] a = 0 1 -1 -1 >> eig(a) ans = -0.5000 + 0.8660i -0.5000 - 0.8660i
- Eigenvalues and eigenvectors:
>> [v,e]=eig(a) v = 0.7071 0.7071 -0.3536 + 0.6124i -0.3536 - 0.6124i e = -0.5000 + 0.8660i 0 0 -0.5000 - 0.8660i
- Inverting a matrix:
>> inv(a) ans = -1 -1 1 0 >> inv(a)*a ans = 1 0 0 1