05. Matlab Graphics PDF

Title 05. Matlab Graphics
Author SARAH ALIA BINTI SHA F20SP1109
Course Programming for Engineers
Institution Universiti Teknologi Malaysia
Pages 25
File Size 2.1 MB
File Type PDF
Total Downloads 411
Total Views 816

Summary

byMohamed HusseinMATLAB GRAPHICScompliments toProf. Michael NegnevitskyUniversity of TasmaniaLecture 2Matlab Graphics Creating Simple Plots Manipulating PlotsCreating Simple PlotsPlots are a powerful visual way to interpretdata.Matlab has an extensive graphicscapabilities.Case Study: y = sin( x ...


Description

MATLAB GRAPHICS by Mohamed Hussein compliments to Prof. Michael Negnevitsky University of Tasmania

Lecture 2 Matlab Graphics  Creating

Simple Plots  Manipulating Plots

Creating Simple Plots Plots are a powerful visual way to interpret data.  Matlab has an extensive graphics capabilities. 

Case Study: y = sin(x) 

Plot a sine function over one period: y = sin(x) for 0 < x < 2

First we choose data point for the independent variable x. This data forms the horizontal axes of the plot.  Then the sine of each data point is found this provides the vertical axes of the plot.  Each pair {xn, yn} is then marked on a suitable set of axes. 

Case Study: y = sin(x) (cont.) 

Matlab uses arrays to accomplish this task: >> x = linspace(0,2*pi,30); linspace(0,2*pi,30); creates 30 points between 0 and 2 . >> y = sin(x); finds the sine of the points in x. >> plot( plot(x,y x,y) x,y) generates the plot.

Case Study: y = sin(x) (cont.)

The Matlab function plot automatically chooses axis limits,  marks the individual data points, and  draws straight lines between them. 

Options in the plot command allow us to plot multiple data sets on the same axes,  use different line types (dotted and dashed),  mark just the data points without drawing lines between them,  use different colors for curves,  place labels on the axes, a title on the top,  draw a grid at the tick marks.  in SciLab use clf to clear plot area 

>> z = cos(x); >> plot(x,y,x,z)

>> plot( plot(x,y,x,y x,y,x,y,'+') x,y,x,y,

Plots a sine twice, once with lines connecting the data points, the second with the data points marked with the symbol +.

>> plot(y,z)

Plots sine versus cosine.

>> plot(x,y,x,2*y.*z, '--')

Plots 2sin(x)cos(x) using dashed lines.

>> grid

Places a grid a the tick marks of the current plot.

>> xlabel('independent variable X')

Places an x-label on the current plot.

>> ylabel('dependent variables')

Places a y-label on the current plot.

>> title('2sin(x)cos(x) = sin(2x)')

Places a title on the current plot.

Colours and Line Styles

>> x = linspace(0,2*pi,30); >> y = sin(x); >> z = cos(x); >> plot(x,y,'b:',x,z,'r--',x,y,'ko',x,z,'m+')

Manipulating Plots (not applicable in SciLab) We add lines to existing plots using the command hold. Matlab does not remove the existing curves when new plot commands are issued. It adds new curves to the current axes.  Setting hold off releases the current figure window for new plots. 

>> x = linspace(0,2*pi,30); >> y = sin(x); >> plot(x,y)

First we plot a sine curve.

>> hold on >> plot(x,z,'m')) >> hold off

Now we hold the plot and add a cosine curve.

 More

on XY Plotting  Other Types of Plotting  3D Plot (XYZ Plotting)

More on XY Plotting Other XY plotting commands are

axis ([xmin ([xmin xmax ymin ymax ymax]) ]) - sets the minimum and maximum limits of x & y axes

fplot( fhandle fplot( fhandle,, [[xmin xmin xmax xmax ]) - Performs Performs aa plotting plotting based based on on aa given given function function which is handle by fhandle

plot (y) - Plot the value of y against its indices or plots the imaginary values against real values if y having complex values

More on XY Plotting (cont.) The graph yy = 3x 3 – 5x22 + 2x -10 10 can canbe be plotted plottedusing usingthe the following following two two lines lines of of commands commands >> y = @(x) (3 *x^3 – 5 *x^2 + 2 *x – 10) >> fplot (y, [[--10

10])

(not in SciLab SciLab)) We Wecan canalso alsoplot plotaadata dataagainst againstits itsindices indicesusing using plot (y) >> y = [0

4

2

8

16

32]; plot (y)

or imaginary imaginary numbers numbers against againstreal real number number or >> y = [[-22--2i

-1-i

1+ ii 2+2i 2+2i ];]; plot plot (y) (y) 00 1+

More on Plot Enhancement Beside Beside changing changing the the lines lines color color and and marking, marking, labelling the the plot axes etc., there are more commands that can help user producing producing presentable presentable figure figure

gtext (‘text’)

- place the text at mouse clicked position (not in SciLab SciLab))

x,y,, ‘text’) - place the text at coordinates x,y text ((x,y x,y legend (‘legtxt1’, ’legtxt2’,…) - creates creates legend legend text text for for each each graph’s graph’s line line

subplot (m,n,p ( m,n,p) m,n,p) - divide divide aa Figure Figure windows windows into into an an array array of of m × n plots, p is the position position of the subplot within the plot array

figure (no.)

- creates creates or or bring bring into into focus focus Figure Figure with with the the specified number

More on XY Plotting (cont.) Try the followings: >> t = [0:100]; >> x =10 * (1 – exp exp ((-t /15 /15 )); yy == 12*(1 12*(112*(1 -exp(-exp( exp(-t /6)); z= 20*(1 20*(1--exp( exp(--t /8)); >> figure (5) >> subplot (2,3, 1); plot (t, x) >> subplot (2,3,3); plot (t, y) >> subplot (2,3,5); plot (t, z)

Exercise Based Basedon onthe theprevious previousexample, example,write writeaascript scripttotoproduce producethe the following plot

Other Types of Plotting There some specilaised plot command type cater various type of graphs

bar (x,y (x,y) x,y)

- creates creates aa bar bar chart chart of of y versus x

loglog (x,y x,y)) - creates creates aa plot plot of of log y versus log x plotyy (x1,y1,x2,y2) - creates creates aa plot plot with with two two y axes, y1 axis on the left and y2 axis on the right

polar ( , r,’ type’) - creates creates aa polar polar plot plot from from the the polar polar coordinates and r. type specifies type of data markers and colors.

Other Types of Plotting (cont.) There some specilaised plot command type cater various type of graphs

semilog x( x(x,y x,y x,y)) - creates a plot of of y versus log x semilogy (x,y x,y)) - creates a plot of log y versus x stairs (x, y)

- creates a stairs plot of yy versus x

stem (x, y)

- creates a stem plot of y versus x

Polar Plot and 3D Plot Try the followings: >> freq == [0 [0 :0.2 :0.2 :100]; :100]; >> gain = 1 ./ sqrt ( ((1 + (0.5 * freq) freq) .^2) .* (1 + (0.25 * freq freq)) .^2))); .^2))); >> phase = --(( atan (0.5 * freq )) atan (( 0.25 0.25 ** freq freq)); )) – ((atan freq)); >> polar (phase, gain, ‘ r - -’ )

>> t = 0: pi /50 : 10 * pi; >> plot3 (sin (t), cos (t), t) >> title ( ‘ Helix Plot ‘), xlabel (‘sine (‘sine t ’ ), ), ylabel (‘ cosine (t )’), zlabel (‘ t’)

Polar Plot and 3D Plot

Mesh and Surface Plots Matlab defines a mesh surface by the zcoordinates of points above a rectangular grid in the x-y plane.  The result looks like a fishing net with the knots at the data points.  Mesh plots are useful for visualising large matrices or plotting functions of two variables.



Mesh and Surface Plots (cont.) 

 

The first step in generating the mesh plot of a function, z = f(x, y), is to generate X and Y matrices consisting of repeated rows and columns, respectively, for some range of the variables x and y. Matlab provides the function meshrig. [X,Y]=meshgrid(x,y) creates matrix X, whose rows are copies of the vector x, and matrix Y, whose columns are copies of the vector y.

Mesh and Surface Plots (cont.) >> x = -7.5:0.5:7.5 >> y = x; >> [X,Y] = meshgrid(x,y); X and Y are a pair of matrices representing a rectangular grid of points in the x-y plane. >> r = sqrt(X.^2 + Y.^2) + eps; >> Z = sin(r)./r; The matrix r contains the radius of each point in [X,Y] - the distance from each point to the centre of the matrix, which is the origin (0, 0). Adding eps prevents dividing by zero.

>> mesh(X,Y,Z)

Mesh and Surface Plots (cont.) 



A surface plot looks like the mesh plot, except that the spaces between the lines (called patches) are filled in. Matlab provides the function surf.

>> surf(X,Y,Z)

Peaks Function  

Matlab provides the function peaks. Peaks is a function of two variables, obtained by translating and scaling Gaussian distributions, which is useful for demonstrating mesh, surf, pcolor, and contour.

>> mesh(peaks) >> title('Mesh plot of the peaks function')

>> [x,y,z] = peaks; >> contour(x,y,z,20)

% generate 20 2-D contours

>> [x,y,z] = peaks; >> contour3(x,y,z,20)

>> axis([-3 3 -3 3 -6 6])

% generate 20 3-D contours

% adjust scale





Another interesting way to visualise contour information is to use color for representing height. The pcolor function maps heights to a set of colours and presents the same information as the contour plot at the same scale.

>> z = peaks; >> pcolor(z) >> title('Peaks: pseudocolour plot')



We can also combine pseudocolour and contour plots of the peaks function.

>> [x,y,z] = peaks; >> colormap(hot) % select a set of colours >> pcolor(x,y,z) % generate the plot >> shading flat % remove the grid lines >> hold on >> contour(x,y,z,20,'k') % plot 20 contour lines >> hold off

Combined pseudocolour and contour plots

>> mesh(peaks(20) + 7) % coarse mesh, shifted up; >> hold on >> pcolor(peaks(20)) % add a pseudocolour plot >> hold off >> title('Mesh with hidden on')

>> hidden off >> title('Mesh with hidden off')

>> colormap(gray) >> surfl(peaks), shading interp...


Similar Free PDFs