## MATLAB MODULE 1

MATLAB Window Environment and the Base Program

## Plotting

MATLAB is an outstanding tool for visualization. In the following, we will learn how to create and print simple plots.

We are going to plot sinusoidal oscillations with exponential decay. To do this, first generate the data ( x- and y- coordinates).   x- coordinate in this case is time steps. Let the initial time =0 sec and final time =10 sec with an interval of 0.05 sec. y- coordinate is the value of sinusoidal oscillations represented by . This plot can be generated by entering the following commands.

>> t=0:0.05:10;       % Generating time steps >> yt=exp(-t/2).*sin(2*pi*t);     %Calculate y(t ) >> plot(t,yt);   %Plot t vs. y(t ) >> grid on;    %Generating grids on x- and y-coordinates >> xlabel('Time Steps: t --->');       %Labeling x-axis >> ylabel('Sinusoid with exponential decay: y(t) --->');      %Labeling y-axis >> title('Plotting exp(-t/2)*sin(2*pi*t)');       %Put a title on the plot Response is shown in graphics or figure window snapshot (Fig. M1.15).

Arguments of the xlabel, ylabel, and title commands are text strings. Text strings are entered within single-quote characters. Lines beginning with % are comments; these lines are not executed. The print command sends the current plot to the printer connected to your computer.

Rather than displaying the graph as a continuous curve, one can show the unconnected data points. To display the data points with small stars, use plot(t,yt,'*'). To show the line through the data points in red color as well as the distinct data points, one can combine the two plots with the command plot(t,yt,'r',t,yt,'*'). To learn more about plot options, type help plot on the MATLAB prompt and hit return.

One can also produce multiple plots in a single window. Enter the following sequence of commands to your MATLAB command window and observe the resultant figure (Fig. M1.16). Fig. M1.15 Plotting sinusoid with exponential decay

>> subplot(3,1,1);plot(sin(2*pi*t)); >> title('plot of sin(2*pi*t)'); >> subplot(3,1,2);plot(exp(-t/2)); >> title('plot of exp(-t/2)'); >> subplot(3,1,3);plot(exp(-t/2).*sin(2*pi*t)); >> title('multiply above two plots to get this'); ```
``` Fig. M1.16 Plotting multiple plots in a single figure window

The command subplot(m,n,p) breaks the figure window into an m-by-n matrix of small axes and selects the pth axes for the current plot. Labeling, title, and grid commands should be given immediately after the particular subplot command to apply them to that subplot. Learn more about subplot using help subplot .

Click on the Plot Editor icon once and then double click anywhere in the plot to open the Property Editor - Axes window (Fig M1.17). Using this property editor, title, axes, scale etc... of the plot can be changed.

Double clicking anywhere exactly on the curve opens the Property Editor � Lineseries (Fig M1.18). From this, plot type can be changed on the spot. Available plot options are: Line, Area, Bar, Stairs, and Stem. Line width and markers can be changed by pull down menu Line and Marker. Fig. M1.17 Axes property editor Fig. M1.18 Lineseries property editor

#### Exercise M1.7

Generate 50 linearly spaced time steps between 0 and 10. Calculate for:

1. and ,3, and 5. Plot all the three curves in a single figure.

2. k =3 and . Plot all the three curves in a single figure.

Both the plots should be with respect to time. In both the plots, mark each curve with its k and values ( Hint: Use Insert Text Box option from main menu bar). From the main menu bar, choose Tools Edit Plot and then click anywhere on a curve. Plot Editor will open. Try several available options. Further to this, do the following:

1. Create legends for both the plots.

2. Use zoom-in and zoom-out tools.

3. Rotate   plots using 3D rotation tool.

4. Click on the Data Cursor icon. Move mouse pointer anywhere in the graph sheet. A cross-hair cursor will appear. Clicking the cross-hair on the curve will give corresponding and axis values. Use Data Cursor to observe values at various time steps.

#### Exercise M1.8

Assign 0.5, , and to , respectively. Calculate for t = [0:0.1:10].

Obtain three plots for . Title the graph and label the Draw

all the three plots on the same graph sheet and mark each curve with appropriate value.

#### Exercise M1.9

1. Study the commands semilogx, semilogy, and loglog using help command.
2. Consider the complex number . Using MATLAB, generate the following two plots on semilog graph sheet for . Use subplot. Title both the plots, label the axes, and generate grid.

a) b) Hint: Use logspace command to generate 100 logarithmically spaced samples of between 0.01 and 1000 rad/sec. Use abs and angle commands for calculations as per ii(a) and ii(b) given above for each Use semilogx to plot.