Lab 3
Lab 3
LAB 3
MATLAB
(M-file, Plotting )
Student Name
Student ID
Student Score
1
M-file
MATLAB is a programming language, as well as an interactive
computational environment. Files that include code in the MATLAB
language are called M-files.
M-files are ordinary ASCII text data written in MATLAB's language. Such
files are called "M-files" because they must have the filename extension
".m" at the end of their name (like myfunction.m). This extension is
needed for this data to be interpreted by MATLAB.
On clicking the new script icon or using the keyboard shortcut, a blank
untitled file gets opened.
The blank file has a default directory to save it. We can change the file
storage location as per our requirement.
Save the script with the keyboard shortcut Ctrl + s or use the Save
icon which is placed along with the new script icon.
2
Use Ctrl + O keyboard shortcut or open file icon to open an already
existing script.
There are three more menu tabs available to create a script as, Editor,
Publish, and View.
The Command Window area is divided into two panes, one for Script
editor and another one for Command Window.
3
MATLAB Plotting
Plotting is a graphical representation of a data set that shows a
relationship between two or more variables. MATLAB plots play an
essential role in the field of mathematics, science, engineering,
technology, and finance for statistics and data analysis.
The plot command is used to create two-dimensional plots. The simplest
form of the command is:
Example 1:
>> plot(a, b)
Output:
4
Example 2: Plot the function sin (a) on the interval [0, 3π], we first create
a vector of a values ranging from 0 to 3π, then compute the sine of
these values, and finally plot the result:
The plot command has additional, optional arguments that can be used
to specify the color and style of the line and the color and type of
markers, if any are desired. With these options the command has the
form:
Some examples:
5
plot(x,y) A blue solid line connects
the points with no markers
(default).
plot(x,y,‘r’) A 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 *
(no line between the points).
plot(x,y,‘g:d’) A green dotted line connects
the points that are marked
with diamond markers.
Exercise
1- Plot the cosine at the interval from 0<x<4π ,and use range
300.
rng(now);
list = 100*rand(rows,columns);
histogram(list,bins)
Save your script and run the code using either of these methods:
6
Type the script name on the command line and press Enter. For
example, to run the numGenerator.m script, type numGenerator.
On the Editor tab, click the Run button.
Homework
7
Plotting Manual