Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
144 views

LAB ACTIVITY 1 - Introduction To MATLAB PART1

The document is an experiment report submitted by a student for an introduction to MATLAB course. It includes the objectives, materials, and a tutorial section with examples of MATLAB commands and functions. The tutorial covers defining variables, arithmetic operators, predefined variables and functions, matrices, m-files, plotting graphs, and more. Screenshots of MATLAB outputs are provided to demonstrate the code examples.

Uploaded by

Zedrik Mojica
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

LAB ACTIVITY 1 - Introduction To MATLAB PART1

The document is an experiment report submitted by a student for an introduction to MATLAB course. It includes the objectives, materials, and a tutorial section with examples of MATLAB commands and functions. The tutorial covers defining variables, arithmetic operators, predefined variables and functions, matrices, m-files, plotting graphs, and more. Screenshots of MATLAB outputs are provided to demonstrate the code examples.

Uploaded by

Zedrik Mojica
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Adamson University

College of Engineering
Computer Engineering Department

Control Systems Laboratory

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.

II. MATERIALS REQUIRED


Computer with MATLAB installed

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.

Some very important functions performed by MATLAB are given as follows:


 Matrix computations
 Vector Analysis
 Differential Equations computations
 Integration is possible
 Computer language programming
 Simulation
 Graph Plotation
 2-D & 3-D Plotting

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?

The output of the expression without the semicolon immediately displays


the result while the output of the expression with the semicolon doesn’t
display the output.

MATLAB utilizes the following arithmetic operators:

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:

PREDEFINED FUNCTIONS IN MATLAB


There are also a number of predefined functions that can be used when defining a variable. Some
common functions that are used in this text are:

For example, with y defined as above,

Output Screenshot:

Output Screenshot:

With a=3 as defined previously,

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:

For example, with ‘v’

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:

There are a number of special matrices that can be defined:

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:

Functions are applied element by element. For example,


Output Screenshot:

Operations that need to be performed element-by-element can be accomplished by preceding the


operation by a ".".

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.

An if statement can be used to define conditional statements. An example is


if(a<=2);
b=1;
elseif(a>=4)
b=2;
else b=3;
The allowable comparisons between expressions are >=, <=, <, >, ==, and ~=.

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.

To label your axes and give the plot a title, type


xlabel (‘time (sec)’) ylabel(‘step response’) title(‘my plot’)

Finally, add a grid to your plot to make it easier to read.


Type grid

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).

SAVING & LOADING


When using MATLAB, you may wish to leave the program but save the vectors and matrices
you have defined. To save the file to the working directory, type
save filename

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:

7. Adding a new Row.

Output Screenshot:
8. Adding a new Column:
Output Screenshot:

9. We can easily find the transpose of the matrix A.

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:

12. Let’s find the inverse of a matrix.

Output Screenshot:

13. Next, we illustrate the fact that a matrix times its inverse is the identity matrix.

Output Screenshot:

14. We can create a magic square and assigning it to the variable A.

Output Screenshot:
VI. ANALYSIS / OBSERVATION

MATLAB is high-performance language for technical computing. It integrates computation,


visualization, and programming in an easy-to-use environment where problems and
solutions are expressed in familiar mathematical notation.

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.

You might also like