Tuesday, March 21, 2017

                                      Two-Dimensional Plots
Plots are very useful tools for presenting information. Using MATLAB we can easily plot two-dimensional and three-dimensional plots. Plot command:

>> x = [1 2 5 7 7.5 10];
>> y = [2 6.5 7 7 7.5 8];
>> plot(x,y)



Once the plot command is executed, the figure window opens and plot is displayed. The plot command has optional arguments that can be used to specify the color and style of the line and color and type of marker. With these options plot command has form:





Line specifiers: Line specifier are optional and can be used to define the type of line, color of line and type of marker (If markers are specified).
Line Color
specifier
red
R
green
G
Blue(default)
B
Cyan
C
Magenta
m
Yellow
Y
black
K
white
W


Line type
specifier


Solid(default)
-
dashed
---
dotted
:
dash-dot
-.


Marker Type
specifier
Plus sign
+
circle
O
Asterisk
*
point
.
square
S
diamond
d
Five-pointed star
P
Six-pointed star
h

·         Specifiers are typed inside the plot command as string and within the string they can be typed in any order.
Some examples:
  • ·         plot(x,y)  blue solid line connects the points with no markers.
  • ·         plot(x,y,'r') red solid line connects the points.
  • ·         plot(x,y,'--y') A yellow dashed line connects the points.
  • ·         plot(x,y,'*') The points are marked with * and there is no line between the points.
  • ·         plot(x,y,’g:d’) green dotted line connects the points that are marked with diamond markers.


Below written Properties are optional in MATLAB and used to specify the thickness of line, the size of marker, color of marker’s edge and color of marker’s face. Four properties and possible values are:

Property Name
Description of property
Possible Value of property
linewidth
Specifies the width of the line
A number()
markersize
Specifies the size of the marker
A number()
markeredgecolor
Specifies the color of the marker or color of edge line for filled markers.
Color specifier from above table
markerfacecolor
Specifies the color of the filling for filled markers.
Color specifier from above table

Example of plot command using all the properties:

plot(x,y,'mo','LineWidth',2,'markersize',12,'MarkerEdgeColor','g',
'markerfacecolor','y')
Above command creates a plot that connects the points with a magenta solid line and circles as markers at the points. The line width is two types and size of the circle markers is 12 points and marker has green edge line and yellow filling.

Plot of a Function:
Steps for plotting a function:
·         Create vector x with the domain of the function.
·         Create vector y with the function value at each x.
·         Plot y as a function of x.
After going through following example steps will be clear to us.

Q) Plot the function f(x) = 0.6*(x^5) - 5*(x^3) + 9*x + 2 for -4<x<4?

clc
clear all
x = -10:0.01:10;
y = 0.6*x.^5 -5*x.^3 + 9*x + 2;
plot(x,y)



The fplot command:
The fplot command plots a function with the form y = f(x) between specified limits. Syntax for using command:


The function can be typed as a string inside the command. The function can include functions that are created by user in the function file.
Limits: The limits can be a vector with two elements that specify the domain of x [xmin,xmax], or a vector of four elements which specifies the domain of x and limits of y-axis [xmin,xmax,ymin,ymax].
Line specifiers are the same as in plot command.

Q) Plot the function f(x) = (x^2 – x + 1)/(x^2 + x + 1) for -10<x<10 using fplot command?
clc
clear all
close all
fplot('(x^2-x+1)/(x^2+x+1)',[-10 10],'b');
%above command plot the function with blue colour from -10 to 10.

PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT:
Two or more graphs can be created in the same plot by typing pairs of vectors inside the plot command.The command has following form:
Plot(x,y,u,v,t,h)
Above command creates three graphs: y vs. x, v vs. u and h vs. t, all in  the same plot.

Q) Plot the function y = 3*x^3 -20*x + 10, and its first and second derivative, for -2<x<4 , all in the same plot?
*link for downloading program: multiplegraphinsameplot.m from folder 2D graph

%plotting multiple graph in same colour
%plot(x,y,u,v)
x = [-3:0.01:3];%create vector x
y = 3*x.^3 - 20*x + 10;%create vector y with function value at each x
yd = 9*x.^2 - 20;%create vector yd (derivative of y)
ydd = 18*x;%create vector ydd (derivative of yd)
plot(x,y,'r',x,yd,'b',x,ydd,'g');



hold on and hold off command:
If after plot command, hold on command is typed, this keeps the figure window open with first plot. Additional graph can be added to the same figure by using plot command. The hold off command stops this process of adding graph to the figure.

Q) Plot the function y = 3*x^3 -20*x + 10, and its first and second derivative for -2<x<4 in the same figure using hold on and hold off command?

%plotting multiple graph on same figure using hold on and hold off command
x = [-3:0.01:3];%create vector x
y = 3*x.^3 - 20*x + 10;%create vector y with function value at each x
yd = 9*x.^2 - 20;%create vector y with derivative of y
ydd = 18*x;
plot(x,y,'r');
hold on
plot(x,yd,'g');
plot(x,ydd,'b');
hold off


*link for downloading program: holdon.m from folder 2D graph
FORMATTING A PLOT USING COMMANDS:
xlabel and ylabel commands:
Labels can be placed next to the axis with the xlabel and ylabel commands in the following form:
xlabel('text as string');
ylabel('text as string');

The grid command:
grid on     Add grid lines to the plot
grid off     Removes grid lines from the plot
The tittle command:
A tittle can be added to the plot with the command:
  title('text as string')
The text is played at the top of the figure as a title.

The text  and gtext command:
text command places the text in the figure. Command has form:
text(x,y,'text as string')
gtext('text as string')
The text command places the text in the figure such that the text starts from the point with the coordinates x, y (according to the axes in the figure).
The gtext command starts the text from the position specified by the user. When the command is executed, the figure window opens and user specifies the position at which we want to display the text.
The axis command:
The axis command can be used to change the range and appearance of the axes.
axis( [xmin],[xmax])  Sets the limits of the x axis (xmin and xmax are numbers)
axis([xmin,xmax,ymin,ymax]) Sets the limit of both the x and y axes







No comments:

Post a Comment