Ch 7 Plotting in MATLAB 7 Objectives p

Ch 7 Plotting in MATLAB 제 7 장 그래프 작성

Objectives p p p p 2차원 그래프 작성 Line style, markers, color 수정 Grid 와 축(axis) 수정 그래프 안에 글자 넣기 그래프 안에 수치 넣기 SEMILOGX, SEMILOGY, LOGLOG, POLAR, COMET, SUBPLOT 함수 이용 그래프 윈도우의 조정

7. 1 Basic Two-Dimensional Plots plot(x, y) : x, y 값은 1 차원 데이터 배열 (원소 수 동일) >> >> >> x=linspace(0, 4*pi, 200); y=sin(x); plot(x, y); title('A simple plot using MATLAB') ylabel('Sin(x)'); xlabel('x (Radians)');

7. 1 Basic Two-Dimensional Plots hold on : 현재의 plot에 새 plot 추가 >> >> >> x=linspace(0, 4*pi, 200); y=sin(x); plot(x, y); title ('A simple plot using MATLAB') ylabel('Sin(x)'); xlabel('x (Radians)'); hold on plot(x, cos(x)) z=abs(sin(x)); plot(x, z); zz=sin(x). *sin(x); plot(x, zz);

7. 1 Basic Two-Dimensional Plots hold off : 기존의 plot를 지우고 새 plot를 작성 >>hold off >>x=linspace(0, 4*pi, 200); >>y=sin(x); >>plot(x, y, x, cos(x)); >>xlabel('x - (Radians)'); >>title('Sine and Cosine Functions');

7. 1 Basic Two-Dimensional Plots >> >> x=linspace(0, 4*pi, 200); y=sin(x); z=abs(sin(x)); zz=sin(x). *sin(x); plot(x, y, x, cos(x), x, zz); xlabel('x - (Radians)'); title('Various Functions'); legend('sin(x)', 'cos(x)', 'abs(sin(x))', 'sin(x)*sin(x)') 선의 색이 다름

7. 1 Basic Two-Dimensional Plots By making x a column array, but same x-coordinate 행을 열로 바꿈 >> >> >> x x=linspace(0, 4*pi, 200)’; y=[sin(x), cos(x), abs(sin(x)), sin(x). *sin(x)]; plot(x, y); legend('sin(x)', 'cos(x)', 'abs(sin(x))', 'sin(x)*sin(x)') xlabel('x - (Radians)'); title('Various Trigonometric Functions'); y

7. 1 Basic Two-Dimensional Plots Same number of points, but different x- and y- coordinates x y >> >> >> >> x 1=linspace(0, pi, 10)'; x 2=linspace(pi, 2*pi, 10)'; x 3=linspace(2*pi, 3*pi, 10)'; x 4=linspace(3*pi, 4*pi, 10)'; x=[x 1, x 2, x 3, x 4]; y 1=sin(x 1); y 2=cos(x 2); y 3=abs(sin(x 3)); y 4=sin(x 4). *sin(x 4); y=[y 1, y 2, y 3, y 4]; plot(x, y) legend('sin(x)', 'cos(x)', 'abs(sin(x))', 'sin(x)*sin(x)', 4) xlabel('x - Radians'); title('Various Trigonometric Function'); Version 5 이상 우측하단에

7. 1 Basic Two-Dimensional Plots Different number of points >> >> >> x 1=linspace(0, 2*pi, 10); x 2=linspace(0, 2*pi, 50); x 3=linspace(pi, 2*pi, 100); x 4=linspace(pi/3, 2*pi, 150); y 1=sin(x 1); y 2=cos(x 2); y 3=abs(sin(x 3)); y 4=sin(x 4). *sin(x 4); plot(x 1, y 1, x 2, y 2, x 3, y 3, x 4, y 4); legend('sin(x)', 'cos(x)', 'abs(sin(x))', 'sin(x)*sin(x)', 4) xlabel('x - Radians'); title('Various Trigonometric Function');

7. 2 Line Styles, Markers, and Colors plot (x 1 , y 1 , s 1 , Color code Color y m c r g b w k yellow magenta (자홍) cyan (청록) red green blue white black x 2 , y 2 , s 2 , x 3 , y 3 , s 3 …. . ) Marker code Marker Line style Code Line style . o x + * s d v ^ < > p h point circle x-mark plus star square diamond triangle(down) triangle(up) triangle(left) triangle(right) pentagram hexagram : -. -- solid dotted dashdot dashed

7. 2 Line Styles, Markers, and Colors >> >> x 1=linspace(0, 2*pi, 10); y 1=sin(x 1); plot(x 1, y 1, 'gs', x 2, y 2, 'kp: ', x 3, y 3, x 4, y 4, 'r--'); title('Various Trigonometric Function'); 3 변수를 모두 지정할 필요는 없음

7. 3 Plot Colors WHITEBG : Plot의 배경색을 바꾸는 함수 >> >> >> x 1=linspace(0, 2*pi, 10); y 1=sin(x 1); plot(x 1, y 1, 'gs', x 2, y 2, 'kp: ', x 3, y 3, x 4, y 4, 'r--'); xlabel('x-Radians'); title('Varous Trigonometric Fuctions'); whitebg('y'); >> whitebg(‘m’)

7. 4 Plotting Grid on – 격자 표시 Grid off –격자 제거 Grid – 토글 >> >> … >> >> >> x 1=linspace(0, 2*pi, 10); y 1=sin(x 1); plot(x 1, y 1, 'gs', x 2, y 2, 'kp: ', x 3, y 3, x 4, y 4, 'r--'); legend('sin(x)', 'cos(x)', 'abs(sin(x))', 'sin(x)*sin(x)', 3) xlabel('x-Radians'); title('Varous Trigonometric Fuctions'); grid on
![7. 5 The Axis Command axis( [Xmin Xmax Ymin Ymax] ) axis auto : 7. 5 The Axis Command axis( [Xmin Xmax Ymin Ymax] ) axis auto :](http://slidetodoc.com/presentation_image_h/829cd4133749f47b0177fd024d305140/image-14.jpg)
7. 5 The Axis Command axis( [Xmin Xmax Ymin Ymax] ) axis auto : MATLAB이 축의 limits 값을 정함 …………… >> plot(x 1, y 1, 'gs', x 2, y 2, 'kp: ', x 3, y 3, x 4, y 4, 'r--'); >> grid on >> axis( [ 3 5 0 1 ] )

7. 6 Placing Text on a Plot … >> plot(x 1, y 1, 'gs', x 2, y 2, 'kp: ', x 3, y 3, x 4, y 4, 'r--'); … >> text(0. 5 , -0. 25 , ‘LEGEND’) text(x , y , ’ str ’)

7. 6 Placing Text on a Plot gtext : 마우스를 이용하여 지정한 위치에 문자 출력 >> gtext(‘This is a text’);

7. 7 Modifying Text with TEX Commands x=linspace(-4, 4, 200); y=exp(-x. *x); plot(x, y); text(-3, 0. 7, 'alphabetagammaomegazetaepsilon') text(-3, 0. 6, 'GammaOmegaSigmaLambdaTheta') text(-3, 0. 5, 'Uparrowdownarrowldotsrightarrowapproxequivsubset') (ver. 5 이상)

7. 7 Modifying Text with TEX Commands set : 그래프영역을 줄여 Plot명을 쓸 수 있게 한다. >> set(gca, 'Plotbox. Aspect. Ratio', [1, 0. 7, 1]) >> title('Plot of y=x^{3 x}+x^2+x^{-100}') >> xlabel('X_{axis} - x_1+x_2+x_{333}') >> ylabel('Y_{axis} - A Plot of an Equation')

7. 8 Obtaining Numerical Values form Plot GINPUT: 그래프에서 수치 값을 얻는 함수 figure(1) msg 1=sprintf('Place the cursor at the location of a point you wish to'); msg 1=[msg 1, sprintf('see the numerical values and then press')]; msg 1=[msg 1, sprintf('the LEFT mouse button. ')]; h=msgbox(msg 1); waitfor(h) [x 1, y 1]=ginput(1); msg 2=sprintf('Place the cursor at the location of where you would'); . msg 2=[msg 2, sprintf('like to display the values on the plot and then')]; msg 2=[msg 2, sprintf('press the LEFT mouse button. ')]; h=msgbox(msg 2); waitfor(h) [x 2, y 2]=ginput(1); str=sprintf('x=%5. 3 f, y=%5. 3 f', x 1, y 1); line([x 1, x 2], [y 1, y 2]); text(x 2, y 2, str); . h=msgbox(msg 1); % 작은 메시지 박스를 열고, msg 1 값을 표시하고, 핸들 값 h를 반환 waitfor(h) % 기다리면서, OK 버튼을 누르면 메시지 박스가 지워지며 실행을 계속 [x 1, y 1]=ginput(1); % 현재 그림에 격자를 그리며, 마우스 버튼 클릭을 기다림

7. 8 Obtaining Numerical Values form Plot

7. 8 Obtaining Numerical Values form Plot 두개의 점을 이용하여 확대 하는 예 x 1=linspace(0, 2*pi, 10); … figure(1) msg 1=sprintf('Place the cursor at the msg 1=[msg 1, sprintf('use as one corner h=msgbox(msg 1); waitfor(h) [x 1, y 1]=ginput(1); msg 2=sprintf('Place the cursor at the msg 2=[msg 2, sprintf('use as the second h=msgbox(msg 2); waitfor(h) [x 2, y 2]=ginput(1); x=[x 1, x 2]; y=[y 1, y 2]; axis([min(x), max(x), min(y), max(y)]); location of a point you wish to'); for the zoom box. ')]; location of a point you wish to'); corner for the zoom box. ')];

7. 8 Obtaining Numerical Values form Plot

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 1 SEMILOGX : x축은 로그 스케일로 사용하고, y축은 선형 축을 사용하여 그래프 출력 >> >> omega=logspace(1, 4, 200); mag=20*log 10(abs(1000. /(i*omega+1000))); semilogx(omega, mag); axis([10, 10000, -20 5]); grid on; Ylabel(‘Gain(d. B)’); Xlabel(‘Frequency(rad/sec)’); Title(‘Bode Magnitude plot of Vo/Vin’);

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 2 SEMILOGY : x축은 선형 축을 사용하고, y축은 로그스케일을 사용하여 그래프 출력 >> >> >> x=linspace(0, 100, 200); y=5. *10. ^(3. *x); k=log 10(y); plot(x, k) ylabel(‘Log 10(y)’); xlabel(‘x’)

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 2 SEMILOGY x=linspace(0, 100, 200); y=5. *10. ^(3. *x); semilogy(x, y) ylabel('y'); xlabel('x');

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 3 LOGLOG : 로그 축을 사용하여 그래프 출력 >> >> >> x=linspace(0, 1, 200); y=5. *10. ^(3. *x); plot(x, y) ylabel('y'); xlabel('x'); >> >> >> x=linspace(0, 1, 200); y=5. *10. ^(3. *x); semilogy(x, y) ylabel('y'); xlabel('x');

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 3 LOGLOG >> >> >> x=linspace(0. 001, 1000); y=5. *10. ^(3. *x); plot(x, y) ylabel('y'); xlabel('x'); >> >> >> x=logspace(-3, 2, 1000); y=5. *10. ^(3. *x); loglog(x, y) ylabel('y'); xlabel('x');

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 4 POLAR polar : 극좌표 축을 사용하여 그래프 출력 >> theta=linspace(0, 8*pi, 200); >> r=2*theta; >> polar(theta, r) theta=linspace(1, 20*pi, 1000); r=5*log 10(theta); polar(theta, r)

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 5 COMET : 그래프 그리는 과정을 에니메이션으로 보여 준다. theta=linspace(1, 20*pi, 1000); r=5*log 10(theta); x=r. *cos(theta); y=r. *sin(theta); comet(x, y); title('Logarithmic Spiral');

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 6 SUBPLOT : 하나의 그림 창에 여러 개의 그래프 출력을 얻고자 할 때 subplot(2, 3, 1) theta=linspace(1, 20*pi, 1000); r=5*log 10(theta); polar(theta, r) title('Logarithmic Spiral'); subplot(2, 3, 2) theta=linspace(0, 8*pi, 200); r=2*theta; polar(theta, r) title('LInear Spiral')

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 7 Working with Multiple Figures figure : 새로운 그림 창을 만든다. clf : 그림 창에 나타나 있는 그래프 제거 EDU>> f 1=figure f 1 = 1 EDU>> f 2=figure f 2 = 2 EDU>> f 3=figure(25) f 3 = 25

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 7 Working with Multiple Figures >> >> >> >> >> subplot(2, 1, 1) omega=logspace(0, 5, 200); mag=20*log 10(abs(1000. /(i*omega+1000))); semilogx(omega, mag); axis([10, 10000 , -20 , 5]); grid on; ylabel('Gain(d. B)'); xlabel('Frequency (rad/sec)'); title('Bode Magnitude plot of Vo/Vin'); subplot(2, 1, 2); rad_to_deg=360/(2*pi); phase = rad_to_deg * angle(1000. /(i*omega+1000)); semilogx(omega, phase); grid on; ylabel('Angle (Degrees)'); xlabel('Frequency (rad/sec)'); title('Bode Phase plot of Vo/Vin');

7. 9 Various MATLAB 2 -D Plot Types 7. 9. 7 Working with Multiple Figures >> >> figure(f 1) theta=linspace(1, 20*pi, 1000); r=5*log 10(theta); polar(theta, r) >> >> >> figure(f 2) x=linspace(0, 100, 200); y=5. *10. ^(3. *x); semilogy(x, y) ylabel('y') xlabel('x')

7. 10 Handle Graphics for Manipulating Plots 7. 10. 1 Obtaining a Handle for an Object Handles from Plot Commands omega=logspace(1, 4, 200); mag=20*log 10(abs(1000. /(i*omega+1000))); hp=semilogx(omega, mag) hp = 74. 0074 axis([10, 10000, -20 5]); grid on;

7. 10 Handle Graphics for Manipulating Plots 7. 10. 1 Obtaining a Handle for an Object Text object >> hy=ylabel('Y-Axis-') hy = 20. 007 >> hx=xlabel('Time(Seconds)') hy = 21. 007 >> ht=title('Various Trigonometric Fuctions') ht = 22. 007 >> htext=text(4, 0. 8, 'Tist is a test. ') htext = 23. 007 Axes Handle (GCA) : active 상태의 figure안에 있는 axes의 해당 handles를 반 환 >> h_axes = gca ;

7. 10 Handle Graphics for Manipulating Plots 7. 10. 2 Modifying an Object with the SET Command SET : 객체(Object)의 속성을 변화시킬 수 있는 함수 text의 글꼴, 크기, 폭, 색상, 각도 등을 바꾼다 >> set(ht) Color Erase. Mode: [ { normal} Editing : [ on | off ] Font. Angle : [ { normal Font. Name Font. Size …. Button. Down. Fcn Children Clipping: [ {on} | off … Tag User. Data Visible : [ {on} | off | background | xor |none ] } | italic | oblique ] ] ]

7. 10 Handle Graphics for Manipulating Plots 7. 10. 2 Modifying an Object with the SET Command Modifying Text object >> set( ht , ‘Font. Name’, ‘Courier New’); >> set( ht, ‘Font. Size’, 24 ) >> set( ht, ’Rotation’, 90, ‘Font. Weight’, ‘bold’)

7. 10 Handle Graphics for Manipulating Plots 7. 10. 2 Modifying an Object with the SET Command Modifying Text object set(ht, 'Rotation', 0, 'Fontsize', 18, 'color', 'c') set(hy, 'Fontname', 'Arial', 'Fontsize', 18, 'color', 'r' ) set(hx, 'Fontname', 'Arial', 'Fontsize', 18, 'color', 'g' ) set(htext, 'Fontname', 'Arial', 'Fontsize', 18, 'color', 'm' ) set(h_axes, 'Fontname', 'Arial', 'Fontsize', 14, 'color', 'y' ) set(h_axes, 'Ycolor', 'Xcolor', 'g')

7. 10 Handle Graphics for Manipulating Plots 7. 10. 2 Modifying an Object with the SET Command Changing the Default Properties of Figures set(0, 'defaultlinewidth', 2); set(0, 'defaultaxesfontname', 'Arial'); set(0, 'defaultaxesfontsize', 14); set(0, 'defaultaxeslinewidth', 2); set(0, 'defaulttaxtfontsize', 14); set(0, 'defaulttaxtfontname', 'Arial');

7. 10 Handle Graphics for Manipulating Plots 7. 10. 3 Modifying Fonts with UISETFONT >> uisetfont(hy, 'specify the font y-axis. ')

7. 10 Handle Graphics for Manipulating Plots 7. 10. 4 Modifying Colors with UISETFONT >> uisetcolor(hy, ’Specify the font color for y-axis. ’)
- Slides: 41