Matlab Quick Plotting and Line Specifiers

In this section Matlab Quick Plotting and Line Specifiers , we are going to discuss on how to plot in Matlab Quickly and how to represent line specifications.

Matlab Quick Plotting and Line Specifiers Video

For given data (in numerical form or in equations form), we plot in Matlab by using following methods,

1.  plot  command in command window/ editor window
2.  fplot command
3.  TOOLSTRIP in the main window.

I. Matlab Quick Plotting

Generating Data for 2D Plotting

Let us assume that the given for x and y variables are

(i)    In numerical form

x= [1.0  1.7  2.5  3.4  5.1  6.5  7.1 8.2 10];
y=[1.8 2.5 6.2  7.2 8 4.5 5.7 6.3 9];

(ii)    In equation form

$$ f(x)=\frac{x^2-4x+9}{2x+5}$$, for $$-1\leq x\leq 5$$

Now we will plot using first method “plot command”

1. By Using ‘plot’ command

The basic syntax of the plot is ‘plot(x)’or ‘plot (x,y)’
Now in the Matlab command window or editor window write the above equations or just copy following lines and paste in the command window

%% program
x= [1.0  1.7  2.5  3.4  5.1  6.5  7.1 8.2 10];
y=[1.8 2.5 6.2  7.2 8 4.5 5.7 6.3 9];
plot(x)

plotx1

x= [1.0  1.7  2.5  3.4  5.1  6.5  7.1 8.2 10];
y=[1.8 2.5 6.2  7.2 8 4.5 5.7 6.3 9];
plot(x,y)

plotxy2

From two figures, we can observe that, in the first figure, plotting is done with a number of  elements in x, i.e. 9  to the corresponding x values. Whereas in the second figure, plotting is done with x versus y.

Similarly for mathematical equation (ii)
x1=linspace(-1,5,100);
y1=(x1.^2-4*x1+9)./(2*x1.^2+5);
plot(x1,y1)

fplot1

Note :
In plot command, default line color is ‘BLUE’, ‘STRONG’ and 0.5 units width
Note:
MATLAB does not understand functions; it can only use arrays of numbers of “equal length”, which are variables  and x axis values should be increasing.
%%  Practice
$$x=0\rightarrow 2\pi$$
$$y=sin(x)$$

2.  By using fplot

The syntax of the fplot is : fplot(‘function’,limits) or  fplot((‘function’, limits, ‘line specifiers’)

The following equation

$$ f(x)=\frac{x^2-4x+9}{2x+5}$$, for $$-1\leq x\leq 5$$
is plotted in Matlab as
fplot(‘(x^2-4*x+9)/(2*x^2+5)’,[-1,5])

Note:
for one variable x – [xmin,xmax]
For two variables x, y –  [xmin,xmax, ymin,ymax]

Practice:

  • $$ j(x)=’x^2+4*sin(2*x)-1$$, for $$-3\leq x\leq 3$$
  • $$ j(x)= 2e^{(-t^2)}$$, for $$-2\leq x\leq 2$$

3. By using TOOLSTRIP

toolstrip

Define the variable in the command window, which appear in Workspace, select the variable or variables to be plotted, and go to the plots strip and select the required plot format.

Note:
Select the x variable first and then select y variable  (using Shift button or Ctrl button)
You can change by using Switch button
If one element is selected, then plotting is number of elements vs selected variable.

I. Matlab Plot Line Specifications or Specifiers

 In general, all the plots were done in the Matlab default color is blue and line width is 0.5 units. If you have multiple plots to be compared in the single Matlab window. Then we need a different option to distinguish between all the different plots.
So, all the features can be changed by selecting a different option within the plot command. Now we have  to add color, line style and markers in the plot. We can also modify line width, marker edge and fill color and marker size.

In the MATLAB
The line colors 
r= red, g= green,  b-blue, c=cyan, w=white, k=black, y=yellow  and m=magenta.

Syntax:
plot(x,y, ‘linecolor’,’r’) or plot(x,y, ‘r:’)

The Line style
Default is solid (-), other formats are  dash (–), dot(:), dash-dot (-.)and non (invisible).

Syntax:
plot(x,y, ‘linestyle’,’–‘) or plot(x,y, ‘–’)
The Line width
Default is 0.5, but you can increase.

Syntax:
plot(x,y, ‘linewidth’,2)

The Line Markers

By default non, but you can use plus(+),circle(o),asterisk(*), point(.), triangle up (^), triangle down (v), triangle left (<), triangle tight (>),  square (s) , diamond (d), 5 pointed star ( p), 6 p0inted star (h).

Syntax:
plot(x,y, ‘marker’,’+’) or plot(x,y, ‘+’)

All together

plot(x,y, ‘r:o’, ‘LineWidth’, ‘2’, ‘MarkerSize’, ‘12’, ‘MarkerEdgeColor’, ‘c’, ‘markerfacecolour’, ‘y’)

Note:
Line specifiers are used in ‘plot’ command, all three together in any order.

>>plot(x,y,’r- – o’)       or      >> plot(x,y,’o–r’)