Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
%matlab introduction course notes ---week 6, term 1 1. Matlab as calculator 1.1 basic arithmetic operator + - * / ^ () e.g. 2+3/4*5 = 3^2*4 = 3-4/4-2 = 1.2 extended arithmetic - accidental error 1/0 = -1/0 = 0/0 = 1/Inf = we know 1/(1/x) = x, then try 1/(1/0) = ? complex number 1+i , -1+3*i 2. Numbers and formats 2.1 Different kind of numbers Integer: e.g. 123, -218 Real : e.g. 1.234, - 10.9 complex : e.g. 3.21-3.4*i (i = sqrt(-1)). Inf : Infinity (dividing by 0) NaN: Not a number (0/0) ex: notation for very large or small number, e.g. -1.34e+03 = ? , 1.34e-05 = ? 2.2 Calculation: 15 significant figures The 'format' tells how matlab prints numbers. Type 'help format' in command window for full list e.g. pi = ?? usually 3.1416 format long pi = ?? format short e pi = ?? format short pi = ?? If want to switch back to default format, type: format 2.3 finite accuracy consequences Matlab limit accuracy (enough for most cases) 64 bits, store number as large as 2*10^308, as small as 2*10^(-308) store any number 15 significant figures: e.g. 1.23456789023456 (14 figures, can handle) 1.23456789023456789012 (20 digits, truncated to 15 figures) round off cannnot be avoid. e.g. what is sin (pi) = ? try sin(pi) = ??---slight round-off error, take it as zero as long as small like 10^(-15). 3. Variables 3.1 combination of oth letter and number, case sensitive e.g. a , x1, z2453, A, t = 2+3-9, 2*t-2 Not allowed: Net-c, 2p, %x, @sign 3.2 special names: eps (= 2^(-54)), pi --> avoid using 3.3 complex numbers : i, j = sqrt(-1), unless you change them 4. Suppressing output (don't want to show output) hidden: x = -13; (semi-colon). 5. Build-in function 5.1 sin, cos, tan, sec = 1/sin, cosec = 1/cos, cotan e.g. work out the coordinate of a point on a circle of radius 5 centred at origin, having an elevation 30 degree = pi/6 radians. so (x, y), where x = 5*cos(pi/6), y = 5*sin(pi/6). 5.2 inverse trig function e.g. asin, acos, atan--> answer returned in radians, so asin(1) = pi/2 5.3 exponential exp : exp(x) = e^x logarithm: log: log to base e/ log10 to base 10 square root: sqrt(). e.g. x = 9; sqrt(x), exp(x), log(sqrt(x)), log10(x^2+6) 5.4 more e.g. eigenvalue, eigenvector: eig solving DE: 6. vectors 6.1 row vectors a = [1 2 3] or a = [1, 2, 3] e.g. V = [1 3 sqrt(5)], what is length(V) space vitally important : e.g. v2 = [3+ 4 5], v3 = [3 +4 5]; add vector of the same length: e.g. V + v3, v4 = 3*v3, v5 = 2*V-3*v4, v6 = v+v2??? wrong! since dimension must agree build a row vector from existing ones: e.g. w = [1 2 3], z = [8, 9], cd = [2*z -w], sort(cd) (ascending order) look at value of particular entries: e.g. w(2) = ? set w(3) = 100, then w = ?? 6.2 column vector e.g. c = [1; 3; sqrt(5)] or c2 = [3 return 4 return 5] c3 = 2*c-5*c2 6.3 column notation : a shortcut for producing row vectors e.g. 1:100 3:7 5:0.1:6 1:-1 --> [] 0.32:0.1:0.6 -0.4:-0.3:-2 6.4 extracting bits of a vector e.g. r5 = [1:2:6 -1:-2:-7] get 3rd to 6th entries: r5(3:6) = , try r5(1:2:7) = ?, r5(6:-2:1) = ?? 6.5 tranposing: row-> col, col-> row e.g. w,w', c, c' (w = [1 2 -3], c = [1;2 ; 3.5]) t = w+2*c', T = 5*w'-2*c 7. vector operation 7.1 scalar product: u*v = sum (ui*vi) u = [u1, ..., un]; v = [v1;...; vn] e.g. u = [10 -11 12], v = [20; -21; -22]; prod = u*v e.g. w = [2 1 3]; z = [7; 6; 5]; check: v*w, u*w', u*u', v'*z norm of a vector: ||u|| = sqrt(sum(ui)) compute norm: sqrt(u*u') or norm(u) 7.2 dot product-vector of the same length times with each other u.v = [u1v1,...,unvn] e.g. u.*w, u.*v', u.*z, u'.*v ex.: Tabulate y = x*sin(pi*x) for x = 0, 0.25, ... , 1 ans: x = 0:.25:1; y = x.*sin(pi*x); 7.3 dot divison of array-element by element division e.g. a = 1:5, b = 6:10, check a./b = , a./a = , c = -2:2, a./c, a.*b-24, ans./c ex: limit sin(pi*x)/x, as x Æ 0 ans: x = [.1 .01. .001 .0001], sin(pi*x)./x, format long , ans - pi e.g. 1/x (wrong!), 1./x (correct) 7.4 dot power of array (.^) sqare all element of a vector e.g. u.*u, u.^2, u.^4, u.*w.^(-2) 8. Matrix 8.1 generating matrix: zeros, ones, rand, randn e.g. Z = zeros(2,4) F = 5*ones(3,3) N = 10*rand(1,10); N = fix(N)---Æ fix: round toward zero (others, floor, round, ceil) R = randn(4,4) 8.2 load function Read out file containing matrix generated earlier . e.g. open new text file, input matrix (4 line) under Matlab directory (C:\Users\jasmine\Documents\MATLAB\magik.txt). Save file in the current directory. Type statement load magik.txt reads the file ad create a variable ‘magik’. Or use Import Wizard. 5 9 8.3 m-file e.g. creat a m-file named magik.m, containing A = [16 3 2 13 10 11 8 6 7 12 4 15 14 1]; Statement ‘magik’ read the file and creastes a variable ‘A’. 8.4 concatenation The process of joining small matrix to make bigger one. e.g. B = [A A+32; A+48 A+16]; sum(B) 8.5 Deleting rows and columns X = A; X(:,2) = []Ædeleting the second column X(1,2) = []Æ error, since not a matrix anymore X(2:2:10) = []Æremaining element into a row vector 8.6 Linear Algebra e.g. A+A’Æ produce a symmetric matrix A’*AÆ produce a symmetric matrix det(A)Æ determinant is zero, mean matrix is singular, means no inverse X = inv(A)Æ get warning Poly(A) Æ return the coefficient in the characteristic polynomial det(A-lambda*I) (order from lambda^4, …, lambda^1, constant). A.*AÆ element-by-element multiplication (same as ./ .^) 8.7 building tables: e.g. n = (0:9)’;Æ column vector then, pows = [n n.^2 2.^n]Æ build a table of squares and powers of 2 e.g. format short g x = (1:0.1:2)’; logs = [x log10(x)]Æ build a table of logariths 8.8 Multivariate data e.g. heart rate; weight hours of exercise/week D = [ 72 134 3.2 81 201 3.5 69 156 7.1 82 148 2.4 75 170 1.2] mu = mean(D), sigma = std(D) full list of data analysis: type ‘help datafun’, ‘help stats’ 8.9 scalar expansion e.g. subtract from a matrix from each element: B = A-8.5 e.g. Assign a scalar to all indices in a range: B(1:2,2:3) = 0 8.10 logical subscripting e.g. remove missing data: x = [2.1 1.7 1.6 1.5 NaN 1.9 2.2 1.6 1.8 5.1 1.4 1.8]; use ‘isfinite(x)’----true for all finite number and false for NaN and Inf x = x(isfinite(x)) e.g. remove outlier: x = x(abs(x-mean(x)) <= 2*std(x)) e.g. find location of prime number for magic matrix by setting the non-primes to 0: A(~isprime(A)) = 0 8.11 the find function e.g. k = find(isprime(A))’Æpick up location of primes in the magic square A(k)Æ display those primes