Documente Academic
Documente Profesional
Documente Cultură
Plot function
Plot(a,b) -Plot vectors- plot two vectors a and b To plot x^3 for the interval[-2,2] X=-2:.5:2 Y=x.^3 Axis off or axis equal Title(frapgh of y=x^3 is) Xlabel(text) Ylabel(text) TEXT(X,Y,'string') gtext({'Firstline','Secondline'},'FontName','Times', 'Fontsize',12)
Plot miscellaneous
Figure Hold Cla=Clear current axis. Clf=Clear current figure. RESET(GCA) resets the properties of the current axis. Gca, Get handle to current axis Gcf -Get handle to current figure
To plot a graph of a function over a rectangular domain we need to create a grid of points
Specialized plotting
Text Annotations
plot(0:pi/20:2*pi,sin(0:pi/20:2*pi)) text(pi,0,' \leftarrow sin(\pi)','FontSize',18) annotate the point at (pi,0) with the string sin()
Numeric Variables
You can specify numeric variables in text strings using the num2str (number to string) function. x = 21; ['Today is the ',num2str(x),'st day.'] text(xcoord,ycoord,['Today is the ',num2str(x),'st day.'])
Multiline Text
str1(1) = {'Center each line in the Uicontrol'}; str1(2) = {'Also check out the textwrap function'}; text(5.75,sin(2.5),str1,'HorizontalAlignment' ,'right')
Bar Graphs
A bar chart displays the values in a vector or matrix as horizontal or vertical bars. Y = [5 2 1 873 986 555 4 3 2]; bar(Y)
Pie Chart
Pie charts display the percentage that each element in a vector or matrix contributes to the sum of all elements. Eg X contains yearly sales figures for a specific product over a five-year period,(every 4 months) X = [19.3 22.1 51.6; 34.2 70.3 82.4; 61.4 82.9 90.8; 50.5 54.9 59.1; 29.4 36.3 47.0];
x = sum(X); First, create a vector containing zeros. explode = zeros(size(x)); Then find the slice that contributes the most and set the corresponding explode element to 1. [c,offset] = max(x); explode(offset) = 1; h = pie(x,explode); colormap summer
Stem plots
A stem plot displays data as lines (stems) terminated with a marker symbol at each data value. In a 2-D graph, stems extend from the xaxis. The stem function displays two-dimensional discrete sequence data. Eg alpha = .02; beta = .5; t = 0:4:200; y = exp(-alpha*t).*sin(beta*t); plot(t,y) stem(t,y)
Stairstep plots display data as the leading edges of a constant interval (i.e., zero-order hold state). This type of plot holds the data at a constant yvalue for all values between x(i) and x(i+1), where i is the index into the x data. This type of plot is useful for drawing time-history plots of digitally sampled data systems. alpha = 0.01;beta = 0.5;t = 0:10; f = exp(-alpha*t).*sin(beta*t); stairs(t,f) hold on plot(t,f,'--*') hold off
Stairstep Plots
Feather plot
Create a feather plot showing the direction of theta. theta = (-90:10:90)*pi/180; r = 2*ones(size(theta)); [u,v] = pol2cart(theta,r); feather(u,v);
Quiver
A quiver plot displays velocity vectors as arrows with components (U,V) at the points (X,Y). Expanding X and Y CoordinatesMATLAB expandes X and Y, if they are not matrices. This expansion is equivalent to calling meshgrid to generate matrices from vectors: [x,y] = meshgrid(-2:.2:2,-1:.15:1); z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15); contour(x,y,z), hold on quiver(x,y,px,py), hold off, axis image
A contour plot displays isolines of matrix Z. Label the contour lines using clabel.The clabel function adds height labels to a twodimensional contour plot. Command contour(u) [x,y]=meshgrid(-2.1:0.15:2.1,-6:0.15:6) u=80*y.^2.*exp(-x.^2-0.3*y.^2); contour(u)
3D plots
t = 0:pi/50:10*pi; plot3(sin(t),cos(t),t) axis square; grid on
3D Contour plot
It can be drawn under a surface with command meshc or surfc [x,y]=meshgrid(-2:.2:2); z=x.^exp(-x.^2-y.^2); meshc(z) figure surfc(z)
Visualization of matrices
Mesh function can be used to visualize matrices a=zeroes(30,30); a(:,15)=0.2*ones(30,1); a(7,:)=0.1*ones(1,30); a(15,15)=1; mesh(a)
Rotation of 3D graphs
for az=-37.5:15:-37.5+360 mesh(a),view(az,el) %default el=30 pause(0.5) end
Ribbon plot
[x,y]=meshgrid(-8:1:8); r=sqrt(x.^2+y.^2) + eps; z=sin(r)./r; ribbon(z)
Waterfall
[x,y]=meshgrid(-2:0.1:2); z=x.*exp(-x.^2-y.^2) waterfall(z)