Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Introduction to MATLABDr./ Ahmed Nagib
Mechanical Engineering department, Alexandria university, Egypt
Sep 2015
Chapter 2
Plotting in MATLAB
Nomenclature for a typical xy plot. Figure 5.1–1, page 220
2-‐1
Y LABEL
X LABEL
Matlab contains many powerful functions for easily creating plots of several types such as logarithmic, surface and contour plots.
Command Plot
Example 2.1
As a simple example, let us plot the function𝑦 = 3 cos 2𝑥 for 0≤ 𝑥 ≤ 7.
We choose to use an increment of 0.01 to generate a large number of x values in order to produce a smooth curve.The function plot(x,y) generates a plot with the x values on the horizontal axis and the y on the vertical access, as seen in the following slide:
2-2
Example 2-1 – cont.A graphics window showing a plot
2-3
To add x and y labels and a title to the graph, we use the following commands:
xlabel(‘x’);ylabel(‘y’);title(‘Example 10’);
2-4
To add a grid to the graph, we use the following commands:
grid
2-5
To increase the font size of the xlabel, ylabel and title, we use the following commands:
xlabel('x','FontSize',18);ylabel('y','FontSize',18);title('Example 10','FontSize',24);
2-6
To increase the plotted line width in Matlab, we use the following commands:
plot(x,y,'linewidth', 2)
2-7
To save the figure in Matlab
1. Use the menu system. Select Save as on the File menu in the Figure window.
2. Type the following command in the m file.
saveas(gcf, 'Example 10.tiff');
This command saves the current plot directly to working directory.
We recommend using use the previous command to save the graphs.
2-8
To save the figure in Matlab, we use the following commands:
saveas(gcf, 'Example 10.tiff');
2-9
Data Markers and Line Types
In the graph, to choose different color or marker type or line type (solid, dashed, …..)
Choose from the following table
If you want to see more options, type the following in the command windows>>help plot
2-‐10
2-‐11
Data Markers and Line Types
2-‐12
Data Markers and Line Types
2-‐13
Data Markers and Line Types
A plotting command with a certain color:
plot(x,y, 'r')
A combination of line type & color :
plot(x,y, ’--r')
A combination of mark & color :
plot(x,y, ’+r')
A combination of line type, mark & color :
plot(x,y, ’--+r’)
If we want to plot two variables in the same graph, we use the following commands:
hold onand legend('Y','Z', 'Exact','location', 'best')
Location in the legend command specify the best location in the graph2-14
If we want to plot two figures, we use the following commands:
2-15
If we want to plot two plots in the same window, we use the following commands:
subplot(1,2,1)andsubplot(1,2,2)
2-16
Plot Summary:
So we will copy and paste the plotting commands shown in the previous example to get a nice looking saved graph.
2-17
2-‐18
Axis command
The maximum and minimum of the coordinates on the graph may be specified by the command:
axis([xmin,xmax,ymin,ymax])
A figure can be reshaped by the command:
axis('square’)
2-‐19
Figure clearing
The command clf clears everything inside the graphic window.
clf
2-‐20
Example
clfx=0:0.01:10;y = cos(x).*exp(-0.4*x);xlabel(’x') ylabel('y')plot(x,y)axis([0 , 5 , -0.5 , 1.5])axis('square')grid on
2-‐21
2-‐22
Text in graph
Text can be written in a graph by the following command:
text
Example
text(x , y , ' string ')
where x,y are the coordinates where the string startsstring is the required string
2-‐23
Example
clear ; clf ;theta = 0:0.2:2*pi ;y = theta.*exp(-theta) ;z = sin(theta).*exp(-theta) ;plot(theta , y , 'r' , theta , z , 'b')xlabel('theta(rad)') ; ylabel('Distance')title('Distance y and zeta versus theta')text(2.4 , 0.24 , 'y' , 'fontname' , 'arial' , ... 'fontsize' , 14 , 'color', 'r')text(2.2 , 0.11 , 'z' , 'fontname' , 'symbol' , ... 'fontsize' , 14 , 'color' , 'b')grid on
2-‐24
Example
Logarithmic Plots
It is important to remember the following points when using log scales:
1. You cannot plot negative numbers on a log scale, because the logarithm of a negative number is not defined as a real number.
2. You cannot plot the number 0 on a log scale, because log10 0 = ln 0 = −∞. You must choose an appropriately small number as the lower limit on the plot.
2-‐25(continued…)
Logarithmic Plots
x1 = 0:0.01:3; y1 = 25*exp(0.5*x1);y2 = 40*(1.7.^x1);x2 = logspace(-1,1,500);y3 = 15*x2.^(0.37);subplot(1,2,1),semilogy(x1,y1,x1,y2, ‘--’)legend (‘y = 25e^0.5x’, ‘y = 40(1.7) ^x’)xlabel(‘x’);ylabel(‘y’);gridsubplot(1,2,2),loglog(x2,y3),legend(‘y = 15x^0.37’),xlabel(‘x’),ylabel(‘y’),grid
2-‐26(continued…)
Logarithmic Plots
2-‐27
Command
bar(x,y)
plotyy(x1,y1,x2,y2)
polar(theta,r,’type’)
stairs(x,y)
stem(x,y)
Description
Creates a bar chart of y versus x.
Produces a plot with two y-axes, y1on the left and y2 on the right.
Produces a polar plot from the polar coordinates theta and r, using the line type, data marker, and colors specified in the string type.
Produces a stairs plot of y versus x.
Produces a stem plot of y versus x.
Specialized plot commands. Table 5.2-3, page 236
2-‐28
Three-Dimensional Line Plots:
The following program uses the plot3 function to generate the spiral curve shown in Figure 5.4–1, page 247.
>>t = 0:pi/50:10*pi;>>plot3(exp(-0.05*t).*sin(t),...
exp(-0.05*t).*cos(t),t),...xlabel(’x’),ylabel(’y’),zlabel(’z’),grid
2-‐29
See the next slide.
The curve x = e−0.05t sin t, y = e−0.05t cos t, z = t plotted with the plot3 function. Figure 5.4–1, page 247.
2-‐30
Surface Plots:
The following session shows how to generate the surface plot of the function z = xe−[(x−y2)2+y2], for −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2, with a spacing of 0.1. This plot appears in Figure 5.4–2, page 248.
>>[X,Y] = meshgrid(-2:0.1:2);>>Z = X.*exp(-((X-Y.^2).^2+Y.^2));>>mesh(X,Y,Z),xlabel(’x’),ylabel(’y’),...
zlabel(’z’)
2-‐31
See the next slide.
A plot of the surface z = xe−[(x−y2)2+y2] created with the meshfunction. Figure 5.8–2
2-‐25
Example
clear ; clf ;
xa = -2:0.2:2;
ya = -2:0.2:2;
[x,y] = meshgrid(xa,ya) ;
z = x.*exp(-x.^2-y.^2) ;
mesh(x,y,z)
title('z=x*exp(-x^2-y^2)')
xlabel('x') ; ylabel('y') ; zlabel('z') ;
2-‐26See the next slide.
A contour plot of the surface z = xe−[(x−y2)2+y2] created with the contour function.
2-‐27
ContourContour of a function in a two-dimensional array can be plotted by:
Contour(x , y , z , level)
z is the 2-D array of the functionx and y are the coordinates in one or 2D arraysLevel is a vector containing the levelsThe contour level is determined by dividing the minimum and maximum values of z into k-1 intervals
The following is a script for the contour plot:clear ; clf ;
xa = -2 : 0.2 : 2 ; ya = -2 : 0.2 : 2 ;
[x,y] = meshgrid(xa,ya) ;
z = x.*exp(-x.^2-y.^2) ; zmax=max(max(z)) ;
zmin=min(min(z)) ; k=21 ;
dz=(zmax-zmin)/(k-1) ; level=zmin : dz : zmax ; h=contour(x , y , z , level) ;
title('z=x*exp(-x^2-y^2)') ;
xlabel('x') ; ylabel('y') ;
The following session generates the contour plot of the function whose surface plot is shown in Figure 5.8–2;; namely, z = xe−[(x−y2)2+y2], for −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2, with a spacing of 0.1. This plot appears in Figure 5.4–3, page 249.
>>[X,Y] = meshgrid(-2:0.1:2);>>Z = X.*exp(-((X- Y.^2).^2+Y.^2));>>contour(X,Y,Z),xlabel(’x’),ylabel(’y’)
2-‐26
See the next slide.
A contour plot of the surface z = xe−[(x−y2)2+y2] created with the contour function.
2-‐27
Contour labels may be automatically annotated by : clabel(h)
h is the name of contour
Contour labels may also be placed manually using the mouse and by :
clabel(h,’manual’)
Below is a script with clabel :
Example
clear ; clf ;
xa = -2:0.2:2; ya = -2:0.2:2;
[x,y] = meshgrid(xa,ya) ;
z = x.*exp(-x.^2-y.^2) ; zmax=0.5 ;
zmin=-0.5 ; k=11 ;
dz=(zmax-zmin)/(k-1) ; level=zmin : dz : zmax ; h=contour(x,y,z,level) ; clabel(h,'manual') ;
title('z=x*exp(-x^2-y^2)')
xlabel('x') ; ylabel('y') 2-‐26
2-‐27
Functioncontour(x,y,z)
mesh(x,y,z)
meshc(x,y,z)
meshz(x,y,z)
surf(x,y,z)
surfc(x,y,z)
[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
waterfall(x,y,z)
DescriptionCreates a contour plot.
Creates a 3D mesh surface plot.
Same as mesh but draws contours under the surface.
Same as mesh but draws vertical reference lines under the surface.
Creates a shaded 3D mesh surface plot.
Same as surf but draws contours under the surface.
Creates the matrices X and Y from the vectors x and y to define a rectangular grid.
Same as [X,Y]= meshgrid(x,x).
Same as mesh but draws mesh lines in one direction only.
Three-dimensional plotting functions. Table 5.4–1, page 250.
2-‐28
Plots of the surface z = xe−(x2+y2) created with the meshfunction and its variant forms: meshc, meshz, and waterfall. a) mesh, b) meshc, c) meshz, d) waterfall.Figure 5.4–4, page 250.
2-‐29
Vector plotQuantities at grid points sometimes are required to be plotted in a vector formThe vectors at grid points may be plotted by the quiver command:quiver(x , y , u , v , s)x Array for x-coordinatey Array for y-coordinateu Array of the function uv Array of the function vs Scale factor for vector adjustment
clear , clfxmax= 0.9 ; xmin= -xmax ; dx= 0.1 ;ymax= xmax ; ymin= -ymax ; dy= dx ; xm = xmin : dx : xmax ; ym = ymin : dy : ymax ;[x,y] = meshgrid(xm,ym) ; u = -0.1*(2*y)./(x.^2+y.^2) ; v = -0.1*(-2*x)./(x.^2+y.^2) ;quiver(x,y,u,v,1.5) ; title('Velocity vector plot of free vortex')xlabel('x') ; ylabel('y')axis('square')
This script is an example of a vector plot for a free vortex plane potential flow
Polynomial fit to a given data
A polynomial is required to fit the following data:
The required script for fitting a polynomial and plotting the data is given in the next sheet
yx3.8871.14.2762.34.6513.92.1175.1
clear , clf , hold offx= [1.1,2.3,3.9,5.1] ;y= [3.887,4.276,4.651,2.117] ;a= polyfit(x,y,length(x)-1)xi= 1.1:0.1:5.1 ;yi= polyval(a,xi) ;plot(xi,yi,'r')text(4.2,4.5,'fit','fontsize',[14],'color','r');xlabel('x') ; ylabel('y')hold onplot(x,y,'--',x,y,'+b')
The output polynomial coefficients are printed in the command window as:
a = -0.2015 1.4385 -2.7477 5.4370
Consider the following data:yx2040.53171.5112102.58363.52414.515
It is required to fit a polynomial to the data shown and to plot the results
clear , clf , hold offa=[0 2 ; 0.5 4 ; 1 3 ; 1.5 7 ; 2 11 ; 2.5 10 ;…
3 8 ; 3.5 6 ; 4 2 ; 4.5 1 ; 5 1] ;x= a(:,1) ; y = a(:,2) ; b=polyfit(x,y,length(x)-1)xi=0:0.01:5 ; yi=polyval(b,xi) ;plot(xi,yi,’r’)hold onplot(x,y,’- -’,x,y,’+b’)xlabel('x') ; ylabel('y') ;text(3,10,’+- - - - Data points’, …
’color’,’b’,’fontsize’,[10])text(3,9,’+- - - Fitted polynomial’,…’color’,’r’,’fontsize’,[10])
The polynomial coefficients are as follows:
b =Columns 1 through 5
0.0248 -0.6787 7.8772 -50.5799 195.9348
Columns 6 through 9-467.8019 673.3763 -545.9602 217.2868
Columns 10 through 11-28.4794 2.0000