Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Anthropologist on Matlab. Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y) cumprod(x) cumsum(x) del2(A) diff(x) dot(x,y) gradient(Z,

Similar presentations


Presentation on theme: "An Anthropologist on Matlab. Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y) cumprod(x) cumsum(x) del2(A) diff(x) dot(x,y) gradient(Z,"— Presentation transcript:

1 An Anthropologist on Matlab

2 Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y) cumprod(x) cumsum(x) del2(A) diff(x) dot(x,y) gradient(Z, dx, dy) Correlation coefficients Covariance matrix complex conjugate pairs vector cross product cumulative prod of cols cumulative sum of cols five-point discrete Laplacian diff between elements vector dot product approximate gradient

3 Data Analysis Functions (2) histogram(x) max(x), max(x,y) mean(x) median(x) min(x), min(x,y) prod(x) sort(x) std(x) subspace(A,B) sum(x) Histogram or bar chart max component mean of cols median of cols minimum component product of elems in col sort cols (ascending) standard dev of cols angle between subspaces sum of elems per col

4 Symbolic Math Toolbox

5 Symbolic Expressions ‘1/(2*x^n)’ cos(x^2) - sin(x^2) M = sym(‘[a, b ; c, d]’) f = int(‘x^3 / sqrt(1 - x)’, ‘a’, ‘b’)

6 Symbolic Expressions diff(‘cos(x)’) ans = -sin(x) det(M) ans = a*d - b * c

7 Symbolic Functions numden(m) - num & denom of polynomial symadd(f,g) - add symbolic polynomials symsub(f,g) - sub symbolic polynomials symmul(f,g) - mult symbolic polynomials symdiv(f,g) - div symbolic polynomials sympow(f,’3*x’) - raise f^3

8 Advanced Operations f = ‘1/(1+x^2)’ g = ‘sin(x)’ compose(f,g) % f(g(x)) ans= 1/(1+sin(x)^2)

9 Advanced Operations finverse(x^2) ans = x^(1/2) symsum(‘(2*n - 1) ^ 2’, 1, ‘n’) ans = 11/3*n + 8/3-4*(n+1)^2 + 4/3*(n+1)^3

10 Symbolic Differentiation f = ‘a*x^3 + x^2 + b*x - c’ diff(f) % by default wrt x ans = 3*a^2 + 2*x + b diff(f, ‘a’) % wrt a ans = x^3 diff(f,’a’,2) % double diff wrt a ans = 0

11 Symbolic Integration f = sin(s+2*x) int(f) ans = -1/2*cos(s+2*x) int(f,’s’) ans = -cos(s+2*x) int(f, ‘s’, pi/2,pi) ans= -cos(s)

12 Comments &Punctuation (1) All text after a percentage sign (%) is ignored >> % this is a comment Multiple commands can be placed on one line separated by commas (,) >> A = magic(4), B = ones(4), C = eye(4)

13 Comments &Punctuation (2) A semicolon may be also used, either after a single command or multiple commands >> A = magic(4); B = ones(4); C = eye(4); Ellipses (…) indicate a statement is continued on the next line A = B/… C

14 SAVE Command (1) >> save Store all the variables in binary format in a file called matlab.mat >> save fred Store all the variables in binary format in a file called fred.mat >> save a b d fred Store the variables a, b and d in fred.mat

15 SAVE Command (2) >> save a b d fred -ascii Stores the variables a, b and d in a file called fred.mat in 8-bit ascii format >> save a b d fred -ascii -double Stores the variables a, b and d in a file called fred.mat in 16-bit ascii format

16 Load Command Create a text file called mymatrix.dat with –16.03.02.013.0 – 5.0 10.0 11.0 8.0 – 9.06.0 7.0 12.0 – 4.0 15.0 14.0 1.0 “load mymatrix.dat”, create variable mymatrix

17 M-Files To store your own MatLab commands in a file, create it as a text file and save it with a name that ends with “.m” So mymatrix.m A = [… 16.03.02.013.0 5.010.011.08.0 9.06.07.012.0 4.015.014.01.0]; type mymatrix

18 IF Condition if condition {commands} end If x = 2 output = ‘x is even’ end

19 WHILE Loop while condition {commands} end X = 10; count = 0; while x > 2 x = x / 2; count = count + 1; end

20 FOR Loop for x=array {commands} end for n = 1:10 x(n) = sin(n); end A = zeros(5,5); % prealloc for n = 1:5 for m = 5:-1:1 A(n,m) = n^2 + m^2; end disp(n) end

21 Creating a function function a = gcd(a,b) % GCD Greatest common divisor % gcd(a,b) is the greatest common divisor of % the integers a and b, not both zero. a = round(abs(a)); b = round(abs(b)); if a == 0 & b == 0 error('The gcd is not defined when both numbers are zero') else while b ~= 0 r = rem(a,b); a = b; b = r; end

22 Quick Exercise (!) Consider Polynomial Addition again : how would you write a program that takes in two polynomials and irrespective of their sizes it adds the polynomials together ? Given that the function length(A) returns the length of a vector. Answers on a postcard to : dgordon@maths.kst.dit.ie oh, and while you’re here anyhow, if you have a browser open, please go to the following sites : http://www.the hungersite.com http://www.hitsagainsthunger.com


Download ppt "An Anthropologist on Matlab. Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y) cumprod(x) cumsum(x) del2(A) diff(x) dot(x,y) gradient(Z,"

Similar presentations


Ads by Google