Top Banner
CSE 123 Plots in MATLAB
22

CSE 123 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.

Dec 14, 2015

Download

Documents

Jaime Fishwick
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 123 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.

CSE 123

Plots in MATLAB

Page 2: CSE 123 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.

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.

Page 3: CSE 123 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.

Example:

>>ezplot('x^3-2*x')

-6 -4 -2 0 2 4 6-250

-200

-150

-100

-50

0

50

100

150

200

250

x

x3-2 x

>> ezplot('x^3-x',[-5 5])

-5 -4 -3 -2 -1 0 1 2 3 4 5-150

-100

-50

0

50

100

150

Page 4: CSE 123 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.

>> ezplot('x^2-y^4‘[-5,5,-4,4])

-5 -4 -3 -2 -1 0 1 2 3 4 5-4

-3

-2

-1

0

1

2

3

4

x

y

x2-y4 = 0

Page 5: CSE 123 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.

plot command: Linear 2-D plot

 Syntax: plot(X,Y,linespec, ‘PropertyName',PropertyValue, …)

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.

Page 6: CSE 123 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.

Examples:

x=1:50;y=sin(x*pi/10)+cos(x*pi/10);plot(x,y)

plot(x,y,'--ro')

Examples:

0 5 10 15 20 25 30 35 40 45 50-1.5

-1

-0.5

0

0.5

1

1.5

0 5 10 15 20 25 30 35 40 45 50-1.5

-1

-0.5

0

0.5

1

1.5

0 5 10 15 20 25 30 35 40 45 50-1.5

-1

-0.5

0

0.5

1

1.5

0 5 10 15 20 25 30 35 40 45 50-1.5

-1

-0.5

0

0.5

1

1.5

Page 7: CSE 123 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.

Example

plot(x,y,'-kd', 'MarkerEdgeColor',‘g', 'MarkerFaceColor', ‘r' )

plot(x,y,'-k', 'Linewidth', 5 )

Example

0 5 10 15 20 25 30 35 40 45 50-1.5

-1

-0.5

0

0.5

1

1.5

0 5 10 15 20 25 30 35 40 45 50-1.5

-1

-0.5

0

0.5

1

1.5

Page 8: CSE 123 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.

Plotting multiple plots on the same figure

x=1:50;y1=cos(x*pi/10); y2=sin(x*pi/10);plot(x,y1,x,y2)

0 5 10 15 20 25 30 35 40 45 50-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

x=1:50;y1=cos(x*pi/10); y2=sin(x*pi/10);plot(x,y1)hold onplot(x,y2)

0 5 10 15 20 25 30 35 40 45 50-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 9: CSE 123 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.

Plotting multiple plots on the same figure

plot(x,y1,’k’,x,y2,’r’)

x=1:50;y1=cos(x*pi/10); y2=sin(x*pi/10);

plot(x,y1,’k’)hold onplot(x,y2,’r’ )

Page 10: CSE 123 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.

Editing plots with editplot

Syntax plotedit on plotedit off

Page 11: CSE 123 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.

2 plots: one above the other = 2-by-1 matrix

4 plots: in a 2x2 array = 2-by-2 matrix

subplot command

Syntax: subplot(N,M,i)

Notes: *This function does NOT create a plot *It HAS to be used BEFORE the plot function !!!!

N= number of rowsM= number of columnsi= current plot number

Subplot(2,1,1)

Subplot(2,1,2)

(2,2,1) (2,2,2)

(2,2,3) (2,2,4)

Page 12: CSE 123 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.

Plotting multiple plots on the same figure

x=1:50;y1=cos(x*pi/10);y2=1000*y1;

plot(x,y1,x,y2,’k’); grid on

subplot(2,1,1)plot(x,y1); grid onsubplot(2,1,2)plot(x,y2,’k’); grid on

Page 13: CSE 123 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.

Plot features: xlabel, ylabel, title, grid

x=1:50;y1=cos(x*pi/10); y2=sin(x*pi/10);

plot(x,y1,x,y2)xlabel(‘x’); ylabel(‘y’);title(‘Example’)grid on

text(15,0.8,’y1’)text(20,-0.2,’y2’)

Plot features: text and legend

legend(‘y1’,’y2’)

Page 14: CSE 123 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.

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.

Page 15: CSE 123 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.

Example:

>>x=0:0.05:5;y=sin(x.^2);z=cos(x.^2);plotyy(x,y,x,z)

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.5

0

0.5

1

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.5

0

0.5

1

Page 16: CSE 123 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.

%% Line Plot of a Chirpx=0:0.05:5;y=sin(x.^2);plot(x,y);

>> %% Bar Plot of a Bell Shaped Curvex = -2.9:0.2:2.9;bar(x,exp(-x.*x));

>> %% Bar Plot of a Bell Shaped Curvex = -2.9:0.2:2.9;bar(x,exp(-x.*x));

>> %% Stairstep Plot of a Sine Wavex=0:0.25:10;stairs(x,sin(x));

>> %% Errorbar Plotx=-2:0.1:2;y=erf(x);e = rand(size(x))/10;errorbar(x,y,e);

>> % Polar Plott=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)

Page 17: CSE 123 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.

Logarithmic Plots

commands

semilogx() plots x data on log axis and y on linear axis

semilogy() plots y data on log axis and x on linear axis

loglog() plots x and y on logarithmic axes

Page 18: CSE 123 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.

Logarithmic Plots

Example: plot y=e2x

>> x=0:0.1:10;>> y=exp(2.*x);>>plot(x,y)>> semilogy(x,y)

0 1 2 3 4 5 6 7 8 9 100

0.5

1

1.5

2

2.5x 10

4

0 1 2 3 4 5 6 7 8 9 1010

0

101

102

103

104

105

Page 19: CSE 123 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.

Three dimensional plots

Three dimensional line plots the simplest form is

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)

y(t)= e-0.2tsin(2t) motion of an insect

t=0:0.1:10;x=exp(-0.2*t).*cos(2*t);y=exp(-0.2*t).*sin(2*t);plot(x,y)title('\bf 2D plot')xlabel('\bfx')ylabel('\bfy')grid on

Page 20: CSE 123 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.

Three dimensional plots

instead we could plot the variables with plot3() to preserve the time information as well as the 2D position of the object.

plot3(x,y,t)

t=0:0.1:10;x=exp(-0.2*t).*cos(2*t);y=exp(-0.2*t).*sin(2*t);plot3(x,y,t)title('\bf 3D plot')xlabel('\bfx')ylabel('\bfy')zlabel('\bftime')grid on

Page 21: CSE 123 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.

Three dimensional plots

surface, Mesh and contour plots: such kind of plots represent data that is a function of 2D variables. a user must create three arrays of equal size;

Commands:mesh(x,y,z) creates a mesh or wireframe plot where x,y and z are 2D

arrays of containing x,y,z values at every point, respectively.surf(x,y,z) creates a surface plot,contour(x,y,z) 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.

Page 22: CSE 123 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.

Three dimensional plots

Example:create the mesh plot of the following function

])(5.0[5.0 22

),( yxxeyxz

over the interval 4444 yandx

[x,y]=meshgrid(-4:0.1:4);

z= exp(-0.5*(x.^2+0.5*(x-y).^2));

mesh(x,y,z);