





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























x = 0:pi/100:2pi; y = sin(x); plot(x,y,'LineWidth',3)* Now label the axes and add a title. The characters \pi create the symbol. See text strings for more symbols: xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function','FontSize',12)
For example, these statements use the colon operator to create a vector of x values ranging from 0 to 2, compute the sine of these values, and plot the result:
Result of the example
Resulting graph
You can also specify line characteristics using graphics properties: LineWidth -- Specifies the width (in points) of the line. MarkerEdgeColor -- Specifies the color of the marker or the edge color for filled markers (circle, square, diamond, pentagram, hexagram, and the four triangles). MarkerFaceColor -- Specifies the color of the face of filled markers. MarkerSize -- Specifies the size of the marker in units of points.
t = 0:.4:50; alpha =.055; y = exp(-alphat).sin(.5t) plot(t,y,'--rs','LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)*
Example: Prog3.m
PLOT(X,Y) plots vector Y versus vector X. TITLE('text') adds text at the top of the current plot. XLABEL('text') adds text beside the X-axis on the current axis. YLABEL('text') adds text beside the Y-axis on the current axis. GRID, by itself, toggles the major grid lines of the current axes. GTEXT('string') displays the graph window, puts up a cross-hair, and waits for a mouse button or keyboard key to be pressed. SUBPLOT(m,n,p), or SUBPLOT(mnp),
breaks the Figure window into an m-by-n matrix of small axes. STEM(Y) stemDiscrete sequence or "stem" plot. STEM(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value. SEMILOGX(...) is the same as PLOT(...), except a logarithmic (base 10) scale is used for the X-axis. SEMILOGY(...) is the same as PLOT(...), except a logarithmic (base 10) scale is used for the Y-axis..
Two ways to make multiple plots on a single graph are illustrated here. Another way is with the hold command. The command hold freezes the current graphics screen so that subsequent plots are superimposed on it. Entering hold again releases the "hold". The commands hold on and hold off are also available. One can override the default line types and point types. For example,
% prog3.m t = 0: 0.01: 2pi; y1 = sin(t); y2 = sin(2t); y3 = sin(4t); plot(t, y1, '--', y2, ā', y3, '+')**
Colors Line Styles y yellow. point M magenta o circle C cyan x x-mark R red + plus G green - solid B blue * star W white : dotted K black -. Dashdot -- dashed More mark types are; square(s), diamond(d), up-triangle(v), down- triangle(^), left-triangle(<), right-triangle(>), pentagram(p), hexagram(h) See also help plot for more line and mark color.
% prog4.m % Line plot of a chirp x = 0 : 0.05 : 5; y = sin(x.^2); subplot(2,1,1), plot(x,y); % Bar plot of a bell shaped curve x = -2.9 : 0.2 : 2.9; subplot(2,1,2), bar(x,exp(-x.x));*
ļ± 2 dimensions line plots (x vs. y), ļ± filled plots, ļ± bar charts, ļ± pie charts, ļ± parametric plots, ļ± polar plots, ļ± contour plots, ļ± density plots, ļ± log axis plots, ļ± surface plots, ļ± parametric plots in 3 dimensions ļ± and spherical plots.
To preview some of these capabilities and others as well, enter the command demos.
MATLAB supports many types of graph and surface plots:
The 3-D analog of the plot function is plot3. If x, y, and z are three vectors of the same length, plot3(x,y,z) generates a line in 3-D through the points whose coordinates are the elements of x, y, and z. Then it produces a 2-D projection of that line on the screen. For example, these statements produce a helix.
% prog5.m t = 0:pi/50:10pi; plot3(sin(t),cos(t),t,'LineWidth',3) axis square; grid on*
meshgrid: generate X and Y arrays for 3-D plots mesh: draws a wireframe mesh surf: plot a 3-D shaded surface colorbar: add a color bar tool patch: creating patch graphics objects hidden: remove hidden lines from mesh plot
To illustrate the use of meshgrid, consider the sin(r)/r or sinc function. To evaluate this function between -8 and 8 in both x and y, you need pass only one vector argument to meshgrid, which is then used in both directions. [X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; The matrix R contains the distance from the center of the matrix, which is the origin. Adding eps prevents the divide by zero (in the next step) that produces Inf values in the data. Forming the sinc function and plotting Z with mesh results in the 3-D surface. Z = sin(R)./R; mesh(X,Y,Z)
ļ²
b a
ļ² ļ½ ļ² ļ«ļ²
b m
m a
b a