Lec 01. Introduction to MATLAB

Download Lec 01. Introduction to MATLAB

Post on 15-Jun-2015

2.252 views

Category:

Education

13 download

TRANSCRIPT

  • 1. MATLAB ProgrammingMATLAB kmste2@kaist.ac.kr1

2. MATLAB Programming 1 20 ~ 1 29 (~) 14:00 ~ 18:00 ( 30) 50 , 10 MATLAB (2) MATLAB 7 ( kmste2@kaist.ac.kr ) ( ehgud5@kaist.ac.kr ) IT ( ita@kaist.ac.kr, 042-350-8941~3) 2 3. MATLAB ProgrammingMATLAB MATLAB GUI Interface , (, , ), , , , M- M- 2D, 3D ( , , 3 , ) , , , GUI Application 3 4. MATLAB Programming MATLAB MATLAB MATLAB / 4 5. MATLAB ProgrammingMATLAB? MATrix LABoratory total solution Script , Control System Toolbox Signal Processing Toolbox Image Processing Toolbox Wavelet Toolbox 5 6. MATLAB ProgrammingMATLAB? GUI InterfaceSimulink Model-based design tool device driver VxWare Embedded Solution MATLAB Code C, VHDL, 6 7. MATLAB ProgrammingMATLAB Early 1970s Linear algebra LINPACK , eigenvalue/eigenvector EISPACK Late 1970s Cleve Moler New Mexico LINPACK, EISPACK Fortran Cleve Moler1984 Cleve Moler Stanford . Jack Little . . MATLAB C . . Mathworks Jack Little 7 8. MATLAB ProgrammingMATLAB Cleve Moler http://www.mathworks.co.kr/company/aboutus/founders/clevemoler.html Jack Little http://www.mathworks.co.kr/company/aboutus/founders/jacklittle.html Origins of MATLAB http://www.mathworks.co.kr/videos/origins-of-matlab70332.html?type=shadow8 9. MATLAB Programming MATLAB , , curve fitting, , C++/Fortran , / , , , MATLAB C/C++ , HDL VHDL, Verilog , Cosimulation, , / 9 10. MATLAB Programming MATLAB FPGA , !! 10 11. MATLAB ProgrammingMATLAB !11 12. MATLAB ProgrammingMATLAB KAIST / MATLAB License (*@kaist.ac.kr) (*@*.kaist.ac.kr ) Mathworks http://circnts.kaist.ac.kr/ License (http://ajin.to) MATLAB MATLAB 12 13. MATLAB ProgrammingMATLAB circnts.kaist.ac.kr Etc Products MATLAB13 14. MATLAB ProgrammingMATLAB 14 15. MATLAB ProgrammingMATLAB KAIST I need to create an Account 15 16. MATLAB ProgrammingMATLAB Activation Key:16 17. MATLAB ProgrammingMATLAB (R2012b)17 18. MATLAB ProgrammingMATLAB Workspace EditorCurrent FolderCommand History Command Window18 19. MATLAB ProgrammingMATLAB Current Folder EditorCommand WindowWorkspaceWorkspace Editor Current FolderCommand History Command Window M- MATLAB MATLAB Excel spread sheet , Command History MATLAB 19 20. MATLAB ProgrammingMATLAB MATLAB Layout Default Editor 20 21. MATLAB ProgrammingCommand Window 21 22. MATLAB ProgrammingKeyboard Mapping 22 23. MATLAB ProgrammingMATLAB [] 2 x 3 , Workspace . 1. Command Window . >> A = [1 2 3; 4 5 6]2. Workspace A spread sheet .3. , X spread sheet . . >> A23 24. MATLAB ProgrammingMATLAB [] . 1. Command Window , (>>) .2. / . .3. Command History .24 25. MATLAB ProgrammingMATLAB MATLAB >> quitCommand Window >> clcMATLAB >> clear >> clear all, workspace delete , delete MATLAB >> help cos25 26. MATLAB Programming (C/JAVA ) = = , ! >> (variable) = (expression) >> A = 5 * 3 A= 15 >> a = A * 10 a= 150 ! . , , .MATLAB . a A .26 27. MATLAB Programming ? >> 5 * 3 + 6 MATLAB ans >> 5 * 3 + 6 ans = 26 ans >> ans * 3 ans = 6327 28. MATLAB Programming 6 1+ 5 8+ 2>> 8 + (6 * (1 + sqrt(5))) / 2>> exp(sqrt(3)) + pi * 10^3 MATLAB 4 103 3 3 + 103 1>> 4 / 3 * pi * 10^3>> exp(pi * sqrt(-1))or>> exp(pi * i) = 3.141592 pi e exp(1) . exp(x) i j (complex number) 28 29. MATLAB ProgrammingQuiz x = 5 , .3 sin 2 3 () 2 2 + 2 229 30. MATLAB ProgrammingQuiz Sol. x = 5 , .3 sin 2 3 () 2 2>> x = 5; >> - (3*sin(x)*cos(x)^2-sin(x)^3)/(x^2-2) ans = -0.0283 + 2 2>> x = 5; >> (exp(i*x)+exp(-i*x))/2 * (exp(i*x)-exp(-i*x))/(2*i) ans = -0.2720 30 31. MATLAB Programming (vector) ? : : MATLAB [ ] (space) (,) >> u = [-3 2 4 8 11] u= -3 2 4 8 11or>> u = [-3, 2, 4, 8, 11] u= -3 2 4 8 11(row) & (column) (;) >> u = [1; 2; 3] u= 1 2 3 31 32. MATLAB Programming (vector) () >> u = [-3 2 4] u= -3 2 4 >> u u= -3 2 4>> u = [-3; 2; 4] u= -3 2 4 >> u u= -3 2 4 >> u(1) ans = -3 >> u(2) ans = 2 >> u(3) ans = 4 1 >> u(4) Index exceeds matrix dimensions.32 33. MATLAB Programming [1, 2, 3, , 100] I: >> u = [1 2 3 4 5 100] .. .. ?? II: (colon operator) >> u = 1:100 !! III. linspace >> u = linspace(1,100,100)33 34. MATLAB Programming >> u = :/: : / / 1 1) -5 2 >> u = -5:2or>> u = -5:1:2 2) 2 -5 >> u = 2:-1:-5 3) 0 1 0.01 >> u = 0:0.01:134 35. MATLAB Programming linspace (linearly spaced vector) >> u = linspace(, , ) 100 1) -1 1 9 >> u = linspace(-1, 1, 9) ans = -1.0000 -0.7500 -0.5000 -0.250000.25000.50000.75001.0000 2) 1 -1 5 >> u = linspace(1, -1, 5) ans = 1.0000 0.50000 -0.5000 -1.000035 36. MATLAB Programming >> a = [1 2 3]; >> exp(a) ans = 2.7183 7.3891 20.0855 >> log(a) ans = 0 0.6931 >> sqrt(a) ans = 1.00001.09861.41421.7321 n- >> a = [1 2 3] >> a .^ 2 ans = 1 4 9>> a = [-0.4 1.2 0.6 -1.3] a= -0.4000 1.2000 0.6000 -1.3000 >> abs(a) ans = 0.4000 >> floor(a) ans = -1 1 >> ceil(a) ans = 0 2 1.20000.6000 1.3000 0-2 1-1 ! n- ^ a^3 36 37. MATLAB Programming = (1 , 2 , ), c , + = + = ( + 1 , + 2 , + ) = (1 , 2 , ) = ( 1 , 2 , ) = = ( 1 , 2 , ) / = (1 /, 2 / , /) ./ = (/1 , /2 , / )>> a*c ans = 5>> a = [1 2 3 4]; >> c = 5; >> a + c ans = 6 >> a c ans = -4 >> c a ans = 47-338-229-11101520>> c*a ans = 5101520>> a/c ans = 0.20000.40000.60000.8000>> c./a ans = 5.00002.50001.66671.250037 38. MATLAB Programming , >> x = [-1 0 1] >> y = [3 4 5] >> x + y ans = 246 >> x-y ans = -4 -4 -4 >> cross(x, y) ans = -4 8 4>> x * y ans = -3 4 -5 00 0 34 5 >> x .* y ans = -3 0 5 >> dot(x, y) ans = 2 = >> x ./ y ans = -0.3333 0 0.200038 39. MATLAB Programming , : , , , , , >> x = [5 3 8 4]; >> min(x) ans = 3 >> max(x) ans = 8 >> sum(x) ans = 20>> mean(x) ans = 5>> var(x) ans = 4.6667 >> std(x) ans = 2.160239 40. MATLAB ProgrammingQuiz 6 1 , . 6 1 9883898989929787 1) 2) 3) 4) 78869196 40 41. MATLAB ProgrammingQuiz Sol. 6 1 , . 988389898992978778869196>> s1 = [98 83 89 89]; >> s2 = [89 92 97 87]; >> s3 = [78 86 91 96]; 1) >> (s1+s2+s3)/3 2) >> sum(s1) >> mean(s1) 3) >> max(s1) 4) >> sum(s1+s2+s3) >> sum(s1+s2+s3) / 12 >> (mean(s1)+mean(s2)+mean(s3))/341 42. MATLAB Programming x 10 >> x = linspace(1,100,10) x= 1 12 23 34 4556677889 100 3~6 () >> x(3:6) x= 23Colon Operator 344556 1, 5, 7 () >> x( [1 5 7] ) x= 1 45 67 3, 7~9 >> x( [3 7:9] ) x= 23 67788942 43. MATLAB Programming x 10 >> x = linspace(1,100,10) x= 1 12 23 34 4556677889 100 3 10 >> x(3) = 10 x= 1 1210344556677889 100 5:7 -5 >> x(5:7) = -5 x= 1 12 1034-5-5-57889 100 3, 7~9 105 >> x([3 7:9]) = 105 x= 1 12 105 34-5-5 105 105 105 100 6~8 100, 200, 300 >> x(6:8) = [100 200 300] x= 1 12 105 34 -5 100 200 300 105 100 ! , 43 44. MATLAB ProgrammingQuiz 1 10 10 x , x x . , x = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) , 2 10-2, 4 10-4, 10 10-10 , (1, 8, 3, 6, 5, 4, 7, 2, 9, 0) . : Colon operator, max , , 44 45. MATLAB ProgrammingQuiz Sol. 1 10 10 x , x x . , x = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) , 2 10-2, 4 10-4, 10 10-10 , (1, 8, 3, 6, 5, 4, 7, 2, 9, 0) . : Colon operator, max , , >> x = 1:10 x= 1 2 3 4 5 6 7 >> x(2:2:10) = max(x) x(2:2:10) x= 1 8 3 6 5 4 78910290 45 46. MATLAB Programming x = [1 2 3], y = [4 5], z = [6 7] , [1 2 3 4 5 6 7] ? >> x = [1 2 3]; >> y = [4 5]; >> z = [6 7]; >> [x y z] ans = 1 2 3 ! 456 , 7 [ 6] MATLAB [x y x] . [x y] . , (column) . >> x = [1; 2; 3]; >> y = [4; 5]; >> [x; y] ans = 1 2 3 4 546 47. MATLAB Programming (Matrix)47 48. MATLAB Programming (Matrix) ? : 1 x n : n x 1 48 49. MATLAB Programming2 , (;) = = (2 x 3 ) M= 2 61 34 5>> M = [2 1 4; 6 3 5] (3 x 2 ) >> M = [2 3; 4 6; 5 1] M= 2 4 53 6 149 50. MATLAB Programming 2 = < 1> >> M = [2 1 4] M= 2 1 4 >> M = [M; 6 3 5] M= 2 1 4 6 3 5 >> M = [A; 7 3 9] M= 2 1 4 6 3 5 7 3 9 < 2> >> M = [ [2 1 4]; [6 3 5]; [7 3 9] ] < 3> >> r1 = [2 1 4]; >> r2 = [6 3 5]; >> r3 = [7 3 9]; >> M = [ r1; r2; r3 ]50 51. MATLAB Programming zeros, ones, eye, rand, randn zeros( m, n ) 0 m x n ones( m, n ) 1 m x n eye( m ) 1 m x m rand( m, n )0~1 uniform m x n randn( m, n )0~1 normal m x n magic( m ) m x m 51 52. MATLAB Programming [ 7] , m n . >> zeros(4, 3) >> ones(5, 3) >> eye(6) >> rand(3,5) >> randn(5,7)52 53. MATLAB Programming 1 = 5 92 6 103 7 114 8 , 12 (,) M(1, 1) = 1, M(2,3) = 7, M(3,2) = 10, 1 >> M = [1 2 3 4; 5 6 7 8; 9 10 11 12] M= 1 5 92 6 103 4 7 8 11 12>> M(2,3) ans = 7 53 54. MATLAB Programming 1 2 = 5 6 9 103 7 114 8 , 12Colon Operator M M( :, : ) M (( : ) M( :, k ) M k M( k, : ) M k M( m:n, : ) M m~n M( :, m:n ) M m~n M( m:n, p:q ) M m~n , p~q 54 55. MATLAB Programmingend 1 2 = 5 6 9 103 7 114 8 , 12end M( :, end ) M M( end, : ) M M( k, end ) M k , M( end, k ) M , k M( :, m:end ) M m~ M( m:end, : ) M m~ 55 56. MATLAB Programming 1 = 5 92 6 103 7 114 8 12n 1 M(1) = 1, M(2) = 5, M(3) = 9, , M(10) = 4, M(11) = 8, M(12) = 12 M(end) = 12 (:) , >> M( : ) M= 1 5 9 2 6 10 ...56 57. MATLAB Programming 1 = 2 34 5 67 8 910 11 1213 14 15 M 1, 3 >> M = [1 4 7 10 13; 2 5 8 11 14; 3 6 9 12 15]; >> M(: , [1,3]) ans = 1 7 2 8 3 9 >> M = [1 4 7 10 13; 2 5 8 11 14; 3 6 9 12 15]; >> M(: , [1,3, 1, 3]) ans = 1 7 1 7 2 8 2 8 3 9 3 957 58. MATLAB Programming 1 = 2 34 5 67 8 910 11 1213 14 15 Bit Mask >> M = [1 4 7 10 13; 2 5 8 11 14; 3 6 9 12 15]; >> B = logical([ 1 0 0 1 0 ]); >> M(: , B) ans = 1 10 2 11 3 121 = 2 3 = 14 7 10 13 5 8 11 14 6 9 12 15 001058 59. MATLAB ProgrammingQuiz 10 x 10 magic , 5 x 5 . M= 92 99 98 80 4 81 85 87 86 93 17 24 23 5 79 6 10 12 11 181 7 88 19 25 76 82 13 94 1008 14 20 21 2 83 89 95 96 7715 16 22 3 9 90 91 97 78 8467 74 73 55 54 56 60 62 61 68 42 49 48 30 29 31 35 37 36 4351 57 63 69 75 26 32 38 44 5058 64 70 71 52 33 39 45 46 2740 41 47 28 34 65 66 72 53 59ans = 80 87 24 6 1814 21 83 95 7773 60 42 29 3657 69 26 38 5041 28 65 72 5959 60. MATLAB ProgrammingQuiz Sol. 10 x 10 magic , 5 x 5 . M= 92 99 98 80 4 81 85 87 86 93 17 24 23 5 79 6 10 12 11 181 7 88 19 25 76 82 13 94 1008 14 20 21 2 83 89 95 96 7715 16 22 3 9 90 91 97 78 8467 74 73 55 54 56 60 62 61 68 42 49 48 30 29 31 35 37 36 4351 57 63 69 75 26 32 38 44 5058 64 70 71 52 33 39 45 46 2740 41 47 28 34 65 66 72 53 59ans = 80 87 24 6 1814 21 83 95 7773 60 42 29 3657 69 26 38 5041 28 65 72 59>> M = magic( 10 ); >> M(2:2:end, 2:2:end) 60 61. MATLAB Programming 1 4 7 = 2 5 8 3 6 910 13 11 14 12 15 M(:) = M( : = >> M = [1 4 7 10 13; 2 5 8 11 14; 3 6 9 12 15]; >> M( [1,3], [2,4] ) = [-5, -6; -7, -8]; >> M( [1,3], [2,4] ) = -1 M= 1 2 3-1 5 -17 8 9-1 11 -113 14 15M= 1 2 3-5 5 -77 8 9-6 11 -813 14 1561 62. MATLAB ProgrammingQuiz 10 x 10 magic , 5 x 5 magic .M= 92 98 4 85 86 17 23 79 10 11M= 99 1 80 7 81 88 87 19 93 25 24 76 5 82 6 13 12 94 18 1008 14 20 21 2 83 89 95 96 7715 16 22 3 9 90 91 97 78 8467 73 54 60 61 42 48 29 35 3674 55 56 62 68 49 30 31 37 4351 57 63 69 75 26 32 38 44 5058 64 70 71 52 33 39 45 46 2740 41 47 28 34 65 66 72 53 5992 17 4 23 86 4 23 10 10 1199 80 81 87 93 24 5 6 12 181 8 15 24 14 1 88 20 22 5 21 7 25 2 9 6 83 13 82 89 91 12 95 19 94 96 78 18 77 2567 73 54 60 61 42 48 29 35 3674 8 56 14 68 20 30 21 37 251 57 63 69 75 26 32 38 44 5058 15 70 16 52 22 39 3 46 940 41 47 28 34 65 66 72 53 5962 63. MATLAB ProgrammingQuiz Sol. 10 x 10 magic , 5 x 5 magic .M= 92 98 4 85 86 17 23 79 10 11M= 99 1 80 7 81 88 87 19 93 25 24 76 5 82 6 13 12 94 18 1008 14 20 21 2 83 89 95 96 7715 16 22 3 9 90 91 97 78 8467 73 54 60 61 42 48 29 35 3674 55 56 62 68 49 30 31 37 4351 57 63 69 75 26 32 38 44 5058 64 70 71 52 33 39 45 46 2740 41 47 28 34 65 66 72 53 5992 17 4 23 86 4 23 10 10 1199 80 81 87 93 24 5 6 12 181 8 15 24 14 1 88 20 22 5 21 7 25 2 9 6 83 13 82 89 91 12 95 19 94 96 78 18 77 2567 73 54 60 61 42 48 29 35 3674 8 56 14 68 20 30 21 37 251 57 63 69 75 26 32 38 44 5058 15 70 16 52 22 39 3 46 940 41 47 28 34 65 66 72 53 59>> M = magic( 10 ); >> M(2:2:end, 1:2:end) = magic(5)63 64. MATLAB Programming >> M = magic( 3 ); >> exp(M) ans = 1.0e+03 * 2.9810 0.0201 0.05460.0027 0.1484 8.1031>> M = [-1.3 2.4; 4.3 -2.9] M= -1.3000 2.4000 4.3000 -2.9000 0.4034 1.0966 0.0074>> log(M) ans = 2.0794 0 1.7918 1.0986 1.6094 1.9459 1.3863 2.1972 0.6931 >> sqrt(M) ans = 2.8284 1.0000 1.7321 2.2361 2.0000 3.00002.4495 2.6458 1.4142>> abs(M) ans = 1.3000 2.4000 4.3000 2.9000>> floor(M) ans = -2 2 4 -3>> ceil(M) ans = -1 3 5 -264 65. MATLAB Programming m x n , c , + = + = / ./>> M = magic(2); >> c = 5;>> a*c ans = 5 15 20 10>> M + c ans = 6 8 9 7>> c*a ans = 5 15 20 10>> M c ans = -4 -2 -1 -3>> a/c ans = 0.2000 0.80000.6000 0.4000>> c a ans = 4 2 1 3>> c./a ans = 5.0000 1.25001.6667 2.500065 66. MATLAB Programming ,>> x .* y ans = 0 2 -3 12 10 24>> x = [1 2 3; 4 5 6] >> y = [0 1 -1; 3 2 4] >> x+y ans = 1 73 74 2 >> x ./ y ans = Inf 2.0000 -3.0000 1.3333 2.5000 1.50002 10>> x-y ans = 1 1 1 3 n- >> x .^ 3 ans = 1 8 27 64 125 216 66 67. MATLAB Programming 3 2 3 , = 5 5 6 11 = 42 6 3n x d , d x m >> A = [1 2 3; 4 5 6]; >> B = [3 2; 5 6; 1 3]; >> A * B ans = 16 4323 5667 68. MATLAB ProgrammingQuiz Cayley-Hamilton . (I ) = , 2 + + = 0 =2 1 , 2 6 + 9 = 0 . 1 468 69. MATLAB ProgrammingQuiz Sol. Cayley-Hamilton . (I ) = , 2 + + = 0 =2 1 , 2 6 + 9 = 0 . 1 4 >> A = [2 1; -1 4] A= 2 -11 4>> A^2-6*A+9*eye(2) ans = 0 00 069 70. MATLAB ProgrammingQuiz , . -3x1 x3 = -3 2x1 + 5x2 7x3 = 1 -x1 + 4x2 + 8x3 = 2 Ax = b A-1Ax = x = A-1b m x m M , M : inv(M)70 71. MATLAB ProgrammingQuiz Sol. , . -3x1 x3 = -3 2x1 + 5x2 7x3 = 1 -x1 + 4x2 + 8x3 = 2>> A = [-3 0 -1; 2 5 -7; -1 4 8] A= -3 0 -1 2 5 -7 -1 4 8 >> b = [-3 1 2] b= -3 1 2 >> x = inv(A) * b x= 0.9124 0.2028 0.262771 72. MATLAB Programming , >> M = [5 3 8; 2 4 3]; >> min(M) ans = 2 3 3 >> max(M) ans = 5 4 8 >> sum(M) ans = 7 7 11>> mean(M) ans = 3.5000 3.5000 5.5000 >> var(M) ans = 4.5000 0.5000 12.5000 >> std(M) ans = 2.1213 0.7071 3.535572 73. MATLAB Programming [ 11] 6 1 , . 6 1 9883898989929787 1) 2) 3) 4) 78869196 , , [ 12] 6 1 , 3 , 2 .73