CSE123 Lecture8 2012 PDF
CSE123 Lecture8 2012 PDF
Plots in MATLAB
Easiest way to plot Syntax: ezplot(fun) ezplot(fun,[min,max]) ezplot(fun2) ezplot(fun2,[xmin,xmax,ymin,ymax]) ezplot(fun) plots the expression fun(x) over the default domain -2pi < x < 2pi. ezplot(fun,[min,max]) plots fun(x) over the domain: min < x < max. ezplot(fun2) plots fun2(x,y)= 0 over the default domain -2pi < x <2pi, -2pi < y < 2pi.
Example:
>>ezplot('x^3-2*x')
150
100
150 100
50
50 0 -50 -100 -150 -200 -250 -6 -4 -2 0 x 2 4 6
-50
-100
-150 -5
-4
-3
-2
-1
>> ezplot('x^2-y^4[-5,5,-4,4])
x 2-y 4 = 0 4 3 2 1 0 -1 -2 -3 -4 -5
-4
-3
-2
-1
0 x
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.
Examples:
x=1:50; y=sin(x*pi/10)+cos(x*pi/10); plot(x,y)
1.5 1
0.5
-0.5
-1
-1.5
10
15
20
25
30
35
40
45
50
Examples:
plot(x,y,'--ro')
1.5 1
0.5
-0.5
-1
-1.5
10
15
20
25
30
35
40
45
50
Example
1.5
plot(x,y,'-k', 'Linewidth', 5 )
0.5
-0.5
-1
-1.5
10
15
20
25
30
35
40
45
50
Example
plot(x,y,'-kd', 'MarkerEdgeColor',g', 'MarkerFaceColor', r' )
1.5
0.5
-0.5
-1
-1.5
10
15
20
25
30
35
40
45
50
plot(x,y1,k,x,y2,r)
subplot(N,M,i)
*This function does NOT create a plot *It HAS to be used BEFORE the plot function !!!! 4 plots: in a 2x2 array = 2-by-2 matrix
Subplot(2,1,1)
(2,2,1)
(2,2,2)
Subplot(2,1,2)
(2,2,3)
(2,2,4)
plotyy 2-D line plots with y-axes on both left and right side Syntax plotyy(X1,Y1,X2,Y2)
plotyy(X1,Y1,X2,Y2) plots X1 versus Y1 with y-axis labeling on the left and plots X2 versus Y2 with y-axis labeling on the right.
0.5
0.5
-0.5
-0.5
-1
0.5
1.5
2.5
3.5
4.5
-1 5
%% Line Plot of a Chirp x=0:0.05:5; y=sin(x.^2); plot(x,y); >> %% Bar Plot of a Bell Shaped Curve x = -2.9:0.2:2.9; bar(x,exp(-x.*x)); >> %% Bar Plot of a Bell Shaped Curve x = -2.9:0.2:2.9; bar(x,exp(-x.*x)); >> %% Stairstep Plot of a Sine Wave x=0:0.25:10; stairs(x,sin(x));
>> %% Errorbar Plot x=-2:0.1:2; y=erf(x); e = rand(size(x))/10; errorbar(x,y,e); >> % Polar Plot t=0:.01:2*pi; polar(t,abs(sin(2*t).*cos(2*t))); >> x = 0:0.1:4; y = sin(x.^2).*exp(-x); stem(x,y)
Logarithmic Plots
commands semilogx() semilogy() plots x data on log axis and y on linear axis plots y data on log axis and x on linear axis
loglog()
Logarithmic Plots
Example: plot y=e2x >> x=0:0.1:10; >> y=exp(2.*x); >>plot(x,y) >> semilogy(x,y)
2.5 x 10
4
10
10
1.5
10
10
0.5
10
10
10
10
plot(x,y,z) x,y,z are arrays of equal size containing the locations of data points to plot.
Example: x(t)= e-0.2tcos (2t)
creates a mesh or wireframe plot where x,y and z are 2D arrays of containing x,y,z values at every point, respectively. creates a surface plot, creates a contour plot,
[x,y]=meshgrid(xstart:xinc:xend, ystart:yinc:yend) easily create x and y arrays required for 3D plots to create a 3D plot use meshgrid to create the arrays of x and y values then evaluate the function to plot at each of x,y pairs. finally call the above mentioned functions to create plot.
z ( x, y) e
over the interval [x,y]=meshgrid(-4:0.1:4);
0.5[ x 2 0.5( x y ) 2 ]
4 x 4 and 4 y 4
z= exp(-0.5*(x.^2+0.5*(x-y).^2)); mesh(x,y,z);