820 likes | 1.06k Views
DREAM. IDEA. PLAN. IMPLEMENTATION. Mat rix Lab oratory. Introduction to Matlab By Dr. Kourosh Kiani. Course Structure. Spread over 8 weeks 1 classes per week 2 hour per class. 2D graph. Plotting.
E N D
DREAM IDEA PLAN IMPLEMENTATION
MatrixLaboratory Introduction to MatlabByDr. Kourosh Kiani
Course Structure • Spread over 8 weeks • 1 classes per week • 2 hour per class
Plotting The plot function has different forms, depending on the input arguments. If yis a vector, plot(y) produces a piecewise linear graph of the elements of y versus the index of the elements of y >> y = 0:1:100; >> plot(y)
Plotting >> y = 0:1:100; >> plot(y); >> grid; • GRID ON creates a grid on the current figure • GRID OFF turns off the grid from the current figure • GRID toggles the grid state
Plotting If you specify two vectors as arguments, plot(x,y) produces a graph of y versus x. >> x = 0:pi/100:2*pi; >> y = sin(x); >> plot(x,y); >> grid;
Plotting >> x = 0:pi/100:2*pi; >> y = sin(x); >> plot(x,y); >> grid; >> title('Plot of the Sine Function','FontSize',12) >> xlabel('x = 0:2\pi'); >> ylabel('Sine of x');
Plotting You can plot multiple graphs in one call to plot using x-y pairs. MATLAB automatically cycles through a predefined list of colors (determined by the axes ColorOrder property) to allow discrimination between sets of data. Plotting three curves as a function of t produces >> t = 0:pi/100:2*pi; >>y1 = sin(t); >>y2 = sin(t-0.25); >>y3 = sin(t-0.5); >>plot(t,y1,t,y2,t,y3); >> grid on;
HOLD ON holds the current plot HOLD OFF releases hold on current plot HOLD toggles the hold state Adding additional plots to a figure • » x = 0:.1:2*pi; • y = sin(x); • » plot(x,y,'b') • » grid on • » hold on • » plot(x,exp(-x),'r:*')
Color, Symbols, and Line Types 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 Colors Symbols Line Types
Color, Symbols, and Line Types >> x=0:pi/100:2*pi; >> y=sin(x); >>plot(x,y, ‘g’); >> x=0:pi/100:2*pi; >> y=sin(x); >>plot(x,y, ‘r’);
Color, Symbols, and Line Types >> x=0:pi/100:2*pi; >> y=sin(x); >>plot(x,y, ‘:g’); >> x=0:pi/100:2*pi; >> y=sin(x); >>plot(x,y, ‘--r’);
Color, Symbols, and Line Types x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,… 'MarkerEdgeColor','k',‘… MarkerFaceColor','g',… 'MarkerSize',10)
Color, Symbols, and Line Types >> x = 0:pi/15:4*pi; >>y = exp(2*cos(x)); >>plot(x,y,'-r',x,y,'og')
Plotting: Multiple Graphs >> x = linspace(0, 4*pi); >>y1 = sin(x); >>y2 = sin(x) .^ 2; >>y3 = y1 + y2; >>plot(x,y1,x,y2,x,y3); >>legend( 'sin(x)', 'sin(x)^2', 'sin(x) +sin(x)^2' );
Plotting: Multiple Graphs >>x = linspace ( 0, 10*pi, 100 ); >>y = exp ( -0.1 * x ) .* sin ( x ); >>plot ( x, y, ' r' ); >>hold on; >>plot ( x, exp ( -0.1 * x ), ' g '); >>plot ( x, -exp ( -0.1 * x ) ); >>hold off;
Color, Symbols, and Line Types >>x= -pi:0.1:pi; >>y =sin(x); >>set(gca,’Xtick’, -pi:pi/2:pi) >>set(gca,’XTickLabel’,{‘pi’,’-pi/2’,’0’,’pi/2’,’pi’}
Plotting Examples >>x=-2:0.01:4; >>y=3.5^(-0.5*x).*cos(6*x); >>plot(x,y); >>line([0,0],[-3 3],’color’,’r’);
Plotting Examples >>x=-2:0.01:4; >>y=3.5^(-0.5*x).*cos(6*x); >>plot(x,y); >>line([1,2],[-3 3],’color’,’r’);
Plotting Examples >>x=-2:0.01:4; >>y=3.5^(-0.5*x).*cos(6*x); >>plot(x,y); >>line([-2,4],[1 1],’color’,’r’);
Subplots SUBPLOT- display multiple axes in the same figure window subplot(#rows, #cols, index) • >>x = 0:pi/100:2*pi; • subplot(2,2,1); • plot(x,sin(x)); • subplot(2,2,2); • plot(x,sin(x-0.25)); • subplot(2,2,3); • plot(x,sin(x-0.5); • subplot(2,2,4); • plot(x, cos(x));
Setting Aspect Ratio By default, MATLAB displays graphs in a rectangular axes that has the same aspect ratio as the figure window. >> t=0:pi/20:2pi; >> plot(sin(t), 2*cos(t)); >> grid on; >> axis square
Setting Aspect Ratio >> axis equal This produces an axes that is rectangular in shape, but has equal scaling along each axis.
Setting Aspect Ratio If you want the axes shape to conform to the plotted data, use the tight option in conjunction with equal: >> axis equal right
Loglog plot >> x = logspace(-1,2); >> loglog(x,exp(x), ‘-s’); >> grid on;
Loglog plot >> x = logspace(-1, 4); >> loglog(x,exp(x), ‘-s’); >> grid on;
semilogy >> x = 0:0.1:10; >> semilogy(x, 10.^x); >> grid on;
Two-Dimensional Stem Plots >>alpha = 0.02; >>beta = 0.5; >>t=0:4:200; >>y=exp(-alpha*t).*cos(beta*t); >>plot(t,y);
Two-Dimensional Stem Plots >>alpha = 0.02; >>beta = 0.5; >>t=0:4:200; >>y=exp(-alpha*t).*cos(beta*t); >>stem(t,y);
Two-Dimensional Stem Plots >>alpha = 0.02; >>beta = 0.5; >>t=0:4:200; >>y=exp(-alpha*t).*cos(beta*t); >>stem(t,y,’—sr’,’fill’);
Two-Dimensional Stem Plots >>y = 1988:1994;>>s = [ 8 12 20 22 18 24 27 ];>>stem(y,s);
Stairs plot >>x=0:pi/25:2*pi; >>y=sin(x); >>plot(x,y); >>hold on; >>stairs(x,y);
Stairs plot >>y = 1988:1994;>>s = [ 8 12 20 22 18 24 27 ];>>stairs(y,s);
plotyy Create graphs with y-axes on both left and right side >> x = 0:0.01:20; >> y1=200*exp(-0.05*x).*sin(x); >> y2=0.8*exp(-0.5*x).*sin(10*x); >> plotyy(x,y1,x,y2,’plot’);
plotyy >> t = 0:900; A = 1000; a = 0.005; b = 0.005; >>z1 = A*exp(-a*t); >>z2 = sin(b*t); >>plotyy(t,z1,t,z2,'semilogy','plot'); >> grid on;
plotyy x = 0:pi/20:2*pi; y = exp(sin(x)); plotyy(x,y,x,y,'plot','stem')
comet t= linspace(0,10*pi,20000); y=t.*cos(t); comet(t,y)
Plot3 Command The plot3 function displays a three-dimensional plot of a set of data points >> t=0:pi/50:10*pi; >>plot3(sin(t),cos(t)); >>grid on; >>axix square
Area graph >>area(Y)
Comparing Data Sets with Area Graphs Area graphs are useful for comparing different datasets. For example, given a vector containing sales figures, >>sales = [51.6 82.4 90.8 59.1 47.0]; for the five-year period >>x = 90:94; and a vector containing profits figures for the same five-year period, >>profits = [19.3 34.2 61.4 50.5 29.4]; >>area(x,sales,'FaceColor' ,[.5 .9 .6]); >>hold on; >>area(x,profits, 'FaceColor' ,[.9 .85 .7]);
Bar plot >> x=-2.9:0.2:2.9; >>bar(x, exp(-x.*x),`r`);
Bar plot >> Y=randn(3,5); >>bar(Y);
Vertical bar plot >>y = 1988:1994; >>s = [ 8 12 20 22 18 24 27 ]; >>bar(y,s,'r'); Bar plot
Vertical bar plot >>y = 1988:1994; >>s = [ 8 12 20 22 18 24 27 ]; >>barh(y,s,‘g'); Bar plot
Bar plot >> Y=round(rand(5,3)*10); >>subplot(2,2,1); >>bar(Y, `group`); >>title `Group`; >>subplot(2,2,2); >>bar(Y, `stack`); >>title ` Stack `; >>subplot(2,2,3); >>barh(Y, `stack`); >>title ` Stack `; >>subplot(2,2,4); >>bar(Y, 1.5); >>title ` Width =1.5 `;
Histogram plot >>x = randn(1,100);>>hist(x,10);
Histogram plot >>x = randn(1,100);>>hist(x,20);