Plotting in MATLAB http blog naver comgajest Contents
Plotting in MATLAB http: //blog. naver. com/gajest
Contents 1. Introduction 2. 2차원 그래프 작성 3. 그래프 작성의 관련 함수의 이용 4. Exercises DSRL
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)'); >> plot(x, cos(x)) DSRL
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); DSRL
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'); DSRL
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)‘) 선의 색이 다름 DSRL
Basic Two-Dimensional Plots By making x a column array, but same x-coordinate >> >> >> 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'); DSRL
Basic Two-Dimensional Plots Same number of points, but different x- and y- coordinates >> >> >> >> 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'); DSRL
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'); DSRL
Line Styles, Markers, and Colors plot (x 1 , y 1 , s 1 , Color code y m c r g b w k Color yellow magenta cyan red green blue white black x 2 , y 2 , s 2 , x 3 , y 3 , s 3 …. . ) Marker code. o x + * s d v ^ < > p H Marker point circle x-mark plus star square diamond triangle(down) triangle(up) triangle(left) triangle(right) pentagram hexagram Line style Code Line style : -. -- solid dotted dashdot dashed DSRL
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 변수를 모두 지정할 필요는 없음 DSRL
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’) DSRL
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 DSRL
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 ] ) DSRL
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 ’) DSRL
Placing Text on a Plot gtext : 마우스를 이용하여 지정한 위치에 문자 출력 >> gtext(‘This is a text’); DSRL
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') DSRL
Modifying Text with TEX Commands set : 그래프영역을 줄여 윗 첨자를 쓸 수 있게 한다. >> 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') DSRL
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); % 현재 그림에 격자를 그리며, 마우스 버튼 클릭을 기다림 DSRL
Obtaining Numerical Values form Plot DSRL
Various MATLAB 2 -D Plot Types v 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’); DSRL
Various MATLAB 2 -D Plot Types 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’) DSRL
Various MATLAB 2 -D Plot Types x=linspace(0, 100, 200); y=5. *10. ^(3. *x); semilogy(x, y) ylabel('y'); xlabel('x'); DSRL
Various MATLAB 2 -D Plot Types v 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'); DSRL
Various MATLAB 2 -D Plot Types v 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'); DSRL
Various MATLAB 2 -D Plot Types v 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) DSRL
Various MATLAB 2 -D Plot Types v 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'); DSRL
Various MATLAB 2 -D Plot Types v 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') DSRL
Various MATLAB 2 -D Plot Types v 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 DSRL
Various MATLAB 2 -D Plot Types v 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'); DSRL
Various MATLAB 2 -D Plot Types v 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') DSRL
Handle Graphics for Manipulating Plot v 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; DSRL
Handle Graphics for Manipulating Plot v Obtaining a Handle for an Object Text object >> hy=ylabel('Y-Axis-') hy = 20. 007 >> hx=xlabel('Time(Seconds)') hx = 21. 007 >> ht=title('Various Trigonometric Functions') ht = 22. 007 >> htext=text(4, 0. 8, 'This is a test. ') htext = 23. 007 Axes Handle (GCA) : active 상태의 figure안에 있는 axes의 해당 handles를 반환 >> h_axes = gca ; DSRL
Handle Graphics for Manipulating Plot v 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 ] ] ] DSRL
Handle Graphics for Manipulating Plot v 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’) DSRL
Handle Graphics for Manipulating Plot v 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') DSRL
Handle Graphics for Manipulating Plot v 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'); DSRL
Handle Graphics for Manipulating Plot v Modifying Fonts with UISETFONT >> uisetfont(hy, 'specify the font y-axis. ') DSRL
Handle Graphics for Manipulating Plot v Modifying Fonts with UISETCOLOR >> uisetcolor(hy, ’Specify the font color for y-axis. ’) DSRL
http: //blog. naver. com/gajest
- Slides: 41