MATLAB Graphics Two-Dimensional Plots Syntax is plot(x, y) Where x = vector containing the x-coordinates y = vector containing the y-coordinates Ex: Plot the curve y=x2 where x varied from 0 to 10. Sol: >>x=0:1:10; >>y=x.^2; >>plot(x,y) Note: 1. 2. In case figure is already exits, it clears the figure widow and draw a new plot Size of vector x & y must be same Two-Dimensional Plots Ex: Plot following single variables %Note 1. a=[1 13 6 9 23 12] %one real variable 2. b=[1-2i, 3+4i, 5+6i, 3+8i, 4+9i, 23+12i] %one complex variable Sol: 1. >>a=[1 13 6 9 23 12] >>plot(a) %plot(1:length(a), a) 2. b=[1-2i, 3+4i, 5+6i, 3+8i, 4+9i, 23+12i] >>plot(b) %plot(real(b), imag(b)) Two-D Plots: Labels Ex: Plot the curve given by equation y=sin(x), as x varies from 0 to 2pi. Also label the x and y axis and provide a suitable title for the plot. Sol: Make a .m file for the following code and run it x=0:pi/100:2*pi; y=sin(x); plot(x,y); xlabel('x-axis'); %naming x-axis ylabel('y-asix'); %naming y-axis title('Plot for sine wave'); %naming the plot Two-D Plots: Grid & Axes Box Ex: Plot the curve given by equation a=10 e-t as t varies from 0 to 5. Also label the x and y axis, provide a suitable title for the plot, show the grid lines and axes box. Sol: Make a .m file for the following code and run it t=0:0.1:5; a=10*exp(-t); plot(t,a); box on; %box on or box off to on & off xlabel('Time (t) in Sec'); %naming x-axis ylabel('Output (a)'); %naming y-axis title('Plot for a=10*exp(-t)'); %naming the plot grid on; %grid on or grid off to on & off Note: Box means figure window is enclosed in the box , see plot outer portion Two-D Plots: Entering text in plot Putting a text at some desired place Syntax are text(x-coordinate, y-coordinate, ‘text to be written’) Or gtext(‘text to be written’) %put the cursor on the graph Ex: Plot the curve given by equation a=10 e-t as t varies from 0 to 5. Also label the x and y axis, provide a suitable title for the plot, show the grid lines and axes box and enter a text on the plot. Sol: Make a .m file for the following code and run it t=0:0.1:5; a=10*exp(-t); plot(t,a); box on; %box on or box off to on & off xlabel('Time (t) in Sec'); %naming x-axis ylabel('Output (a)'); %naming y-axis title('Plot for a=10*exp(-t)'); %naming the plot grid on; %grid on or grid off to on & off gtext(‘this is the desired curve’); Two-D Plots: Axis Control & Aspect Ratio Axis Control: Changing the minimum & maximum values of x & y-axis Syntax is axis([xmin, xmax, ymin, ymax]) Axis Aspect Ratio: There are some pre-defined string arguments for the axis command for setting the axis aspect ratio axis(‘equal’) %sets equal scale on both axes axis(‘square’) %sets default rectangular frame to a square frame axis(‘normal’) %rests to default values axis(‘axis”) %freezes the current axes limits axis(‘off’) %turn off all axis line, tick marks & labels Two-D Plots: Axis Control & Aspect Ratio Ex: write a .m file and run it. x=-5:0.1:10; y=3*x.^2+2.*x+5; plot(x,y); axis([-5 5 0 20]); %xmin=-5, xmax=5, ymin=0, ymax=20 xlabel('x-axis'); ylabel('y-axis'); title('Plot of y=3x^2+2x+5'); grid on; Two-D Plots: Multiple plots Multiple plots : Generating multiple plots on same graphic window. Thee methods: 1. Using plot command 2. Using hold command 3. Using line command Two-D Plots: Multiple plots conti… 1. Using ‘plot’ command: Here plot command is modified as Plot(x1, y1,x2, y2, x3, y3); It will generate 3 graphs in the same window. Ex: Make a .m file of following code and run it. x1=0:0.01:20; y1=exp(0.1*x1).*sin(x1); x2=0:0.1:20; y2=sin(x2); x3=0:0.1:20; y3=cos(x3); plot(x1,y1,x2,y2,x3,y3); gtext('curve of y1');gtext('curve of y2');gtext('curve of y3'); %axis('equal'); xlabel('x-axis'); ylabel('y-axis'); title('multiple plots using plot command'); grid on; Two-D Plots: Multiple plots conti… 2. Using ‘hold’ command: Write following command to hold the previous figure hold on or hold off Ex: Make a .m file of following code and run it. x=0:pi/100:2*pi; y1=cos(x); plot(x,y1) hold on; y2=sin(x); plot(x,y2) hold off; xlabel('x-axis'); ylabel('y-axis'); title('multiple plots using hold on & hold off command'); grid on; Note: Run the program without hold on & hold off command & see difference Two-D Plots: Multiple plots conti… 3. Using ‘line’ command: ‘line’ command is used along with plot command to generate multiple plots. Ex: Make a .m file of following code and run it. x=0:0.1:100; y1=sqrt(x.^2+1); y2=5*x+20; y3=10*x+3; plot(x,y1) gtext('y1=sqrt(x^2+1)'); line(x,y2); gtext('y2=5x+20'); line(x,y3); gtext('y3=10x+3'); xlabel('x-axis'); ylabel('y-axis'); title('multiple plots using line command'); grid on; Two-D Plots: Style option Style Option: These are optional parameters that can be given to plot command for better illustration and outlook of plots. Syntax is plot(x-value, y-value, ‘style options’) Where style options = one to three characters which represents color (red, green, yellow, blue etc) line style (solid, dashed, dotted etc) point marker style ( +, *, o, . etc ) Note: Default option in MATLAB plot is blue solid line. Two-D Plots: Style option conti… Ex: Make a .m file of following code and run it. x1=0:0.01:20; y1=exp(0.1*x1).*sin(x1); x2=0:0.1:20; y2=sin(x2); x3=0:0.1:20; y3=cos(x3); plot(x1,y1,’b:’,x2,y2,’g-’, x3,y3,’ro’); %note this line now gtext('curve of y1');gtext('curve of y2');gtext('curve of y3'); %axis('equal'); xlabel('x-axis'); ylabel('y-axis'); title('multiple plots using plot command'); grid on; Two-D Plots: Style option conti… Colors, line style and marker style of style option Two-D Plots: legend command ‘legend’ command: This is another optional parameters that can be given to plot command for better illustration and outlook of plots. Syntax are legend(string1, string2, string3,……) legend(linestyle, string1, linestyle, string2……..) legend(srting1, string2, string3,……,pos) Where pos = position 0 = Automatic ‘best’ placement (least conflict with data) 1 = Upper right hand corner (default) 2 = Upper left hand corner 3 = Lower left hand corner 4 = Lower right hand corner 5 = To the right of the plot Two-D Plots: legend command conti… Ex: Following table gives the values of induced voltage E for different filed current If for a DC generator operating at N = 1500 RPM, plot the open circuit characteristics for this generator at 1500 RPM, 1200 RPM & 1000 RPM on the same sheet. Use the proper ‘hold’ and ‘legend’ commands. (Note E α N) Sol: Make a .m file of following code and run it. If=[0 0.4 1.0 1.45 2.0 3.0 4.0 5.0 6.0]; %field current E = [5 30 70 110 150 215 240 250 260]; %induced emf plot(If, E, 'r-'); %red solid line hold on; E2=(1200/1500)*E; plot(If, E2, 'b:'); %blue doted line E3=(1000/1500)*E; plot(If, E3, 'c-.'); %cyan dash dot line legend('1500 RPM','1200 RPM','1000 RPM',2); hold off; Sub-Plots Sub-plots: A graphic window can be sub divided into many sub-graphs or subplots using subplot command. Syntax is Subplot(I, j, k) % total sub-plots=i*j, i row, j column %select the kth area for sub-plot Ex: Divide the figure window into four sub-windows and plot the following functions 1. Plot v v/s I, where v=4*I & I =1, 2, 3, 4 on the upper left sub window. 2. Plot y v/s x, where y=x2 & x= 1,2,3,4 on the upper right sub-window. 3. For t=0:2*pi in step t=pi/60, plot sin(t) v/s t on the lower left sub-window . 4. For t=0:pi/30:2*pi, plot cost(t) v/s t on the lower right sub-window. Sub-Plots conti… I=[1 2 3 4]; V=4*I; subplot(2,2,1); plot(I, V); xlabel('Current I');ylabel('Voltage V'); title('V-I characteristic'); x=[1 2 3 4]; y=x.^2; subplot(2,2,2); plot(x, y); xlabel('x');ylabel('y'); title('y=x^2'); t=0:pi/60:2*pi; subplot(2,2,3); plot(t,sin(t)); xlabel('Angle');ylabel('sin(angle)');title('ploat of sin function'); t=0:pi/30:2*pi; subplot(2,2,4); plot(t, cos(t)); xlabel('Angle');ylabel('cos(angle)');title('plot of cos function'); Special 2-D Plots Specialized 2-D plots: There are many specialized graphics functions in the MATLAB for 2-D plotting. They are following 1. Logarithmic plot function I. semilogx II. semilongy III. loglog 2. polar function 3. area function 4. bar function 5. barh funtion 6. hist function 7. rose function 8. pie function 9. stairs function 10. stem function 11. compass function Special 2-D Plots conti… 1. Logarithmic plot function I. semilogx: The x-axis is drawn on logarithmic scale and y-axis on liner scale Ex: plot function x=e-a, y=a2 where 0<=a<=10 using semilogx function. a=0:1:10; y=a.^2; x=exp(-a); semilogx(x,y); grid on; xlabel('x=exp(-a)');ylabel('y=a^2'); title('showing the use of semilogx plot'); Special 2-D Plots conti… 1. Logarithmic plot function II. semilogy: The y-axis is drawn on logarithmic scale and x-axis on liner scale Ex: Plot power v/s time for 0<t<8 sec, with power on the log scale and time in the linear scale for a motor whose performance equations are given as follows Rotor speed, w=190(1-e-0.15t) Torque, T=8e-0.15t Power, P=w*T time=0:0.2:8 w=190*(1-exp(-0.15*time)); T=8*exp(-0.15*time); P=w.*T; semilogy(time,P); grid on; xlabel('time');ylabel('torque'); title('showing the use of semilogy plot'); Special 2-D Plots conti… 1. Logarithmic plot function III. loglog: Both x & y-axis are drawn on logarithmic scale. Ex: Plot magnitude v/s frequency on log-log scale for the transfer function G(s) = 1/(1+0.02s). Where s=jw=j*2pi*f freq=1:2:1000; Gs=1./(1+0.02*j*2*pi*freq); mag=abs(Gs); loglog(freq,mag); grid on; xlabel('frequency on log scale');ylabel('magnitude on log scale'); title('showing the use of loglog plot'); Special 2-D Plots conti… 2. Polar plot: To draw plot in polar coordinates Syntax is polar(theta, r) Or polar(theta, r, p) where theta = angle vector r = radius or corresponding distance vector p = optional characteristic string describing color, line style Ex: Plot the following function f(θ)=sin (θ) for - π/2 ≤ θ≤π/2 theta=-pi/2:pi/100:pi/2; f=sin(4.*theta); polar(theta,f,'r-'); title('plot of function f=sin(4*theta) using polar function'); Special 2-D Plots conti… 3. area plot: similar to plot function except that the area under plot is filled with color. Syntax is area(x,y) Ex: Plot the following function f(θ)=sin (θ) for - π/2 ≤ θ≤π/2 theta=0:pi/100:4*pi; f=cos(theta); area(theta,f); title('plot of function f=cos(theta) using area function'); Special 2-D Plots conti… 4. bar plot: It draws vertical bar plot. Syntax are bar(y) %draws vertical bar plot using default values of x=1:k, where k is no of data values in vector y, the values of y determines the height of bar. or bar(x, y) %if y is a matrix of mxn, then it draws m grourps of n vertical bars or bar(x, y, ‘style options’) or bar(x, y, width) %width specifies width of bars, if width>1 → overlapped bar, default width = 0.8 Ex: x=[0 1 2 3 4 5 6]; y=[10 15 25 20 30 27 19]; bar(x, y, 'c') %c means cyan color Special 2-D Plots conti… Ex: Plot a bar graph to show the comparison of average temp in city A, B & C for the months from September to February. The data is given as Months City A City B City C September 31 28 24 October 29 26 22 November 28 25 20 December 27 24 16 January 26 22 17 February 29 25 20 temp=[31 28 24;29 26 22; 28 25 20; 27 24 16; 26 22 17; 29 25 20]; bar(temp); legend('City A', 'City B', 'City C'); xlabel('Months from sep to feb'); ylabel('Temp in celsius'); Special 2-D Plots conti… 5. hist plot: It draws histogram of the given data. Syntax are hist(x) %draws 10-bin(default) histogram of vector x or bar(x, n) % draws n-bin histogram of vector x or bar(x, p) %draws histogram of vector x using the bins specified in vector p Ex: x=randn(100,1)*pi; %generate 100 random data points hist(x); title('Histogram to show 100 random values in 10-bins'); Ex x=randn(100,1)*pi; %generate 100 random data points hist(x, 5); %histogram with 5 bins title('Histogram to show 200 random values in 10-bins'); Note: bins means no of vertical lines. Special 2-D Plots conti… 6. ‘rose’ plot: It draws angle histogram of the given data. Syntax are rose(a) %draws 20-bin(default) angle histogram of angle given in vector a or rose(a, n) % draws n-bin angle histogram or rose(a, v) %draws angle histogram of angle given in vector a using the bins specified in vector v Ex: x=randn(100,1)*pi; %generate 100 random data points rose(x); title('Histogram to show 100 random values in 20-bins'); Ex x=randn(100,1)*pi; %generate 100 random data points rose(x, 5); %histogram with 5 bins title('Histogram to show 200 random values in 10-bins'); Special 2-D Plots conti… 7. ‘pie’ plot: It draws pie plot of the given data. Syntax are pie(x) %x is the vector whose pie plot is to be drawn or pie(x, k) % k optional vector which specifies slices that should be pulled out from the pie or pie(x, k, labels) %labels is also an optional vector which is use to label each pie slice with cell array labels Special 2-D Plots conti… Ex: Following table show the concentration of different industries in the region, show it with pie chart. Name of industry No of industrial Units Cement 4 Textile 8 Software 20 Chemical 2 Telecom 7 Banking 10 industry=[4 8 20 2 7 10]; pie(industry); title('Pie char showing percentage of different industries'); Special 2-D Plots conti… Repeat above example with sliced pie chart industry=[4 8 20 2 7 10]; show=[0 0 0 1 0 1]; %show slice of 4th & 6th industiry pie(industry, show); title('Pie char showing percentage of different industries'); legend('Cement','Textile','Software','Chemical','Telecom','Banking',3); Try this also industry=[4 8 20 2 7 10]; company=[{'Cement','Textile','Software','Chemical','Telecom','Banking‘}]; show=[0 0 0 1 0 1]; %show slice of 4th & 6th industiry pie(industry, show, industry); title('Pie char showing different industries'); Special 2-D Plots conti… 8. ‘stairs’ plot: It draws staircase graph of the given data. Syntax are stairs(y) %y is the vector whose staircase plot is to be drawn or stairs (x, y) % y optional vector which specifies location or stairs(x, y, style) %style is also an optional which is use for line style Special 2-D Plots conti… Ex: Draw the stairs plot to show the function y = x.*x, where -2≤x≤2 Sol: make a .m file and run it x=-2:0.1:2; y=x.*x; stairs(x,y,'r'); grid on xlabel('x-axis'); ylabel('y=x.*x'); title('showing the stair plot'); Ex: Draw the stairs plot for the following data X=[0 2 3 4 5 7] & y= [5 -1 8 4 7 3] Sol: X=[0 2 3 4 5 7]; Y= [5 -1 8 4 7 3]; stairs(X,Y,'*-.'); box off; xlabel('x-axis'); ylabel('y-axis'); title('showing the stair plot'); Special 2-D Plots conti… 9. ‘stem’ plot: It draws staircase graph of the given data. Syntax are stem(y) %y is the vector whose stem plot is to be drawn or stem (x, y) % x optional vector which specifies location or stem(x, y, style) %style is also an optional which is use for line style Special 2-D Plots conti… Ex: Draw the stem plot to show the function y = x.*x, where -2≤x≤2 Sol: make a .m file and run it x=-2:0.1:2; y=x.*x; stem(x,y,'r'); grid on xlabel('x-axis'); ylabel('y=x.*x'); title('showing the stem plot'); Ex: Draw the stem plot for the following data X=[0 2 3 4 5 7] & Y= [5 -1 8 4 7 3] Sol: X=[0 2 3 4 5 7]; Y= [5 -1 8 4 7 3]; stem(X,Y,‘*'); box off; xlabel('x-axis'); ylabel('y-axis'); title('showing the stem plot'); Special 2-D Plots conti… 10. ‘compass’ plot: complex data can be plotted by using compass function. It displays angle and magnitude of the complex number on a circular grid. Syntax are compass(y) %draws angle & magnitude of the complex data of vector y %It is equivalent to compass( real(y), imag(y)) %It draws n arrows from origin, n= now of elements of y Ex: Z=[1+1i 4-8i 20+7i 6+8i 9+2i 12-4i]; compass(Z); xlabel('x-axis'); ylabel('y-axis'); title('showing the compass plot'); 3-D Plots For visualization of data in three dimensions, MATLAB has a number of functions. These are extension of 2-D functions i.e. 1. plot3 2. bar3 3. bar3h 4. pie3 etc Note: All style option of 2-D plots are also applicable to 3-D plots 3-D Plots conti… 1. ‘plot3’ function: Syntax is plot3(x, y, t, ‘style_option’) Ex: Given is x=t2 & y = 4t for -4≤t≤4. Obtain a 3-D plot showing the matrix in (x, y) space as a factors of time . Sol: t=-4:0.1:4; x=t.^2; y=4*t; plot3(x,y,t); grid on; xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis-time'); title('Showing 3-D plot'); 3-D Plots conti… 2. ‘bar3’ & bar3h function: bar3 : for horizontal 3-D bar plot bar3h: for vertical 3-D bar plot Ex: Draw 3-D bar plots of the following data. x=[0 1 2 3 4 5 6]; y=[5 17 25 21 31 27 19]; Sol: x=[0 1 2 3 4 5 6]; y=[5 17 25 21 31 27 19]; bar3(x, y, 'c'); % try this with bar3h(x, y, ’c’) 3-D Plots conti… 2. ‘pie3’ function: Similar as pie in 2-D Syntax are pie3(x) or pie3(x, k) or pie3(x, k, labels) Ex: Repeat the example of pie plot with pie3 industry=[4 8 20 2 7 10]; company=[{'Cement','Textile','Software','Chemical','Telecom','Banking'}]; show=[0 0 0 1 0 1]; %show slice of 4th & 6th industiry pie3(industry, show, company); title('Pie char showing different industries'); 3-D Plots conti… 3. ‘stem3’ function: Similar as stem in 2-D Syntax are stem3(x) or stem3(x, y, z) or stem3(x, y, ‘filled’) Ex: Repeat the example of stem plot with stem3 x=-2:0.1:2; y=x.*x; stem3(x,y,'r'); grid on xlabel('x-axis'); ylabel('y=x.*x'); title('showing the stem3 plot'); And X=[0 2 3 4 5 7]; Y= [5 -1 8 4 7 3]; stem(X,Y,‘*'); box off; xlabel('x-axis'); ylabel('y-axis'); title('showing the stem plot'); 3-D Plots conti… 3. ‘stem3’ function: Similar as stem in 2-D Syntax are stem3(x) or stem3(x, y, z) or stem3(x, y, ‘filled’) Ex: Repeat the example of stem plot with stem3 x=-2:0.1:2; y=x.*x; stem3(x,y,'r'); grid on xlabel('x-axis'); ylabel('y=x.*x'); title('showing the stem3 plot'); And X=[0 2 3 4 5 7]; Y= [5 -1 8 4 7 3]; stem(X,Y,‘*'); box off; xlabel('x-axis'); ylabel('y-axis'); title('showing the stem plot'); 3-D Plots conti… 4. ‘meshgrid’ function: Meshgrid function transforms the domain specified by vector x and y into matrix x and y that can be used for evaluation of the function of two variables in 3-D surface plot. Syntax is [x, y]=meshgrid(x_begin : x_increment : x_end, y_begin : y_increment : y_end) After evaluating matrix (arrays) of x & y values, the given function is evaluated and plotted suing mesh, surf or contour plots. Ex: Create meshgrid for data -1≤x≤1 and -2≤y≤2 Sol: [x,y]=meshgrid(-1:0.5:1, -2:1:2) 3-D Plots conti… 5. ‘mesh’ function: It is wireframe plot Syntax is mesh(x, y, z); Or mesh(s, y, z, linestyle); Ex: Create mesh plot for the function z=2/(x2+y2+1) over the interval -5≤x≤5 and -5≤y≤5 Sol: [x,y]=meshgrid(-5:0.1:5, -5:0.1:5); z=2./(x.^2+y.^2+1); mesh(x,y,z); grid on xlabel('x-axis');ylabel('y-axis');zlabel('z-axis') 6. ‘surf’ function: it is similar to mesh function with the difference that instead of wireframe plot it is a surface plot. ( Try above example) 3-D Plots conti… 7. ‘contour’ & ‘contour3’ function: They are used to draw contour in 2-D & 3D respectively. Syntax are contour(z); %gives the contour plot of the specified matrix treating the values given in the matrix as the heights above the plane. Or contour(x, y, z) %similar to countour(z) with the difference that x & y specifies (x,y) coordinates of the surface as for surf function. Ex: Create contour & contour 3-D plots for the function z=sqrt(y2+x2) over the interval -3≤x≤3and -3≤y≤3 Sol: [x,y]=meshgrid(-5:0.1:5, -5:0.1:5); z=sqrt(y.^2-x.^2+1); contour3(x,y,z); grid on xlabel('x-axis');ylabel('y-axis');zlabel('z-axis')