응용 전산 및 실습 MATLAB – Chapter 3 행렬연산 위덕대학교 에너지전기공학부 이 성 환 Chapter 3 Uiduk University
MATLAB – Scripting M-File REVIEW MATLAB 메뉴의 File New M-File 선택 M-File Editor 가 실행되며 다음과 같이 화면에 나타남 원하는 MATLAB 명령어 입력 (명령어 입력 시 절대 한글을 사용하지 말 것!) File Save 를 선택하고 원하는 경로에 원하는 파일 이름으로 저장 (파일 이름에 절대 한글을 쓰지 말 것!) Debug Run 을 선택하여 생성한 M-File을 실행하면 MATLAB Command Window에 결과가 나타남 Chapter 3 Uiduk University
MATLAB – Enhancing M-File REVIEW Keyword / Function Meaning Example ; Suppress output radius = 5; % Comment radius = 5; % 반지름 값 disp(variable) Display results without identifying variable names disp(radius) echo Control the ‘Command Window’ echoing of script file commands input Prompt user for input radius = input(‘반지름을 입력하세요> ‘); pause Pause until user presses any key pause(n) Pause for n seconds, then continue pause(3) fprintf(format, var1, var2,,,) Print msg fprintf(‘계산이 완료되었습니다.’); fprintf(‘반지름은 %f 입니다.\n’, radius); waitforbuttonpress Pause until user presses mouse button or keyboard key Chapter 3 Uiduk University
MATLAB – Array Definition MATLAB – Array Calculation REVIEW Keyword Meaning [ ] 배열의 시작과 끝 ; 행 구분 , 열 구분 MATLAB – Array Calculation Operation Symbol Example Addition, a + b + A + B Subtraction, a – b - A – B Multiplication, a • b * A * B Division, a b / or inv() A / B = A * inv(B) Exponentiation, ab ^ A ^ 2 Transposition, aT ‘ A’ Chapter 3 Uiduk University
MATLAB – Solving Linear Equations REVIEW EX6) Solving the following linear equations >> A = [1, -1, 2; 3, 2, 9; 0, 1, -4] A = 1 -1 2 3 2 9 0 1 -4 >> B = [10; 9; 3] B = 10 9 3 >> x = inv(A) * B x = 6.4783 -4.0435 -0.2609 Chapter 3 Uiduk University
MATLAB – Scalar and Array Operation REVIEW Element-By-Element Operation Representative Data Scalar Addition Scalar Multiplication Array Addition Array Multiplication Array Division Array Exponentiation Chapter 3 Uiduk University
MATLAB – Standard Arrays REVIEW Keyword Description ones(n) ones(m, n) Create n x n arrays containing all ones Create m x n arrays containing all ones zeros(n) zeros(m, n) Create n x n arrays containing all zeros Create m x n arrays containing all zeros eye(n, n) eye(m, n) Produce n x n identity matrices Produce m x n identity matrices rand(n) rand(m, n) Uniformly distributed n x n random arrays Uniformly distributed m x n random arrays randn(n) randn(m, n) Zero-mean, unit-variance normal distribution n x n, m x n random arrays size(A) Return row & column size Chapter 3 Uiduk University
MATLAB – Easy Array Construction Array Construction Technique Description x = [ 2, 2*pi, sqrt(5), 2-3j ] Create row vector x containing element specified x = first : last Create row vector x starting with first, counting by one, ending at or before last x = first : increment : last Create row vector x starting with first, counting by increment, ending at or before last x = linspace(first, last, n) Create row vector x starting with first, ending at last, having n elements x = logspace(first, last, n) Create logarithmically-spaced row vector x starting with 10first, ending at 10last, having n elements Chapter 3 Uiduk University
MATLAB – Easy Array Construction EX1) >> c = [1, 6, 9, 7] c = 1 6 9 7 >> a = 1 : 5 a = 1 2 3 4 5 >> b = 1 : 2 : 7 b = 1 3 5 7 >> x = (0 : 0.1 : 0.3) * pi x = 0 0.3142 0.6238 0.9425 EX2) >> d = [ 1:2:5, 1, 0, 1 ] d = 1 3 5 1 0 1 >> a = 1 : 3, b = 1 : 2 : 7 a = 1 2 3 b = 1 3 5 7 >> c = [ b, a ] c = 1 3 5 7 1 2 3 EX3) >> x = linspace(2, 6, 3) x = 2 4 6 >> x = logspace(0, 2, 11) 1.0000 1.5849 2.5119 3.9811 6.3096 10.0000 15.8489 25.1189 39.8107 63.0957 100.0000 Chapter 3 Uiduk University
MATLAB – Complex Array’s Transpose Pitfall 1 2 3 >> d = a + i * a d = 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i >> e = d’ % Complex conjugate transpose of d e = 1.0000 - 1.0000i 2.0000 - 2.0000i 3.0000 - 3.0000i >> f = d.’ % Dot transpose = Transpose of d 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i Chapter 3 Uiduk University
MATLAB – Array Addressing Description A(r, c) Indicates the elements by the r’th row and the c’th column A(r, : ) Addresses a subarray within A defined by the index vector of desired rows in r and all columns A( : , c) Addresses a subarray within A defined by all rows and the index vector of desired columns in c A( a : b , c : d) Addresses a subarray within A intersected by the rows from a to b and the columns from c to d Chapter 2 Uiduk University
MATLAB – Array Addressing EX5) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = 1 2 3 4 5 6 7 8 9 >> A(3, 3) = 0 7 8 0 >> A(2, 6) = 1 % extends array A 1 2 3 0 0 0 4 5 6 0 0 1 7 8 0 0 0 0 EX6) >> A( : , 4) = 4 A = 1 2 3 4 0 0 4 5 6 4 0 1 7 8 0 4 0 0 >> B = A(1 : 2, 2 : 3) B = 2 3 5 6 >> A( :, 2) = [ ] % removes 2nd column 1 3 4 0 0 4 6 4 0 1 7 0 4 0 0 Chapter 3 Uiduk University
MATLAB – Array Addressing EX7) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = 1 2 3 4 5 6 7 8 9 >> B = [1, 4, 7] B = 1 4 7 >> A(2, : ) = B % substitutes 2nd row Chapter 3 Uiduk University
MATLAB – Array Manipulation Function Description rot90(A) rot90(A, k) Rotates vector A by 90 degrees Rotates vector A by 90 degrees * k (k > 0 = ccw, k < 0 = cw) flipud(A) Flips array in up-down direction fliplr(A) Flips array in the left-right direction triu(A) Extracts upper triangular part tril(A) Extracts lower triangular part diag(A) Extracts diagonal elements cross(A, B) Outer product of A and B : dot(A, B) = sum(A.*B) Inner product of A and B : Chapter 3 Uiduk University
MATLAB – Array Manipulation EX8) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = 1 2 3 4 5 6 7 8 9 >> flipud(A) ans = >> fliplr(A) 3 2 1 6 5 4 9 8 7 EX9) >> rot90(A) ans = 3 6 9 2 5 8 1 4 7 >> diag(A) 1 5 9 >> diag(ans) 1 0 0 0 5 0 0 0 9 EX10) >> triu(A) ans = 1 2 3 0 5 6 0 0 9 >> A = [1, 2, 3]; >> B = [4, 5, 6]; >> dot(A, B) 32 >> cross(A, B) -3 6 -3 Chapter 3 Uiduk University
MATLAB – Reading From Data File << mat.txt>> 파일의 내용 0.7918 1.2590 -0.2172 -0.9242 2.6448 0.1716 -0.5228 0.3639 4.5700 -0.1982 0.0586 0.1214 6.4240 -0.0158 0.0763 -0.0615 8.3750 0.0303 -0.0131 -0.0144 10.2545 0.0003 -0.0104 0.0101 12.2296 -0.0044 0.0026 0.0014 14.1076 0.0003 0.0013 -0.0016 16.0704 0.0006 -0.0005 -0.0001 18.2613 -0.0001 -0.0001 0.0002 >> load mat.txt >> t = mat( : , 1); % 시간 벡터 >> x = mat( : , 2); % 변위 벡터 >> v = mat( : , 3); % 속도 벡터 >> a = mat( : , 4); % 가속도 벡터 Chapter 3 Uiduk University
MATLAB – Multidimensional Arrays >> A = zeros(2, 3) % start with a 2-D array A = 0 0 0 >> A( : , : , 2) = ones(2, 3) % add a second page to go 3-D ! A( : , : , 1) = A( : , : , 2) = 1 1 1 Chapter 3 Uiduk University