LAB ACTIVITY 1 - Introduction To MATLAB PART1
LAB ACTIVITY 1 - Introduction To MATLAB PART1
College of Engineering
Computer Engineering Department
Experiment No.1
Introduction to MATLAB (Part 1)
Score
Submitted by
Mojica, Zedrik Maxwell M.
Submitted to
Maria Concepcion A. Mirabueno.
Instructor
Date Submitted
September 13, 2020
Experiment No. 1
Introduction to MATLAB (Part 1)
I. OBJECTIVES
The objectives of this experiment, using the MATLAB software, are for the students:
1. To be familiar with the interface and functionalities of MATLAB.
2. To learn the different commands and functions used in MATLAB.
III.DISCUSSION
MATLAB is a very important tool used for making long complicated calculations and
plotting graphs of different functions depending upon our requirement. An m-file is created in
which the basic operations are performed which leads to short and simple computations of some
very complicated problems in no or very short time.
Benefits
Some Benefits of MATLAB are given as follows:
o Simple to use
o Fast computations are possible
o Wide working range Solution of matrix of any order
o Desired operations are performed in matrices
o Different Programming languages can be used
o Simulation is possible
In this laboratory activity, we will have an introduction to MATLAB and get started with
working in its environment. In the following paragraphs, you are provided with a tutorial in
MATLAB.
IV. TUTORIAL WITH EXAMPLES (Paste the screenshot if required)
DEFINITION OF VARIABLES
Variables are assigned numerical values by typing the expression directly, for example,
Output Screenshot:
Now, type
What’s the difference in their output?
A variable can be assigned using a formula that utilizes these operators and either numbers or
previously defined variables. For example, since a was defined previously, the following
expression is valid b = 2*a;
To determine the value of a previously defined quantity, type the quantity by itself.
If your expression does not fit on one line, use an ellipsis (three or more periods at the end of the
line) and continue on the next line.
c = 1+2+3+...
5+6+7
There are several predefined variables which can be used at any time, in the same manner as
user-defined variables:
For example,
Output Screenshot:
Output Screenshot:
Output Screenshot:
Output Screenshot:
Output Screenshot:
Note that exp can be used on complex numbers. For example, with y = 2+8i as defined above,
Output Screenshot:
DEFINITION OF MATRICES
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices.
Therefore, vector and matrix operations are as simple as common calculator operations. Vectors
can be defined in two ways. The first method is used for arbitrary elements
Output Screenshot:
Note that commas could have been used in place of spaces to separate the elements. Additional
elements can be added to the vector:
Output Screenshot:
Output Screenshot:
The second method is used for creating vectors with equally spaced elements:
Output Screenshot:
Note that the middle number defines the increment. If only two numbers are given, then the
increment is set to a default of 1
Output Screenshot:
Matrices are defined by entering the elements row by row:
Output Screenshot:
Output Screenshot:
Output Screenshot:
Output Screenshot:
Output Screenshot:
Output Screenshot:
Operations and functions that were defined for scalars in the previous section can also be used on
vectors and matrices. For example,
Output Screenshot:
For example, to obtain a vector x that contains the elements of x(t) = tcos(t) at specific points in
time, you cannot simply multiply the vector t with the vector cos(t). Instead you multiply their
elements together:
Output Screenshot:
M-FILES
M-files are macros of MATLAB commands that are stored as ordinary text files with the
extension "m", that is ‘filename.m’. An m-file can be either a function with input and output
variables or a list of commands. MATLAB requires that the m-file must be stored either in the
working directory or in a directory that is specified in the MATLAB path list.
For example, consider using MATLAB on a PC with a user-defined m-file stored in a directory
called "\MATLAB\MFILES". Then to access that M-file, either change the working directory by
typing cd\matlab\mfiles from within the MATLAB command window or by adding the directory
to the path. Permanent addition to the path is accomplished by editing the \MATLAB\matlabrc.m
file, while temporary modification to the path is accomplished by typing
path(path,'\matlab\mfiles') from within MATLAB. Or, this can easily be achieved through the
path browser. As example of an M-file that defines a function, create a file in your working
directory named yplusx.m that contains the following commands:
function yplusx(y,x)
z=y+x
The following commands typed from within MATLAB demonstrate how this m-file is used:
x=2
y=3
z = yplusx(y,x)
All variables used in a MATLAB function are local to that function only. Variables which are
used in a script m-file which is not a function are all global variables.
MATLAB m-files are most efficient when written in a way that utilizes matrix or vector
operations. Loops and if statements are available, but should be used sparingly since they are
computationally inefficient. An example of the use of the command for is
Output Screenshot:
Now, type and discuss the difference of output and the code with the
previous step
Output Screenshot:
There are no differences in the output however the code that was used before
implemented a for loop.
Suppose that you want to run an m-file with different values of a variable T. The following
command line within the m-file defines the value:
T = input('Input the value of T: ')
Whatever comment is between the quotation marks is displayed to the screen when the m-file is
running, and the user must enter an appropriate value.
PLOTTING GRAPHS
Commands: plot, xlabel, ylabel, title, grid, axis, axes, stem, subplot, zoom, hold
The command most often used for plotting is plot, which creates linear plots of vectors and
matrices; plot(t,y) plots the vector t on the x-axis versus vector y on the y-axis. There are options
on the line type and the color of the plot which are obtained using plot(t,y,'option'). The linetype
options are '-' solid line (default), '--' dashed line, '-.' dot dash line, ':' dotted line. The points in y
can be left unconnected and delineated by a variety of symbols: + . * o x.
The following colors are available options: r, g, b, k, y, m etc. For example, plot(t,y,'--') uses a
dashed line, plot(t,y,'*') uses * at all the points defined in t and y without connecting the points,
and plot(t,y,'g') uses a solid green line. The options can also be used together, for example,
plot(t,y,'g:') plots a dotted green line. To plot two or more graphs on the same set of axes, use the
command plot(t1,y1,t2,y2), which plots y1 versus t1 and y2 versus t2.
The problem that you will encounter most often when plotting functions is that MATLAB will
scale the axes in a way that is different than you want them to appear. You can easily override
the auto scaling of the axes by using the axis command after the plotting command axis([xmin
xmax ymin ymax]);
where xmin, xmax, ymin, and ymax are numbers corresponding to the limits you desire for the
axes. To return to the automatic scaling, simply type axis. For discrete-time signals, use the
command stem which plots each point with a small open circle and a straight line. To plot y[k]
versus k, type stem(k,y)
You can use stem(k,y,'filled') to get circles that are filled in.
To plot more than one graph on the screen, use the command subplot(mnp) which partitions the
screen into an mxn grid where p determines the position of the particular graph counting the
upper left corner as p=1. For example,
subplot(211),semilogx(w,magdb); subplot(212),semilogx(w,phase);
plots the bode plot with the log-magnitude plot on top and the phase plot below. Titles and labels
can be inserted immediately after the appropriate semilogx command or plot command. To
return to a full screen plot, type
subplot(111).
where "filename" is a name of your choice. To retrieve the data later, type
load filename
GENERAL INFORMATION
• MATLAB is case sensitive so "a" and "A" are two different names.
• Comment statements are preceded by a "%".
• You can make a keyword search by using the help command.
• The number of digits displayed is not related to the accuracy. To change the format of the
display, type format short e for scientific notation with 5 decimal places, format long e for
scientific notation with 15 significant decimal places and format bank for placing two significant
digits to the right of the decimal.
• The commands who and whos give the names of the variables that have been defined in the
workspace.
• The command length(x) returns the length of a vector x and size(x) returns the dimension of the
matrix x.
V. PROCEDURES
1. Type the following vector in MATLAB: . Capture and paste the
output.
Output Screenshot:
2. Add an element to the vector by typing type . Capture and paste the output.
Output Screenshot:
3. Plot the result of the vector addition with grid lines. Type the following:
Output Screenshot:
4. MATLAB can make other graph types as well, with axis
Output Screenshot:
5. MATLAB can use symbols in plots as well. Here is an example using stars to mark the
points. MATLAB offers a variety of other symbols and line types.
Output Screenshot:
6. Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of
a matrix.
Output Screenshot:
Output Screenshot:
8. Adding a new Column:
Output Screenshot:
Output Screenshot:
10. Now let’s multiply these two matrices together. Note again that MATLAB doesn’t require
you to deal with matrices as a collection of numbers. MATLAB knows when you are
dealing with matrices and adjusts your calculations accordingly.
Output Screenshot:
11. Instead of doing a matrix multiply, we can multiply the corresponding elements of two
matrices or vectors using the.* operator.
Output Screenshot:
Output Screenshot:
13. Next, we illustrate the fact that a matrix times its inverse is the identity matrix.
Output Screenshot:
Output Screenshot:
VI. ANALYSIS / OBSERVATION
VII. CONCLUSION
In conclusion, MATLAB is an incredible useful program for engineers as it can solve real
life problems involving matrices and solves them in no time at all.