Upcoming SlideShare
×

Introduction to MatLab programming

3,285 views
3,122 views

Published on

Published in: Education
5 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• nice presentation...it is very uesful sir........

Are you sure you want to  Yes  No
• Thanks

Are you sure you want to  Yes  No
Views
Total views
3,285
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
413
2
Likes
5
Embeds 0
No embeds

No notes for slide

Introduction to MatLab programming

1. 1. Zen and the Art of MatLab Damian Gordon
2. 2. Hard work done by : <ul><li>Daphne Gilbert & </li></ul><ul><li>Susan Lazarus </li></ul>
3. 3. Introduction to MatLab <ul><li>MatLab is an interactive, matrix-based system for numeric computation and visualisation </li></ul><ul><li>MATrix LABoratory </li></ul><ul><li>Used in image processing, image synthesis, engineering simulation, etc. </li></ul>
4. 4. References <ul><li>“ Mastering MatLab” Duane Hanselman, Bruce Littlefield </li></ul><ul><li>“ The MatLab Primer” http://www.fi.uib.no/Fysisk/Teori/KURS/WRK/mat/mat.html </li></ul><ul><li>“ The MatLab FAQ” http://www.isr.umd.edu/~austin/ence202.d/matlab-faq.html </li></ul>
5. 5. Printed Circuit Board
6. 6. Specific Bond Selected
7. 7. Bond Shape Estimated
8. 8. MATLAB Command Window To get started, type one of these: helpwin, helpdesk, or demo. For product information, type tour or visit www.mathworks.com. » » help HELP topics:
9. 9. Creating Variables <ul><li>>> varname = 12 </li></ul><ul><li>varname = </li></ul><ul><li>12 </li></ul><ul><li>>> SS = 56; N = 4; Tot_Num = SS + N </li></ul><ul><li>Tot_Num = </li></ul><ul><li>60 </li></ul>
10. 10. Operations + Addition - Subtraction * Multiplication ^ Power Division / Division <ul><li>Add vars </li></ul><ul><li>Subtract vars Multiplication </li></ul><ul><li>Raise to the power </li></ul><ul><li>Divide vars (A div B) </li></ul><ul><li>Divide vars (B div A) </li></ul>
11. 11. Creating Complex Numbers <ul><li>>> 3 + 2i </li></ul><ul><li>>> sqrt(9) + sin(0.5)*j </li></ul><ul><li>ans = </li></ul><ul><li>3.0000 + 0.4794i </li></ul><ul><li>Num = sqrt(9) + sin(0.5)*j </li></ul><ul><li>real(Num) </li></ul><ul><li>imag(Num) </li></ul>
12. 12. Entering Matrices (1) <ul><li>>> A = [1 2 3; 4 5 6; 7 8 9] </li></ul><ul><li>OR </li></ul><ul><li>>> A = [ </li></ul><ul><li>1 2 3 </li></ul><ul><li>4 5 6 </li></ul><ul><li>7 8 9 ] </li></ul>
13. 13. Entering Matrices (2) <ul><li>To create an NxM zero-filled matrix </li></ul><ul><li>>> zeros(N,M) </li></ul><ul><li>To create a NxN zero-filled matrix </li></ul><ul><li>>> zeros(N) </li></ul><ul><li>To create an NxM one-filled matrix </li></ul><ul><li>>> ones(N,M) </li></ul><ul><li>To create a NxN one-filled matrix </li></ul><ul><li>>> ones(N) </li></ul>
14. 14. Entering Matrices (3) <ul><li>To create an NxM randomly-filled matrix (which is uniformly distributed) </li></ul><ul><li>>> rand(N,M) </li></ul><ul><li>To create an NxM randomly-filled matrix (which is normally distributed) </li></ul><ul><li>>> randn(N,M) </li></ul>
15. 15. Complex Matrices <ul><li>To enter a complex matrix, you may do it in one of two ways : </li></ul><ul><li>>> A = [1 2; 3 4] + i*[5 6;7 8] </li></ul><ul><li>OR </li></ul><ul><li>>> A = [1+5i 2+6i; 3+7i 4+8i] </li></ul>
16. 16. MATLAB Command Window » who Your variables are: a b c » whos Name Size Bytes Class a 8x8 512 double array b 9x9 648 double array c 9x9 648 double array Grand total is 226 elements using 1808 bytes
17. 17. Matrix Addition <ul><li>» A = [ 1 1 1 ; 2 2 2 ; 3 3 3] </li></ul><ul><li>» B = [3 3 3 ; 4 4 4 ; 5 5 5 ] </li></ul><ul><li>» A + B </li></ul><ul><li>ans = </li></ul><ul><li>4 4 4 </li></ul><ul><li>6 6 6 </li></ul><ul><li>8 8 8 </li></ul>
18. 18. Matrix Subtraction <ul><li>» A = [ 1 1 1 ; 2 2 2 ; 3 3 3] </li></ul><ul><li>» B = [3 3 3 ; 4 4 4 ; 5 5 5 ] </li></ul><ul><li>» B - A </li></ul><ul><li>ans = </li></ul><ul><li>2 2 2 </li></ul><ul><li>2 2 2 </li></ul><ul><li>2 2 2 </li></ul>
19. 19. Matrix Multiplication <ul><li>» A = [ 1 1 1 ; 2 2 2 ; 3 3 3] </li></ul><ul><li>» B = [3 3 3 ; 4 4 4 ; 5 5 5 ] </li></ul><ul><li>» A * B </li></ul><ul><li>ans = </li></ul><ul><li>12 12 12 </li></ul><ul><li>24 24 24 </li></ul><ul><li>36 36 36 </li></ul>
20. 20. Matrix - Power <ul><li>» A ^ 2 </li></ul><ul><li>ans = </li></ul><ul><li>6 6 6 </li></ul><ul><li>12 12 12 </li></ul><ul><li>18 18 18 </li></ul><ul><li>» A ^ 3 </li></ul><ul><li>ans = </li></ul><ul><li>36 36 36 </li></ul><ul><li>72 72 72 </li></ul><ul><li>108 108 108 </li></ul>
21. 21. Matrix Transpose <ul><li>A = </li></ul><ul><li>1 1 1 </li></ul><ul><li>2 2 2 </li></ul><ul><li>3 3 3 </li></ul><ul><li>» A' </li></ul><ul><li>ans = </li></ul><ul><li>1 2 3 </li></ul><ul><li>1 2 3 </li></ul><ul><li>1 2 3 </li></ul>
22. 22. Matrix Division <ul><li>Left Division </li></ul><ul><li>x = AB (is A*x=B) </li></ul><ul><li>>> A = rand(4) </li></ul><ul><li>>> B = rand(4) </li></ul><ul><li>>> C = A B </li></ul><ul><li>=> A * C = B </li></ul><ul><li>Right Division / </li></ul><ul><li>x=A/B (is x*A=B) </li></ul><ul><li>>> A = rand(4) </li></ul><ul><li>>> B = rand(4) </li></ul><ul><li>>> C = A / B </li></ul><ul><li>=> C * A = B </li></ul>
23. 23. Matrix Operations + Addition - Subtraction * Multiplication ^ Power ‘ Conjugate Transpose Left Division / Right Division <ul><li>Add matrices </li></ul><ul><li>Subtract matrices Matrix Multiplication </li></ul><ul><li>Raise to the power </li></ul><ul><li>Get transpose </li></ul><ul><li>x = AB (is A*x=B) </li></ul><ul><li>x=A/B (is x*A=B) </li></ul>
24. 24. Philosophiae Naturalis Principia Matlab ematica Damian Gordon
25. 25. Getting to Matlab
26. 26. Magic Matrix <ul><li>MAGIC Magic square. </li></ul><ul><li>MAGIC(N) is an N-by-N matrix constructed from the integers </li></ul><ul><li>1 through N^2 with equal row, column, and diagonal sums. </li></ul><ul><li>Produces valid magic squares for N = 1,3,4,5,... </li></ul>
27. 27. Identity Function <ul><li>>> eye (4) </li></ul><ul><li>ans = </li></ul><ul><li>1 0 0 0 </li></ul><ul><li>0 1 0 0 </li></ul><ul><li>0 0 1 0 </li></ul><ul><li>0 0 0 1 </li></ul>
28. 28. Upper Triangle Matrix » a = ones(5) a = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 » triu(a) ans = 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1
29. 29. Lower Triangle Matrix » a = ones(5) a = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 » tril(a) ans = 1 0 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 1 1 1 1 1
30. 30. Hilbert Matrix <ul><li>» hilb(4) </li></ul><ul><li>ans = </li></ul><ul><li>1.0000 0.5000 0.3333 0.2500 </li></ul><ul><li>0.5000 0.3333 0.2500 0.2000 </li></ul><ul><li>0.3333 0.2500 0.2000 0.1667 </li></ul><ul><li>0.2500 0.2000 0.1667 0.1429 </li></ul>
31. 31. Inverse Hilbert Matrix » invhilb(4) ans = 16 -120 240 -140 -120 1200 -2700 1680 240 -2700 6480 -4200 -140 1680 -4200 2800
32. 32. Toeplitz matrix. <ul><li>TOEPLITZ </li></ul><ul><li>TOEPLITZ(C,R) is a non-symmetric Toeplitz matrix having C as its </li></ul><ul><li>first column and R as its first row. </li></ul><ul><li>TOEPLITZ(R) is a symmetric (or Hermitian) Toeplitz matrix. </li></ul><ul><li>-> See also HANKEL </li></ul>
33. 33. Summary of Functions <ul><li>magic </li></ul><ul><li>eye(4) </li></ul><ul><li>triu(4) </li></ul><ul><li>tril(4) </li></ul><ul><li>hilb(4) </li></ul><ul><li>invhilb(4) </li></ul><ul><li>toeplitz(4) </li></ul>- magic matrix - identity matrix - upper triangle - lower triangle - hilbert matrix - Inverse Hilbert matrix - non-symmetric Toeplitz matrix
34. 34. Dot Operator <ul><li>A = magic(4); b=ones(4); </li></ul><ul><li>A * B </li></ul><ul><li>A.*B </li></ul><ul><li>the dot operator performs element-by-element operations, for “*”, “” and “/” </li></ul>
35. 35. Concatenation <ul><li>To create a large matrix from a group of smaller ones </li></ul><ul><li>try </li></ul><ul><ul><li>A = magic(3) </li></ul></ul><ul><ul><li>B = [ A, zeros(3,2) ; zeros(2,3), eye(2)] </li></ul></ul><ul><ul><li>C = [A A+32 ; A+48 A+16] </li></ul></ul><ul><ul><li>Try some of your own !! </li></ul></ul>
36. 36. Subscripts <ul><li>Row i and Column j of matrix A is denoted by A(i,j) </li></ul><ul><li>A = Magic(4) </li></ul><ul><li>try </li></ul><ul><ul><li>A(1,4) + A(2,4) + A(3,4) + A(4,4) </li></ul></ul><ul><li>try </li></ul><ul><ul><li>A(4,5) </li></ul></ul>
37. 37. The Colon Operator (1) <ul><li>This is one MatLab’s most important operators </li></ul><ul><li>1:10 means the vector </li></ul><ul><ul><li>1 2 3 4 5 6 7 8 9 10 </li></ul></ul><ul><li>100:-7:50 </li></ul><ul><ul><li>100 93 86 79 72 65 58 51 </li></ul></ul><ul><li>0:pi/4:pi </li></ul><ul><ul><li>0 0.7854 1.5708 2.3562 3.1416 </li></ul></ul>
38. 38. The Colon Operator (2) <ul><li>The first K elements in the jth column is </li></ul><ul><ul><li>A(1:K, j) </li></ul></ul><ul><li>Sum(A(1:4, 4)) is the sum of the 4th column </li></ul><ul><li>or </li></ul><ul><li>Sum(A(:, 4)) means the same </li></ul>
39. 39. matlABBA
40. 40. The Colon Operator (1) <ul><li>This is one MatLab’s most important operators </li></ul><ul><li>1:10 means the vector </li></ul><ul><ul><li>1 2 3 4 5 6 7 8 9 10 </li></ul></ul><ul><li>100:-7:50 </li></ul><ul><ul><li>100 93 86 79 72 65 58 51 </li></ul></ul><ul><li>0:pi/4:pi </li></ul><ul><ul><li>0 0.7854 1.5708 2.3562 3.1416 </li></ul></ul>
41. 41. The Colon Operator (2) <ul><li>The first K elements in the jth column is </li></ul><ul><ul><li>A(1:K, j) </li></ul></ul><ul><li>Sum(A(1:4, 4)) is the sum of the 4th column </li></ul><ul><li>or </li></ul><ul><li>Sum(A(:, 4)) means the same </li></ul>
42. 42. Deleting Rows and Columns (1) <ul><li>Create a temporary matrix X </li></ul><ul><li>X=A; </li></ul><ul><li>X(:, 2) = [] </li></ul><ul><li>Deleting a single element won’t result in a matrix, so the following will return an error </li></ul><ul><li>X(1,2) = [] </li></ul>
43. 43. Deleting Rows and Columns (2) <ul><li>However, using a single subscript, you can delete </li></ul><ul><ul><li>a single element </li></ul></ul><ul><ul><li>sequence of elements </li></ul></ul><ul><li>So X(2:2:10) = [] </li></ul><ul><li>gives </li></ul><ul><li>x = 16 9 2 7 13 12 1 </li></ul>
44. 44. The ‘FIND’ Command (1) >> x = -3:3 x = -3 -2 -1 0 1 2 3 K = find(abs(x) > 1) K = 1 2 6 7
45. 45. The ‘FIND’ Command (2) A = [ 1 2 3 ; 4 5 6 ; 7 8 9] [i, j] = find (A > 5) i = 3 3 2 3 j = 1 2 3 3
46. 46. Special Variables <ul><li>ans </li></ul><ul><li>pi </li></ul><ul><li>eps </li></ul><ul><li>flops </li></ul><ul><li>inf </li></ul><ul><li>NaN </li></ul><ul><li>i,j </li></ul><ul><li>why </li></ul>- default name for results - pi - “help eps” - count floating point ops - Infinity, e.g. 1/0 - Not a number, e.g. 0/0 - root minus one - why not ?
47. 47. LOGO Command
48. 48. The ‘PLOT’ Command (1) <ul><li>>> X = linspace(0, 2*pi, 30); </li></ul><ul><li>>> Y = sin(X); </li></ul><ul><li>>> plot(X,Y) </li></ul><ul><li>>> Z = cos(X); </li></ul><ul><li>>> plot(X,Y,X,Z); </li></ul>
49. 49. The ‘PLOT’ Command (2) <ul><li>>> W = [Y ; Z] </li></ul><ul><li>>> plot (X,W) </li></ul><ul><li>Rotate by 90 degrees </li></ul><ul><li>>> plot(W,X) </li></ul>
50. 51. The ‘PLOT’ Command (1) <ul><li>>> X = linspace(0, 2*pi, 30); </li></ul><ul><li>>> Y = sin(X); </li></ul><ul><li>>> plot(X,Y) </li></ul><ul><li>>> Z = cos(X); </li></ul><ul><li>>> plot(X,Y,X,Z); </li></ul>
51. 52. The ‘PLOT’ Command (2) <ul><li>>> W = [Y ; Z] </li></ul><ul><li>>> plot (X,W) </li></ul><ul><li>Rotate by 90 degrees </li></ul><ul><li>>> plot(W,X) </li></ul>
52. 53. PLOT Options <ul><li>>> plot(X,Y,’g:’) </li></ul><ul><li>>> plot(X,Y,’r-’) </li></ul><ul><li>>> plot(X,Y,’ko’) </li></ul><ul><li>>> plot(X,Y,’g:’,X,Z,’r-’,X,Y,’wo’,X,Z,’c+’); </li></ul>
53. 54. PLOT Options <ul><li>>> grid on </li></ul><ul><li>>> grid off </li></ul><ul><li>>> xlabel(‘this is the x axis’); </li></ul><ul><li>>> ylabel(‘this is the y axis’); </li></ul><ul><li>>> title(‘Title of Graph’); </li></ul><ul><li>>> text(2.5, 0.7, ’sin(x)’); </li></ul><ul><li>>> legend(‘sin(x)’, ‘cos(x)’) </li></ul>
54. 55. SUBPLOT Command <ul><li>Subplot(m,n,p) </li></ul><ul><ul><li>creates a m-by-n matrix in and plots in the p th plane. </li></ul></ul><ul><li>subplot(2,2,1) </li></ul><ul><li>plot(X,Y) </li></ul><ul><li>subplot(2,2,2) </li></ul><ul><li>plot(X,Z) </li></ul><ul><li>subplot(2,2,3) </li></ul><ul><li>plot( X,Y,X,Z) </li></ul><ul><li>subplot(2,2,4) </li></ul><ul><li>plot(W,X) </li></ul>
55. 56. Specialised matrices <ul><li>compan </li></ul><ul><li>gallery </li></ul><ul><li>hadamard </li></ul><ul><li>hankel </li></ul><ul><li>pascal </li></ul><ul><li>rosser </li></ul><ul><li>vander </li></ul><ul><li>wilkinson </li></ul><ul><li>Companion matrix </li></ul><ul><li>Higham test matrices </li></ul><ul><li>Hadamard matrix </li></ul><ul><li>Hankel matrix </li></ul><ul><li>Pascal matrix. </li></ul><ul><li>Classic symmetric eigenvalue test problem </li></ul><ul><li>Vandermonde matrix </li></ul><ul><li>Wilkinson's eigenvalue test matrix </li></ul>
56. 57. Polynomials <ul><li>Polynomials are represented as </li></ul><ul><li>row vectors with its coefficients in </li></ul><ul><li>descending order, e.g. </li></ul><ul><li>X 4 - 12X 3 + 0X 2 +25X + 116 </li></ul><ul><li>p = [1 -12 0 25 116] </li></ul>
57. 58. Polynomials <ul><li>The roots of a polynomial are found as follows </li></ul><ul><li>r = roots(p) </li></ul><ul><li>roots are represented as a column vector </li></ul>
58. 59. Polynomials <ul><li>Generating a polynomial from its roots </li></ul><ul><li>polyans = poly(r) </li></ul><ul><li>includes imaginary bits due to rounding </li></ul><ul><li>mypolyans = real(polyans) </li></ul>
59. 60. Polynomial Addition/Sub <ul><li>a = [1 2 3 4] </li></ul><ul><li>b = [1 4 9 16] </li></ul><ul><li>c = a + b </li></ul><ul><li>d = b - a </li></ul>
60. 61. Polynomial Addition/Sub <ul><li>What if two polynomials of different order ? </li></ul><ul><li>X 3 + 2X 2 +3X + 4 </li></ul><ul><li>X 6 + 6X 5 + 20X 4 - 52X 3 + 81X 2 +96X + 84 </li></ul><ul><li>a = [1 2 3 4] </li></ul><ul><li>e = [1 6 20 52 81 96 84] </li></ul><ul><li>f = e + [0 0 0 a] or f = e + [zeros(1,3) a] </li></ul>
61. 62. Polynomial Multiplication <ul><li>a = [1 2 3 4] </li></ul><ul><li>b = [1 4 9 16] </li></ul><ul><li>Perform the convolution of two arrays ! </li></ul><ul><li>g = conv(a,b) </li></ul><ul><li>g = </li></ul><ul><li>1 6 20 50 75 84 64 </li></ul>
62. 63. Polynomial Division <ul><li>a = [1 2 3 4] </li></ul><ul><li>g = [1 6 20 50 75 84 64] </li></ul><ul><li>Perform the deconvolution of two arrays ! </li></ul><ul><li>[q,r] = deconv(g, a) </li></ul><ul><li>q = {quotient} </li></ul><ul><li>1 4 9 16 </li></ul><ul><li>r = {remainder} </li></ul><ul><li>0 0 0 0 0 0 0 0 </li></ul>
63. 64. Polynomial Differentiation <ul><li>f = [1 6 20 48 69 72 44] </li></ul><ul><li>h = polyder(f) </li></ul><ul><li>h = </li></ul><ul><li>6 30 80 144 138 72 </li></ul>
64. 65. Polynomial Evaluation <ul><li>x = linspace(-1,3) </li></ul><ul><li>p = [1 4 -7 -10] </li></ul><ul><li>v = polyval(p,x) </li></ul><ul><li>plot (x,v), title(‘Graph of P’) </li></ul>
65. 66. Rational Polynomials <ul><li>Rational polynomials, seen in Fourier, Laplace and Z transforms </li></ul><ul><li>We represent them by their numerator and denominator polynomials </li></ul><ul><li>we can use residue to perform a partial fraction expansion </li></ul><ul><li>We can use polyder with two inputs to differentiate rational polynomials </li></ul>
66. 67. An Anthropologist on Matlab
67. 68. Data Analysis Functions (1) <ul><li>corrcoef(x) </li></ul><ul><li>cov(x) </li></ul><ul><li>cplxpair(x) </li></ul><ul><li>cross(x,y) </li></ul><ul><li>cumprod(x) </li></ul><ul><li>cumsum(x) </li></ul><ul><li>del2(A) </li></ul><ul><li>diff(x) </li></ul><ul><li>dot(x,y) </li></ul><ul><li>gradient(Z, dx, dy) </li></ul><ul><li>Correlation coefficients </li></ul><ul><li>Covariance matrix </li></ul><ul><li>complex conjugate pairs </li></ul><ul><li>vector cross product </li></ul><ul><li>cumulative prod of cols </li></ul><ul><li>cumulative sum of cols </li></ul><ul><li>five-point discrete Laplacian </li></ul><ul><li>diff between elements </li></ul><ul><li>vector dot product </li></ul><ul><li>approximate gradient </li></ul>
68. 69. Data Analysis Functions (2) <ul><li>histogram(x) </li></ul><ul><li>max(x), max(x,y) </li></ul><ul><li>mean(x) </li></ul><ul><li>median(x) </li></ul><ul><li>min(x), min(x,y) </li></ul><ul><li>prod(x) </li></ul><ul><li>sort(x) </li></ul><ul><li>std(x) </li></ul><ul><li>subspace(A,B) </li></ul><ul><li>sum(x) </li></ul><ul><li>Histogram or bar chart </li></ul><ul><li>max component </li></ul><ul><li>mean of cols </li></ul><ul><li>median of cols </li></ul><ul><li>minimum component </li></ul><ul><li>product of elems in col </li></ul><ul><li>sort cols (ascending) </li></ul><ul><li>standard dev of cols </li></ul><ul><li>angle between subspaces </li></ul><ul><li>sum of elems per col </li></ul>
69. 70. Symbolic Math Toolbox
70. 71. Symbolic Expressions <ul><li>‘ 1/(2*x^n)’ </li></ul><ul><li>cos(x^2) - sin(x^2) </li></ul><ul><li>M = sym(‘[a , b ; c , d]’) </li></ul><ul><li>f = int(‘x^3 / sqrt(1 - x)’, ‘a’, ‘b’) </li></ul>
71. 72. Symbolic Expressions <ul><li>diff(‘cos(x)’) </li></ul><ul><li>ans = </li></ul><ul><li>-sin(x) </li></ul><ul><li>det(M) </li></ul><ul><li>ans = </li></ul><ul><li>a*d - b * c </li></ul>
72. 73. Symbolic Functions <ul><li>numden(m) - num & denom of polynomial </li></ul><ul><li>symadd(f,g) - add symbolic polynomials </li></ul><ul><li>symsub(f,g) - sub symbolic polynomials </li></ul><ul><li>symmul(f,g) - mult symbolic polynomials </li></ul><ul><li>symdiv(f,g) - div symbolic polynomials </li></ul><ul><li>sympow(f,’3*x’) - raise f^3 </li></ul>
73. 74. Advanced Operations <ul><li>f = ‘1/(1+x^2)’ </li></ul><ul><li>g = ‘sin(x)’ </li></ul><ul><li>compose(f,g) % f(g(x)) </li></ul><ul><li>ans= </li></ul><ul><li>1/(1+sin(x)^2) </li></ul>
74. 75. Advanced Operations <ul><li>finverse(x^2) </li></ul><ul><li>ans = </li></ul><ul><li>x^(1/2) </li></ul><ul><li>symsum(‘(2*n - 1) ^ 2’, 1, ‘n’) </li></ul><ul><li>ans = </li></ul><ul><li>11/3*n + 8/3-4*(n+1)^2 + 4/3*(n+1)^3 </li></ul>
75. 76. Symbolic Differentiation <ul><li>f = ‘a*x^3 + x^2 + b*x - c’ </li></ul><ul><li>diff(f) % by default wrt x </li></ul><ul><li>ans = </li></ul><ul><li>3*a^2 + 2*x + b </li></ul><ul><li>diff(f, ‘a’) % wrt a </li></ul><ul><li>ans = </li></ul><ul><li>x^3 </li></ul><ul><li>diff(f,’a’,2) % double diff wrt a </li></ul><ul><li>ans = </li></ul><ul><li>0 </li></ul>
76. 77. Symbolic Integration <ul><li>f = sin(s+2*x) </li></ul><ul><li>int(f) </li></ul><ul><li>ans = </li></ul><ul><li>-1/2*cos(s+2*x) </li></ul><ul><li>int(f,’s’) </li></ul><ul><li>ans = </li></ul><ul><li>-cos(s+2*x) </li></ul><ul><li>int(f, ‘s’, pi/2,pi) </li></ul><ul><li>ans= </li></ul><ul><li>-cos(s) </li></ul>
77. 78. Comments &Punctuation (1) <ul><li>All text after a percentage sign (%) is ignored </li></ul><ul><li>>> % this is a comment </li></ul><ul><li>Multiple commands can be placed on one line separated by commas (,) </li></ul><ul><li>>> A = magic(4), B = ones(4), C = eye(4) </li></ul>
78. 79. Comments &Punctuation (2) <ul><li>A semicolon may be also used, either after a single command or multiple commands </li></ul><ul><li>>> A = magic(4); B = ones(4); C = eye(4); </li></ul><ul><li>Ellipses (…) indicate a statement is continued on the next line </li></ul><ul><li>A = B/… </li></ul><ul><li>C </li></ul>
79. 80. SAVE Command (1) <ul><li>>> save </li></ul><ul><li>Store all the variables in binary format in a file called matlab.mat </li></ul><ul><li>>> save fred </li></ul><ul><li>Store all the variables in binary format in a file called fred.mat </li></ul><ul><li>>> save a b d fred </li></ul><ul><li>Store the variables a, b and d in fred.mat </li></ul>
80. 81. SAVE Command (2) <ul><li>>> save a b d fred -ascii </li></ul><ul><li>Stores the variables a, b and d in a file called fred.mat in 8-bit ascii format </li></ul><ul><li>>> save a b d fred -ascii -double </li></ul><ul><li>Stores the variables a, b and d in a file called fred.mat in 16-bit ascii format </li></ul>
81. 82. Load Command <ul><li>Create a text file called mymatrix.dat with </li></ul><ul><ul><li>16.0 3.0 2.0 13.0 </li></ul></ul><ul><ul><li>5.0 10.0 11.0 8.0 </li></ul></ul><ul><ul><li>9.0 6.0 7.0 12.0 </li></ul></ul><ul><ul><li>4.0 15.0 14.0 1.0 </li></ul></ul><ul><li>“ load mymatrix.dat”, create variable mymatrix </li></ul>
82. 83. M-Files <ul><li>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” </li></ul><ul><li>So mymatrix.m </li></ul><ul><ul><ul><ul><ul><li>A = [… </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>16.0 3.0 2.0 13.0 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>5.0 10.0 11.0 8.0 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>9.0 6.0 7.0 12.0 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>4.0 15.0 14.0 1.0]; </li></ul></ul></ul></ul></ul><ul><li>type mymatrix </li></ul>
83. 84. IF Condition if condition {commands} end If x = 2 output = ‘x is even’ end
84. 85. WHILE Loop while condition {commands} end X = 10; count = 0; while x > 2 x = x / 2; count = count + 1; end
85. 86. 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
86. 87. Creating a function <ul><li>function a = gcd(a,b) </li></ul><ul><li>% GCD Greatest common divisor </li></ul><ul><li>% gcd(a,b) is the greatest common divisor of </li></ul><ul><li>% the integers a and b, not both zero. </li></ul><ul><li>a = round(abs(a)); b = round(abs(b)); </li></ul><ul><li>if a == 0 & b == 0 </li></ul><ul><li>error('The gcd is not defined when both numbers are zero') </li></ul><ul><li>else </li></ul><ul><li>while b ~= 0 </li></ul><ul><li>r = rem(a,b); </li></ul><ul><li>a = b; b = r; </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
87. 88. Quick Exercise (!) <ul><li>Consider Polynomial Addition again : </li></ul><ul><li>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. </li></ul><ul><li>Answers on a postcard to : dgordon@maths.kst.dit.ie </li></ul><ul><li>oh, and while you’re here anyhow, if you have a browser open, please go to the following sites : </li></ul><ul><li>http://www.the hungersite.com </li></ul><ul><li>http://www.hitsagainsthunger.com </li></ul>
88. 89. Flash Gordon and the Mud Men of Matlab
89. 90. Quick Exercise (!) <ul><li>Consider Polynomial Addition again : </li></ul><ul><li>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. </li></ul><ul><li>Answers on a postcard to : dgordon@maths.kst.dit.ie </li></ul><ul><li>oh, and while you’re here anyhow, if you have a browser open, please go to the following sites : </li></ul><ul><li>http://www.the hungersite.com </li></ul><ul><li>http://www.hitsagainsthunger.com </li></ul>
90. 91. Creating Programs <ul><li>Title : Program.m </li></ul>function out = program(inputs) % PROGRAM <code> C:
91. 92. Know Thyself <ul><li>Where am I ? </li></ul><ul><ul><li>pwd </li></ul></ul><ul><li>Get me onto the hard disk </li></ul><ul><ul><li>cd C: </li></ul></ul><ul><li>Where am I now ? </li></ul><ul><ul><li>pwd </li></ul></ul><ul><li>Get me to where I know </li></ul><ul><ul><li>cd .. </li></ul></ul>
92. 93. Quick Answer (!) function c = mypoly(a,b) % MYPOLY Add two polynomials of variable lengths % mypoly(a,b) add the polynomial A to the polynomial % B, even if they are of different length % % Author: Damian Gordon % Date : 3/5/2001 % Mod'd : x/x/2001 % c = [zeros(1,length(b) - length(a)) a] + [zeros(1, length(a) - length(b)) b];
93. 94. Recursion <ul><li>function b = bart(a) </li></ul><ul><li>%BART The Bart Simpson program writes on the blackboard </li></ul><ul><li>% program, Bart writes the message a few times </li></ul><ul><li>% and then goes home to see the Simpsons </li></ul><ul><li>if a == 1 </li></ul><ul><li>disp( 'I will not....'); </li></ul><ul><li>else </li></ul><ul><li>disp( 'I will not skateboard in the halls'); </li></ul><ul><li>bart(a - 1); </li></ul><ul><li>end </li></ul>
94. 95. Curve Fitting <ul><li>What is the best fit ? </li></ul><ul><ul><li>In this case least squares curve fit </li></ul></ul><ul><li>What curve should be used ? </li></ul><ul><ul><li>It depends... </li></ul></ul>
95. 96. POLYFIT : Curve Fitting <ul><li>polyfit(x,y,n) - fit a polynomial </li></ul><ul><li>x,y - data points describing the curve </li></ul><ul><li>n - polynomial order </li></ul><ul><li>n = 1 -- linear regression </li></ul><ul><li>n = 2 -- quadratic regression </li></ul>
96. 97. Curve Fitting Example <ul><li>x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1]; </li></ul><ul><li>y = [-.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2]; </li></ul><ul><li>polyfit(x,y,n) </li></ul><ul><li>n = 1 </li></ul><ul><ul><li>p = 10.3185 1.4400 </li></ul></ul><ul><li>n = 2 </li></ul><ul><ul><li>p = -9.8108 20.1293 -0.0317 </li></ul></ul><ul><ul><li>y = -9.8108x 2 + 20.1293x - 0.0317 </li></ul></ul>
97. 98. Curve Fitting Example <ul><li>xi = linspace(0,1,100); </li></ul><ul><li>z = polyval(p,xi) </li></ul><ul><li>plot(x,y,'o',x,y,xi,z,':'); </li></ul>
98. 99. Interpolation - 1D <ul><li>t = interp1(x,y,.75) </li></ul><ul><li>t = </li></ul><ul><li>9.5200 </li></ul><ul><li>also </li></ul><ul><li>interp1(x,y,.75,’spline’) </li></ul><ul><li>interp1(x,y,.75,’cubic’) </li></ul>
99. 100. Interpolation - 2D <ul><li>interp2(x,y,Z,xi,yi,TYPE) </li></ul>TYPE = 'nearest' - nearest neighbor interpolation 'linear' - bilinear interpolation 'cubic' - bicubic interpolation 'spline' - spline interpolation
100. 101. Fourier Functions <ul><li>fft </li></ul><ul><li>fft2 </li></ul><ul><li>ifft </li></ul><ul><li>ifft2 </li></ul><ul><li>filter </li></ul><ul><li>filter2 </li></ul><ul><li>fftshift </li></ul><ul><li>Fast fourier transform </li></ul><ul><li>2-D fft </li></ul><ul><li>Inverse fft </li></ul><ul><li>2-D Inverse fft </li></ul><ul><li>Discrete time filter </li></ul><ul><li>2-D discrete tf </li></ul><ul><li>shift FFT results so -ve freqs appear first </li></ul>
101. 102. Tensors <ul><li>See ‘Programs’ </li></ul><ul><li>‘ Tensors’ </li></ul><ul><li>‘ Tensors.html’ </li></ul>
102. 103. 3D Graphics <ul><li>T = 0:pi/50:10*pi; </li></ul><ul><li>plot3(sin(t), cos(t), t); </li></ul>
103. 104. 3D Graphics <ul><li>title('Helix'), xlabel('sin(t)'), </li></ul><ul><li>ylabel('cos(t)'), zlabel('t') </li></ul><ul><li>grid </li></ul>
104. 105. 3D Graphics <ul><li>Rotate view by elevation and azimuth </li></ul><ul><li>view(az, el); </li></ul><ul><li>view(-37.5, 60); </li></ul>