Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB GRAPHICS 2-D.

Similar presentations


Presentation on theme: "MATLAB GRAPHICS 2-D."— Presentation transcript:

1 MATLAB GRAPHICS 2-D

2 FIGURE WINDOWS MATLAB directs graphics output to a window called figure that is separate from the command window. The figure function creates figure windows. Example: >>figure

3 2-D Plotting The plot function is used to produce two-dimensional curves, using x- and y-data matrices specified by the user. plot(xdata,ydata,’clm’) You can plot multiple lines at once, using pairs of x- and y-data, or triples of x, y. plot(x1,y1,’clm1’,x2,y2,’clm2’)

4 Examples: >> x = 0:10 ; >> y = 2*x + 3; >> plot(x,y) >> y1 = 4*x – 2; >> y2 = x + 2; >> plot(x,y1,x,y2)

5 Adding a Grid GRID ON creates a grid on the current figure
GRID OFF turns off the grid from the current figure GRID toggles the grid state

6 Color, Line Style and Marker
The user can specify the color, line style and marker of a graph. If not, a blue solid line without marker symbols, is produced. Some of them are: COLORS MARKERS LINE STYLES y yellow . point -- dashed m magenta x x-mark -. dashdot c cyan * star : dotted r red + plus g green o circle ^ triangle (up) b blue s square p pentagram w white d diamond h hexagram k black v triangle (down)

7 Graph a red sine wave >> x = 0:0.1:2*pi; >> y = sin(x); >> plot(x,y,’r’) >> grid on

8 Try to graph the following using the different line styles, markers and colors.
1. Graph y = 2cos3x 2. Graph the exponential function, logarithmic function, inverse trigonometric function, hyperbolic function with appropriate domain.

9 Adding Additional Plots to a Figure
By default, plot deletes existing lines and resets all axis properties when a new line is drawn. HOLD ON holds the current plot HOLD OFF releases hold on the current plot HOLD toggles the hold state

10 >> x = 0:0.1:2*pi; >> y = sin(x); >> plot(x,y,’r’) >> grid on >> hold on >> plot(x, exp(-x),’b:*’)

11 Controlling Viewing Area
ZOOM ON allows user to select viewing area ZOOM using the figure tool bar ZOOM OFF prevents zooming operations ZOOM toggles the zoom state LEFT mouse button zoom in (x2) RIGHT mouse button zoom out (x ½) Double-click LEFT zoom out completely

12 GRAPH ANNOTATIONS Example
Type the following commands in the command window. >>x=0:pi/20:2*pi; >>y=sin(x); >>plot(x,y,’bs-’,’linewidth’,2) >>hold on >>y1=cos(x); >>plot(x,y1,’r>:’,’linewidth’,2)

13 ADDING A TITLE TO A GRAPH
There are several ways to add title to a graph: 1. Using the Title Option on the Insert Menu. (i) Click the Insert menu in the Figure window menu bar and choose Title. (ii) Enter the text of the label and click anywhere in the figure background to close the text entry box.

14 ADDING A TITLE TO A GRAPH
2. Using the Property Editor to Add a Title. (i) Double click on the axes on the graph to open the Property Editor. (ii) Select the Style panel and type in the text of your title in the Title entry box. (iii) Click Apply.

15 ADDING A TITLE TO A GRAPH
3. Using the Title Function. To add a title to a graph at the MATLAB command prompt, use the title function. Example: >> title(‘Graph of Sine and Cosine Functions’)

16

17 ADDING A LEGEND TO A GRAPH
There are two ways to add legend to a graph: 1. Using the Legend Option on the Insert Menu. - Click on the Insert menu and choose Legend. 2. Using the Legend Function. To add a legend to a graph at the MATLAB command prompt, use the legend function. Example: >>legend(‘ Sine Function ’,’ Cosine Function ’)

18 ADDING AXES LABELS TO A GRAPH
There are three ways to add labels to a graph: 1. Using the Label Options on the Insert Menu. (i) Click on the Insert menu and choose label option that corresponds to the axes you want to label. (ii) Enter the text of the label, or edit the text of an existing label. 2. Using the Property Editor. (i) Start plot editing mode. (ii) Double click on the axes on the graph to open the Property Editor. (iii) Select the Labels panel and enter the text of the label in the appropriate text entry box. (iv) Click Apply.

19 ADDING AXES LABELS TO A GRAPH
3. Using the Label Commands. To add x, y and z axis labels to a graph use xlabel, ylabel and zlabel functions. Example: >>xlabel(‘ x-axis’,’FontSize’,16)

20 ADDING TEXT ANNOTATIONS TO A GRAPH
1. Creating Text Annotations in Plot Editing Mode. (i) Click on the Insert menu and choose the Text option or click the text button in the figure window toolbar. (ii) Position the cursor where you want to add a text annotation in the graph and click. (iii) Enter a text. (iv) Click anywhere in the figure background to close the text entry box.

21 ADDING TEXT ANNOTATIONS TO A GRAPH
2. Adding Text Annotations with the text or gtext Command. To create annotations using text function, the text and its location must be specified using the x and y coordinates. Example: a. >>str1(1) = {‘Sine Function:‘} >>str1(2) = {‘y=sin(x)‘} >>text(3,0.5,str1) b. >>str2(1) = {‘Cosine Function: ‘} >>str2(2) = {‘y1=cos(x)‘} >>text(0.3,-0.6,str2)

22 ADDING TEXT ANNOTATIONS TO A GRAPH

23 SAVING FIGURES You can save figures with Save or Save As through the File menu on the Figure Window. This will create a .fig file.

24 Displaying Multiple Plots per Figure
Format: subplot(m,n,i) This function breaks the figure window into m-by-n matrix of small subplots and selects the ith subplot for the current plot. Examples: 1. >> subplot(2,2,1) 2. >> subplot(2,3,1) >> subplot(2,2,2) >> subplot(2,3,2) >> subplot(2,2,3) >> subplot(2,3,3) >> subplot(2,2,4) >> subplot(2,3,4) >> subplot(2,3,5) >> subplot(2,3,6)

25 MULTIPLE PLOTS PER FIGURE
1 2 3 4 5 6 subplot(2,2,i) where i = 1 to 4 subplot(2,3,i) where i = 1 to 6

26 Example Type the following commands in the command window. >>x=0:pi/20:2*pi; >>y=sin(x); >>subplot(1,2,1) >>plot(x,y,’bs-’,’linewidth’,2) >>y1=cos(x); >>subplot(1,2,2) >>plot(x,y1,’r>:’,’linewidth’,2)

27 BASIC PLOTTING COMMANDS

28 ezplot ezplot is an easy to use function plotter for algebraic and transcendental functions, parametric equations, implicit and explicit functions. ezplot(f) plots the expression f = f(x) over the default domain -2 < x <  ezplot(f,[a,b]) plots f = f(x) over a < x < b

29 Examples: >> subplot(2,3,1) >> ezplot(‘cos(x)’) >> subplot(2,3,2) >> ezplot(‘cos(x)’,[0, pi]) >> subplot(2,3,3) >> ezplot(‘1/y-log(y)+log(-1+y)+x-1’) >> subplot(2,3,4) >> ezplot(‘x^2+y^2-4’) >> subplot(2,3,5) >> ezplot(‘x^3+y^3-5*x*y+1/5’,[-3,3]) >> subplot(2,3,6) >> ezplot(‘sin(t)’,’cos(t)’)

30

31 POLAR CURVES Polar in polar coordinates can be created using the polar(t,r,S) function, where t is the angle vector in radians, r is the radius vector, and S is an optional character string describing the color, marker symbol, and/or line style. Example >>t=linspace(0,2*pi); >>r=sin(2*t).*cos(2*t); >>polar(t,r) >>title(‘Polar Plot’)

32 HISTOGRAM Histogram illustrates the distribution of values in a vector. hist(y) draws a 10-bin histogram for the data in vector y. hist(y,n), where n is a scalar, draws a histogram with n bins. hist(y,x), where x is a vector, draws a histogram using the bins specified in x. Example >>x=-2.9:0.2:2.9; %specify the bins to use >>y=randn(5000,1); %generate 5000 random points >>hist(y,x) %draw the histogram >>title(‘Histogram of Gaussian Data’)

33 PIE CHART Standard pie charts can be created using the pie(a,b) function, where a is a vector of values and b is an optional logical vectors describing a slice or slices to be pulled out of the pie chart. The pie3 function renders the pie chart with a 3-D appearance. Example >>a=[ ]; >>subplot(1,2,1) >>pie(a,a==max(a)); %produces chart a and pull out the biggest slice. >>subplot(1,2,2) >>explode=[ ]; >>pie(a,explode) % Which part is pulled out?

34 BAR GRAPHS Bar graphs display vector or matrix data. By default, a bar graph represents each element in matrix. Bars in a 2-D graph, created by bar function, are distributed along the x-axis with each element in a column drawn at a different location. All elements in a row are clustered around the same location on the x-axis. Example >> y =[5 2 1; 8 7 3; 9 8 6; 5 5 5;4 3 2]; >> subplot(1,2,1) >> bar(y) >> subplot(1,2,2) >>bar3(y)

35 SPECIALIZED PLOT COMMANDS
The following are some other specialized plot commands: area pie3 rose stairs stem3 quiver compass feather

36 MATLAB GRAPHICS 3-D

37 ezsurf ezsurf(f) creates a graph of f(x,y), where f is a string that represents a mathematical function of two variables, such as x and y. Example: >> subplot(1,2,1) >> ezsurf('x^2+y^2') >> subplot(1,2,2) >> ezsurf('x^2-y^2')

38 ezmesh ezmesh(f) creates a graph of f(x,y), where f is a symbolic expression that represents a mathematical function of two variables, such as x and y. Example: >> subplot(1,2,1) >> ezmesh('x^2+y^2') >> subplot(1,2,2) >> ezmesh('x^2-y^2')

39 OTHER PLOTTING COMMANDS
mesh contour contour3 waterfall surf plot

40 FIGURE WINDOW TOOLS

41 ENJOY MATLAB GRAPHICS !


Download ppt "MATLAB GRAPHICS 2-D."

Similar presentations


Ads by Google