응용 전산 및 실습 MATLAB – Chapter 4 그래픽 위덕대학교 에너지전기공학부 이 성 환 Chapter 4 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 4 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 4 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 4 Uiduk University
MATLAB – Easy Array Construction REVIEW 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 4 Uiduk University
MATLAB – Easy Array Construction REVIEW 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 = [ a(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 4 Uiduk University
MATLAB – Reading From Data File REVIEW << 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 4 Uiduk University
MATLAB – Plot Procedure Plot 할 데이터 준비 (x, y 축의 데이터 생성 / Reading Data) Single graphic / Multi graphic 선택 Plot 함수 호출 Line 이나 marker 의 속성 선택 축의 한계값, grid line 등을 설정 xlabel, legend, text 등으로 라벨링 그래픽 객체 출력 Chapter 4 Uiduk University
MATLAB – Single Plot (1) Plot 할 데이터 준비 >> x = 0:0.01:10; >> y = sin(x) .* cos(x + pi/2); >> size(x) ans = 1 1001 >> size(y) Single graphic / Multi graphic 선택 >> figure(1) Chapter 4 Uiduk University
MATLAB – Single Plot (2) Plot 함수 호출 >> plot(x, y); Line 이나 marker 의 속성 선택 >> set(plot(x,y), 'LineStyle', '--'); Chapter 4 Uiduk University
MATLAB – Single Plot (3) 축의 한계값, grid line 등을 설정 >> axis([1, 5, -0.5, 0]); >> grid on; xlabel, legend, text 등으로 라벨링 >> xlabel('x'); >> ylabel('y'); >> title('Plotting x and y'); 그래픽 객체를 출력 >> print –dbitmap d:\ex1.bmp >> print –djpeg d:\ex1.jpg >> print –dtiff d:\ex1.tif Chapter 4 Uiduk University
MATLAB – Plot Function (Linear Plot) Form Meaning Plot(y) Plots the columns of y versus their index Plot(x, y) Plots vector y versus vector x Plot(x, y, s) s is a character string made from the table on the next page Plot(x1, y1, s1, x2, y2, s2, … ) Combines the plots defined by the (x, y, s) triples Chapter 4 Uiduk University
MATLAB – Plot Options Color Point Mark Line Style b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star y yellow s square k black d diamond v triangle(down) ^ triangle(up) < triangle(left) > triangle(right) p pentagram h hexagram Chapter 4 Uiduk University
MATLAB – Plot Example (1) >> y = [1, 6, 9, 7] ; >> plot(y); EX2) >> y = [1, 6, 9, 7] ; >> plot(y, 'rs:'); Chapter 4 Uiduk University
MATLAB – Plot Example (2) >> y = x.^3 - 6*x.^2 + 11*x - 6; >> plot(x, y); >> axis([0, 4, -5, 5]); EX4) >> x = 0:0.01:10; >> y = x.^3 - 6*x.^2 + 11*x - 6; >> plot(x, y, 'b:'); >> axis([0, 4, -5, 5]); Chapter 4 Uiduk University
MATLAB – Plot Example (3) >> y = x.^3 - 6*x.^2 + 11*x - 6; >> plot(x, x – x, x, y); >> axis([0, 4, -5, 5]); EX6) >> x = 0:0.01:10; >> y = x.^3 - 6*x.^2 + 11*x - 6; >> y1 = 3 * sin(3.*x); >> plot(x, x - x, 'k-', x, y, 'b-.', x, y1, 'k-'); >> axis([0, 4, -5, 5]); Chapter 4 Uiduk University
MATLAB – SubPlot Function Form Meaning subplot(m, n, i) Breaks the Figure window into an m X n matrix of small axis and selects the i’th axis for the current plot subplot(mni) EX7) >> x1 = 0:0.1:10; >> f1 = sin(x1) .* cos(x1 + pi / 2); >> subplot(221); >> plot(x1, f1); >> x2 = 0:0.1:100; >> f2 = x2.^4 - 0.4*x2.^3 + 10*x2.^2 - 30*x2 + 5; >> subplot(222); >> plot(x2, f2); >> x3 = 0:0.1:100; >> f3 = -(0.4*x3.^3).*sin(x3); >> subplot(223); >> plot(x3, f3); >> grid on; >> xlabel('x3'); >> ylabel('y3'); >> x4 = 1:1:200; >> f4 = sqrt(x4).*log(x4); >> subplot(224); >> plot(x4, f4); Chapter 4 Uiduk University