Practical:3: Software Practices Lab Manual
Practical:3: Software Practices Lab Manual
Practical:3: Software Practices Lab Manual
Manual
Practical :3
MATLAB has several commands to allow you to display results of your computations
graphically. The plot command is the simplest way of doing this. If x is a vector, plot(x) will
plot the elements of x against their indices. The adjacent values of x will be connected by lines.
For example, to plot the discrete-time sequence that is a sinusoid of frequency pi/6, you would
type:
>>n=0:11;
>> y = sin((pi/6)*n;
>> plot(y)
If x and y are two vectors of the same length then plot(x,y) plots x versus y.
For example, to obtain the graph of y = cos(x) from 0 to 2π , we can first define the vector x
with components equally spaced numbers between 0 and 2π , with increment, say 0.01.
» x= [0:0.01:2*pi];
» y=cos(x);
» plot(x,y)
It is good practice to label the axis on a graph and if applicable indicate what each axis represents. This
can be done with the xlabel and ylabel commands.
» xlabel('x')
» ylabel('y=cos(x)')
Inside parentheses, and enclosed within single quotes, we type the text that we wish to display along the x
and y axis, respectively. We could even put a title on top using
» title('Graph of cosine from -pi to pi')
INDOWS OF MATLAB
As shown in fig 1.2 the main window on the right is called the Command Window. This is the
window in which you
interact with MATLAB. Once the MATLAB prompt >> is displayed, all MATLAB commands
are
executed from this window. In the figure, you can see that we execute the command:
>> a = 7
In the top left corner you can view the Workspace window and the Current Directory window.
Swap
from one to the other by clicking on the appropriate tab. Note that when you first start up
MATLAB,
the workspace window is empty. However, as you execute commands in the Command window,
the
Workspace window will show all variables that you create in your current MATLAB session. In
this
example, the workspace contains the variable a.
During the MATLAB sessions you will create files to store programs or workspaces. Therefore
create an appropriate folder to store the lab files.
Set the current directory to be your students drive by clicking on the Browse button next to the
Current
Directory path. Go to My Computer (at the top), click on the + and select drive as given in the fig
1.3.
MATLAB HELP
MATLAB’s help system provides information to assist you while using MATLAB. Select Help
MATLAB Help to open the help system.
You can then browse the commands via the Contents window, look through the Index of
commands or Search for keywords or phrases within the documentation. This is the most
comprehensive documentation for MATLAB and is the best place to find out what you need.
You can also start the MATLAB Help using the helpwin command.
If you type help , MATLAB will provide help for that particular command. For example,
help hist will call up information on the hist command, which produces histograms. Note that the
help command only displays a short summary of how to use the command you are interested in.
For more detailed help documentation, use the doc command. For example, doc hist will display
the full documentation of the hist command, including examples of how to use it and sample
output.
MATLAB VARIABLES
Structure for the storage of all data in MATLAB is a matrix. MATLAB stores scalar values as
matrix variables with 1 row and 1 column. These variables show in your workspace is shown as
1x1 matrices.
Assigning Variables
To create a (scalar) variable in MATLAB simply enter a valid variable name at the command
line and assign it a value using =. Once a variable has been assigned a value it is added to the
MATLAB Workspace and will be displayed in the Workspace window. For example, after
entering:
>> height = 5
height = 5
>> width = 4
width = 4
the Workspace window will contain both height and width as scalars as shown in fig 1.4.
You should choose variable names that indicate quantities they represent, e.g., width,
cableLength, water_flow. You can use multiple words in the same variable name either by
capitalizing the first letter of each word after the first one, e.g., cableLength, or by putting
underscores between words, e.g., water_flow
Calculations with Variables
Once you have added variables to the MATLAB Workspace, you can use them within
calculations. For example, to calculate the area of a 5 x 4 rectangle you could enter:
>> height = 5
>> width = 4
>> area = height * width