MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer http://gdjpkc.xmu.edu.cn MATLAB Lecture 8-Graphics-Curve 绘图 Ref: MATLAB- Graphics- Basic Plotting Commands Vocabulary: plot绘图 轴 display显示 annotate注释 curve/ curved line曲线 explicit显式 implicit隐式 parameter参数 polar coordinates极坐标 boldface黑体 grid网格 tite标题 figure图形 legend图例 radian 弧度 radius半径 ● Some functions plot ezplot polar plot title xlabel ylabel *figure biplot grid on *grid off ● Graphics MATLAB provides a variety of functions for displaying vector data as line plots, as well as functions for annotating and printing these graphs t The representation of curve and surface The representation of curve on plane Explicit scheme y=f(x) Implicit formula: F(x, y)=0 x=x(1) Parameter representation Polar coordinates representation: p=f(e) Tabulation, say table of trigonometric function, experimental data etc Figure, say sine curve, experimental curve etc The representation of curve in space Explicit scheme: z=f(x,y) Implicit formula: F(x, y, z)=0 x=x(u, v) Parameter representation: y=y(u, v) (,v) Data form,x={x},y={y},z={n},=1,2,…,m,j=1,2,…,n
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec81 MATLAB Lecture 8 Graphics Curve 绘图 Ref: MATLAB→Graphics→Basic Plotting Commands l Vocabulary: plot 绘图 xaxis x 轴 display 显示 annotate 注释 curve/curved line 曲线 explicit 显式 implicit 隐式 parameter 参数 polar coordinates 极坐标 boldface 黑体 grid 网格 title 标题 figure 图形 legend 图例 radian 弧度 radius 半径 l Some functions plot ezplot polar plot3 title xlabel ylabel *figure *legend *subplot *grid on *grid off l Graphics MATLAB provides a variety of functions for displaying vector data as line plots, as well as functions for annotating and printing these graphs. ² The representation of curve and surface ¸ The representation of curve on plane Explicit scheme: y = f(x) Implicit formula: F(x,y) = 0 Parameter representation: ( ) ( ) x x t y y t Ï = Ì Ó = Polar coordinates representation: r = f (q ) Tabulation, say table of trigonometric function, experimental data etc. Figure, say sine curve, experimental curve etc. ¸ The representation of curve in space Explicit scheme: z = f(x,y) Implicit formula: F(x,y,z) = 0 Parameter representation: ( , ) ( , ) ( , ) x x u v y y u v z z u v Ï = Ô Ì = Ô = Ó Data form, { }, { }, { }, 1, 2,..., , 1, 2,..., i j ij x = x y = y z = z i = m j = n . Graphics
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn 2 Basic plotting Commands Plotting st The process of constructing a basic graph to meet your presentation graphics requirements is outlined in the following table. The table shows seven typical steps and some example code for each If you are performing analysis only, you may want to view various graphs just to explore your data. In this case, steps I and 3 may be all you need. If you are creating presentation graphics, you may want to fine-tune your graph by positioning it on the page, setting line styles and colors, adding annotations, and making other such Improvements I Typic 1. Prepare your data X=0:0.2:12 yl= bessel(l, x) 2. Select a window and position a plot figure(1) region within the window subplot(2, 2, 1) 3.Call elementary plotting function h=plot(x,yl, x,y2, x, y3); elect line and marker characteris- set(h Line Width, 2, 'Line Style"), ('",-,".) set(h, 'Color, f'r, g; b)) 5. Set axis limits, tick marks, and grid axis([0 12-0.]) grid 6. Annotate the graph with axis xlabel(Time,") labels. legend. and text ylabel('Amplitude legend(h, First, 'Second, Third) tle( Bessel Functions) yⅸx]=min(yl1) text(x(ix),y, First Min \ rightarrow Horizontalali 7. Export graph print-depsc-tiff-r200 myplot Plot plot(X, Y): plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up It can be used to plot of a curve expressed in explicit scheme or parameter 123456};y=[1-1.52-3-17] Lec8-2
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec82 ² Basic Plotting Commands ¸ Plotting Steps The process of constructing a basic graph to meet your presentation graphics requirements is outlined in the following table. The table shows seven typical steps and some example code for each. If you are performing analysis only, you may want to view various graphs just to explore your data. In this case, steps 1 and 3 may be all you need. If you are creating presentation graphics, you may want to finetune your graph by positioning it on the page, setting line styles and colors, adding annotations, and making other such improvements. Step Typical Code 1. Prepare your data x = 0:0.2:12; y1 = bessel(1,x); y2 = bessel(2,x); y3 = bessel(3,x); 2. Select a window and position a plot region within the window figure(1) subplot(2,2,1) 3. Call elementary plotting function h = plot(x,y1,x,y2,x,y3); 4. Select line and marker characteristics set(h,'LineWidth',2,{'LineStyle'},{'';':';'.'}) set(h,{'Color'},{'r';'g';'b'}) 5. Set axis limits, tick marks, and grid lines axis([0 12 0.5 1]) grid on 6. Annotate the graph with axis labels, legend, and text xlabel('Time') ylabel('Amplitude') legend(h,'First','Second','Third') title('Bessel Functions') [y,ix] = min(y1); text(x(ix),y,'First Min \rightarrow',... 'HorizontalAlignment','right') 7. Export graph print depsc tiff r200 myplot ¸ Plot function plot(X,Y): plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. It can be used to plot of a curve expressed in explicit scheme or parameter representation. >> x = [1 2 3 4 5 6]; y = [1 1.5 2 3 1 7]; >> plot(x, y);
MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer http:/gdjpkc.xmu.edu.cn File dit yiew Insert Tools Desktop Indos Help →日舀回长日国口 >a=16;149162536] ploa);% plot six lines(1,1)(2,4),(3,9);(4,16),(5,25);(6,36) Eile Edit Vie Insert Tools Desktop Window Help D日每令骨回日国回 Plot sin x over [0, 2T ]with title and grid >>t=0:pi/100:2°pi plot(t,y) > title( Plot of sinx vs x) %titled Plot of sinx vs x >xlabel('x #a'); ylabel('sinx) %adds text beside the X-axis Y-axis on the current axis grid on
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec83 >> a =[1:6; 1 4 9 16 25 36]; >> plot(a); %plot six lines (1, 1); (2, 4); (3, 9); (4, 16); (5, 25); (6, 36) Plotsin x over [0, 2p ] with title and grid. >> t = 0:pi/100:2*pi; >> y = sin(t); >> plot(t,y) >> title (‘Plot of sinx vs x’) %titled ‘Plot of sinx vs x’ >> xlabel(‘x 轴’); ylabel(‘sinx’) % adds text beside the Xaxis & Yaxis … on the current axis >> grid on
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http:/gdjpkc.xmu.edu.cn File Edit yiew Insert Tool: Desktop Lindow Help →日回长日国 一 Plot of sinx vsxytitle x轴)一a5 Plot the curve with x= asin mt, y=a cos nt over t E[0, 2T while a=8, m=2 and >t= linspace( 0, 2*pi); generates a row vector of 100 linearly equally spaced points between 0 and 2 T >X=a°sin(m*t); >y=a°cos(n°t) plot(x, y, 'b-7) color blue, line style"- FIgure 1 File Edit view Insert Tools Desktop Window Help y DG副舀心回|国| 10 10 Lec8-4
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec84 Plot the curve with x = a sin mt, y = a cos nt over t Œ[0, 2p ] while a=8, m=2 and n=5. >> t = linspace(0,2*pi); % generates a row vector of 100 linearly equally spaced… points between 0 and 2p . >> a=8; >> m=2; >> n=5; >> x = a*sin(m*t); >> y = a*cos(n*t); >> plot (x, y, ‘b’) % color blue, line style ‘’
MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer http://gdjpkc.xmu.edu.cn v Polar Plots polar(THETA, RHO)makes a plot using polar coordinates of the angle THETA,in radians versus the radius rho g=0.5 >> theta= linspace(0, 2*pi, 51) %generates 51 linearly equally spaced points polar(theta, gain, r-') %color red, line style solid title('\bf Gain versus angle \theta):%boldface, Theta will be display as 8 9Figure I Eile Edit Lie Insert Tools Desktop lindow Help theta Ezplot Function--Easy to use function plotter It is usually used to plot a curve expressed in implicit formula ezplot( FUn) plots the function FUN(X) over the default domain-2 pi<X<2 pi ezplot(FUN, [A, BD plots FUN(X)over A<X<B Plot the function x2-2x+1, over x∈[-2x,2丌 eplo(x^2-2*x+1) Figure 1 Edt互 N Insert o1s正 estep Rindos He D母↓间平日口
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec85 ¸ Polar Plots polar(THETA, RHO) makes a plot using polar coordinates of the angle THETA, in radians, versus the radius RHO. >> g=0.5; >> theta = linspace(0,2*pi,51); %generates 51 linearly equally spaced points… between 0 and 2p . >> gain = 2*g*(1+cos(theta)); >> polar (theta, gain, ‘r’) %color red, line style solid‘’ >> title (‘\bf Gain versus angle \theta’); %boldface, \theta will be display as q ¸ Ezplot Function Easy to use function plotter It is usually used to plot a curve expressed in implicit formula. ezplot(FUN) plots the function FUN(X) over the default domain 2*pi > ezplot('x^2 2*x + 1');
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University tp: /gdjpkc. xmu. edu.cn Plot x+y=2, over x,yE[-2, 2 > ezplot(x.*4+y4-2~43,[-44]) 口佥日舀日回长日 v Plot 3 Function Plot lines and points in 3-D space plot(x,y, z), where x, y and z are three vectors of the same length, plots a line in 3-space through the points whose coordinates are the elements of x, y and z plot(X,Y, Z), where X, Y and Z are three matrices of the same size, plots several lines obtained from the columns of x.y and Z Various line types, plot symbols and colors may be obtained with plot(X,Y, Z, s) where s is a 1, 2 or 3 character string made from the characters listed under the plot command >t=0pi/50:10*pi; plot(sin(t), cos(t), t); Figure I Eile Edit yie Insert Ioos Desktop window Belp Lec8-6
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec86 Plot 4 4 4 x + y = 2 , over x, y Œ[-2, 2]. >> ezplot(‘x.*4+ y.^42^4’, [4,4]) ¸ Plot3 Function Plot lines and points in 3D space. plot3(x,y,z), where x, y and z are three vectors of the same length, plots a line in 3space through the points whose coordinates are the elements of x, y and z. plot3 (X,Y,Z), where X, Y and Z are three matrices of the same size, plots several lines obtained from the columns of X, Y and Z. Various line types, plot symbols and colors may be obtained with plot3 (X,Y,Z,s) where s is a 1, 2 or 3 character string made from the characters listed under the plot3 command. >> t = 0:pi/50:10*pi; >> plot3(sin(t),cos(t),t);
MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer tp: /gdjpkc. xmu. edu.cn t=0:pi/30:9*pi;a=20;c=3 >x=at *sin(t); y=a*t *cos(t):z=c*t; > plot(x,y, z, 'mo) magenta, line type circle View Insert Icols Qeakte? Lindow ulp 的回日m曰 Compare ezplot3(20t. sin(t), 20*t *cos(t), 3 t, [0 9*pi],'mo) y2=coS(x); > plot(x, yl, b-'): %blue color solid(-) line style hold on: %after this command, all additional plots will be laid on top of the previously existing plot le > hold off, %switches plotting behavior back to the default situation, in which A new plot replaces the previous one >>x=-pi: pi/20: pi; >yl=sin(x);
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec87 >> t = 0:pi/30:9*pi; a = 20; c = 3; >> x = a*t.*sin(t); y = a*t.*cos(t); z = c*t; >> plot3(x,y,z,’mo’) %color magenta, line type circle Compare >> ezplot3('20*t.*sin(t)','20*t.*cos(t)','3*t',[0 9*pi],’mo’) ² Plotting Multiple Plots on the Same Axes >> x = pi:pi/20:pi; >> y1 = sin(x); >> y2 = cos(x); >> plot (x, y1, ‘b’ ); %blue color solid() line style >> hold on; %after this command, all additional plots will be laid on top of the .. previously existing plots. >> plot (x, y2, ‘k’); %black color dashed() line style >> hold off; %switches plotting behavior back to the default situation, in which … A new plot replaces the previous one >> legend (‘sin x’, ‘cosx’); Or >> x = pi:pi/20:pi; >> y1 = sin(x);
MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer tp: /gdjpkc. xmu. edu.cn >>y2=c0s(x) plot(x, yl, b-',x, y2, k--) >legend (sin x',cosx) =4线:口口 Create Multiple Figures >figure(1) %figure window 1 >x=00.05:2 > plot(x, yD) > figure(2) %create another figure window plot(x, y2); 小 Figute2 tile Eat fie Insert Tools lesktog LiadesHelp Eile Rtt ye sert Tools Desktop Yindo yule 日舀回←日■ 舀代费回长日目日回 0204 subplot(2, 1, 1) %create 2 X lsubplots in current figure, arranged in 2 rows and I column, and selects subplot I to receive all current plotting commands
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec88 >> y2 = cos(x); >> plot (x, y1, ‘b’, x, y2, ‘k’); >> legend (‘sin x’, ‘cosx’) ² Create Multiple Figures >> figure(1) %figure window 1 >> x = 0:0.05:2; >> y1 = exp(x) ; >> plot (x, y1); >> figure (2) %create another figure window 2 >> y2 = exp(x); >> plot (x, y2); ² Subplots >> subplot(2,1,1) %create 2×1subplots in current figure, arranged in 2 rows … and 1 column, and selects subplot 1 to receive all current … plotting commands >> x = pi:pi/20:pi;
MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer http://gdjpkc.xmu.edu.cn lot(x, y) tle(窗口1标题”) >> subplot(2, 1, 2) %create 2 X lsubplots in current figure, arranged in 2 rows and I column, and selects subplot 2 to receive all current plotting command >>y=cos(-X) > plot(x, y); te(窗口2标题) D回俄回长日阻■口 窗口2标题 Some examples clear; x0: pi/20: 2. " pi, y= sin(x) y1=cos(2*x): plot(x, y, --rs', linewidth, 2, markeredgecolor', k, ' markerfacecolor, 'g, 'markersize, 10) hold > plot(x,yl, -ko, markeredgecolor','k, markerfacecolor,'T, markersize, 10) >legend(sinx,"cos2x') Bile Edit View Insert Fools Desktop lindow Help Lec8-9
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec89 >> y = sin(x); >> plot (x, y); >> title (‘窗口 1 标题’); >> subplot(2,1,2) %create 2×1subplots in current figure, arranged in 2 rows … and 1 column, and selects subplot 2 to receive all current … plotting commands >> x = pi:pi/20:pi; >> y = cos(x); >> plot (x, y); >> title (‘窗口 2 标题’); ² Some examples >> clear; x=0:pi/20:2.*pi; y= sin(x); >> y1=cos(2.*x); >> plot(x,y,'rs','linewidth',2,'markeredgecolor','k','markerfacecolor','g','markersize',10) >> hold on >> plot(x,y1,'ko','markeredgecolor','k','markerfacecolor','r','markersize',10) >> legend (‘sinx’, ‘cos2x’)
MATLAB Lecture 8 School of Mathematical Sciences Xiamen Univer http://gdjpkc.xmu.edu.cn A curve fitting example(曲线拟合) > High boy=[155,1.58,1.6,1.64,1.66,1.68,1.7,1.73,1.78,1.8,1.82,1.85 >> Weight boy={60,57,57,65,63,64,70,65,68,76,72,78 > plot( High boy, Weight boy, 'b* >> hold on >Everage boy=polyfit( High boy, Weight boy, 1) >>X1,5: 0.1: 1.9, boy line=Everage boy(1)x+ Everage boy (2) plot(x, boy line, ' g-,linewidth, 1.2); 旧囟 File Edit View Insert Tools Desktop Window Help 3 D日舀心Q回日国 Appendix Various line types, plot symbols and colors may be obtained with PLoT(X,Y, S)where S is a character string made from one element from any or all the following 3 columns point X-mark dashdot magenta star (none)no line diamond triangle(up) triangle(left) triangle(right) pentagram You can use a subset of TeX commands embedded in the string to produce special characters such as Greek letters and mathematical symbols. The following table lists these characters and the character sequences used to define them Lec8-I0
MATLAB Lecture 8 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec810 A curve fitting example (曲线拟合) >> High_boy=[1.55, 1.58, 1.6, 1.64, 1.66, 1.68, 1.7, 1.73, 1.78, 1.8, 1.82, 1.85]; >> Weight_boy=[60, 57, 57, 65, 63, 64, 70, 65, 68, 76, 72, 78]; >> plot(High_boy, Weight_boy, 'b*') >> hold on; >> Everage_boy=polyfit(High_boy, Weight_boy, 1) >> x=1.5:0.1:1.9; boy_line=Everage_boy(1)* x + Everage_boy(2) >> plot(x, boy_line, 'g', 'linewidth', 1.2); ² Appendix Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: b blue . point solid g green o circle : dotted r red x xmark . dashdot c cyan + plus dashed m magenta * star (none) no line y yellow s square k black d diamond v triangle(down) ^ triangle (up) triangle(right) p pentagram h hexagram You can use a subset of TeX commands embedded in the string to produce special characters such as Greek letters and mathematical symbols. The following table lists these characters and the character sequences used to define them