0% found this document useful (0 votes)
68 views6 pages

Experiment 1 Avenido CP Final

The document provides an overview of an introductory MATLAB tutorial experiment. It discusses getting started with MATLAB, basic commands and functions, variables, matrices, and plotting. The goal is to explain essential MATLAB topics for beginners through examples and hands-on exercises.

Uploaded by

Julius Avenido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views6 pages

Experiment 1 Avenido CP Final

The document provides an overview of an introductory MATLAB tutorial experiment. It discusses getting started with MATLAB, basic commands and functions, variables, matrices, and plotting. The goal is to explain essential MATLAB topics for beginners through examples and hands-on exercises.

Uploaded by

Julius Avenido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 1: MATLAB® Familiarization

Julius R.L. Avenido

Department of Electrical and Electronics Engineering, University of San Carlos


Nasipit, Talamban, Cebu City, Philippines
[email protected]

Abstract—This document provides an overview of the first


experiment in EE2100L, also known as Computer Programming
2 for EE. The author performed the experiment with the goal of
understanding basic MATLAB commands and functions through
various exercises in the tutorial.

Keywords—basic, commands, functions, exercises, tutorial

I. INTRODUCTION
The tutorial used in the experiment is intended for beginners
in MATLAB. Various topics are discussed, though in an
introductory manner. The discussion on MATLAB functions is
strictly limited to what is only essential for electrical
engineering students for an initial hands-on experience. The Fig. 2 Assigning variable names
tutorial also requires that the discussion for a specific topic to be
dispersed out on the whole material and not just focused on one Variables cannot be defined with the same names as
case. The intention is to explain only what is significant as the MATLAB keywords, such as if or end.
students read and implement the discussed MATLAB
To remove a certain variable from the workspace, the
commands.
command is clear variableName. The command clear removes
II. METHODOLOGY all the variables in the workspace. The command clc clears all
the text from the Command Window, resulting in a clear
A. Getting Started screen.
The MATLAB prompt (>>) is where formulas, commands,
or functions are typed next to. Pressing the Enter key processes The MATLAB variables are case sensitive, i.e. the variable
the command. A simple mathematical calculation can be ‘J’ is different from ‘j’.
performed by typing it at the prompt. MATLAB has integrated variables like pi (π), i and j (the
imaginary operator or -1), and eps (the smallest difference
between two numbers as represented in MATLAB). The user
should be careful not to use integrated variables or keywords as
variable names in the workspace.

Fig. 1 Simple mathematical calculation

MATLAB processed the addition of the numbers 2 and 5,


and then stored the result in the variable named ans. It is the
default variable if no variable name is specified.
The output display can be suppressed by adding a
semicolon after the command.
MATLAB allows users to assign variable names with
numbers, letters, and the underscore character. The first Fig. 3 Integrated variables
character should be a letter. The command whos can be typed The variables shown in Figure 3 are built-in ones in
to view the variables in the workspace. MATLAB. However, their values can be changed by the user.

XXX-X-XXXX-XXXX-X/XX/$XX.00 ©20XX IEEE


Though this should be done with caution as changing their can be described using matrices. Calculating circuit
default values may cause confusion in the results. components can also be reduced to multiplying matrices.

Pressing the arrow up (↑) views the previous lines that have
been entered. This is similar to the copy and paste method
where the user does not need to retype the lines.
Multiple commands can be entered on a single line by
separating them with commas.
Anytime MATLAB encounters a percent sign (%), it treats
the rest of the line as a comment. Comments do not affect the
code itself.

Fig. 4 Adding comments

Adding comments next to command lines is a good coding


etiquette. It keeps things easy to understand for others who Fig. 6 Three ways to create a 2x4 matrix
use the code for reference.
Matrix elements are encoded row-by-row, with each row
There is also a range of help commands that assist the user separated by a semicolon or a newline. Elements within a row
in navigating through the software. One way to use the help can either be separated by a comma or space. Square brackets
system is to type in the command window help are used to enclose the whole array.
function_name.
An element in a matrix is indexed according to its row and
MATLAB has a set of built-in elementary math functions. column numbers. The i-j’th element of the matrix A is indexed
These functions include arithmetic operations, exponentials, in MATLAB as A(i, j).
logarithms, constants, trigonometric functions, and solutions of
algebraic equations. To see what is available, type help elfun
in the command window.

Fig. 7 Modifying matrix elements

By typing A(1,4) = 15 , the element located at the 1st row


and 4th column was modified from 4 to 15.
MATLAB also has some functions for creating special
matrices.

Fig. 5 Using help command to view the list of elementary math functions

The command help elfun is one of the many help


commands that can be used by users to view a specific list of
functions. Other examples include help ops, help axis, etc.
B. Matrices
MATLAB is a computing environment that revolves around
the use of matrices, hence the name, which stems from MATrix
LABoratory. Every variable in MATLAB is a matrix. The user
can create custom matrices depending on the situation. There
are numerous applications of matrices, both in mathematics and
other sciences. The behavior of many electronic components Fig. 8 Special matrices
Typing ones(m,n) function gives an mxn matrix composed
of number 1’s. Typing zeros(m,n) function gives an mxn matrix
composed of number 0’s. Typing eye(n) returns an nxn identity
matrix with ones on the main diagonal and zeros elsewhere.
Properly defined matrices can also be operated with each
other using conventional operations like addition (+),
subtraction (-), multiplication (*), division (/), power (^), and
transpose (‘).

Fig. 12 Plot of a sine wave

Typing x = 0:0.1:1, y = sin(2*pi*1*x), plot(x,y) results in


the figure above. The sine wave does not look very smooth
because of the 0.1 increment.

Fig. 9 Matrix addition

Figure 9 shows the addition of matrix A and matrix B.


Matrix addition and subtraction are defined only for matrices
of the same size.
The colon operator is used for creating uniform increment
vectors.

Fig. 10 Listing numbers 1 to 10 using colon operator Fig. 13 Plot of a sine wave with lesser increment

The default increment is 1 when listing a range of numbers Typing x = 0:0.02:1, y = sin(2*pi*1*x), plot(x,y) results in
using the colon operator. the figure above. The sine wave looks smoother because of the
0.02 increment. To obtain a finer plot, use a smaller increment
value.

Fig. 11 Custom increments in the colon operator


If the necessity arises, an increment can be specified using
the syntax start:increment:end.

C. Plots
The function plot(x, y) is used to create plots in MATLAB. Fig. 14 Red-colored plot of a sine wave
Entering the function causes a separate figure window to appear. The color of the plot can also be changed. To obtain a red-
Increments can also be specified when creating plots. Finer plots colored curve, type figure, plot(x,y,'r'). The ‘r’ applies color
can be achieved by decreasing the increment value. red to the plot.
Fig. 15 Customization of the plot of a sine wave Fig. 18 Customization of the plot of a sine wave

Typing figure(1), grid, xlabel('Time, sec'), ylabel('Signal Typing figure, stem(x,y) results in the figure above. In
value'), title('Sine Wave') results in the figure above. The plot plotting discrete-time signals, a 'lollipop' or stem plot is often
looks more detailed with the addition of gridlines, x-axes and y- desired. The command stem(x,y) plots the data sequence y at
axes labels, and title. the values specified in x.
III. RESULTS AND DISCUSSION
A. Getting Started

Fig. 19 Viewing variables in the workspace

The command whos lists in alphabetical order the names,


Fig. 16 Customization of the plot of a sine wave sizes, and types of all variables in the currently active
workspace.
Typing figure(5), plot(x,y,'x') results in the figure above.
The ‘x’ applies x-shaped markers to the plot.

Fig. 20 Accessing command line history using up arrow key

Pressing the up arrow key (↑) allows the user to review past
commands and/or functions. This is helpful if the user needs to
Fig. 17 Customization of the plot of a sine wave alter parts of the code containing an error. It also serves like a
copy and paste function where the user can edit the command
Typing figure, plot(x,y,'g',x,y,'ko') results in the figure without retyping the whole line.
above. The ‘g’ applies color green and ‘ko’ applies o-shaped
markers to the plot.
B. Matrices

Fig. 22 Matrix power vs Element-wise power

F^2 and F.^2 produce different results. The operator ^


gives the “matrix power,” so that it computes F to the F power.
On the other hand, the operator .^ gives the “element-wise
power.” It raises each element of F to the corresponding power
in F.

C. Plots

Fig. 21 Evaluating problems using elementary mathematical functions

MATLAB provides a host of elementary mathematical


functions. It evaluates the elementary functions component-
wise. The figure above shows the evaluation of the following:
o
1) cos 45
2) sin-10.8 (for angles, give the answer in degrees)
3) the angle θ1 for the point (3,2) in the Cartesian plane
4) the angle θ2 for the point (–3,–2) in the Cartesian plane
5) e-2 Fig. 23 Docking and tiling figures
6) log 400 Figures can also be docked above the command window to
7) ln 5 make them more organized. Docking is done by clicking the
8) the magnitude and angle representation for the complex curved arrow below the ‘X’ or Close button of a figure window.
number 3 + j4 Once docked, figures can be tiled by dragging them into specific
areas on the window.
Fig. 24 Customizing the plot of a sine wave Fig.26 Plot of a cosine wave with 2 cycles at 2 Hz

Under the Tools menu, there is Edit Plot. The user can edit The axis function allows the user to display two cycles of
the plot by adding gridlines and changing the curve color, line the cosine wave instead of three. By using the format
style, line width, marker type, and marker size. Under Edit axis([XMIN XMAX YMIN YMAX]), the scaling for the x-
menu, the user can choose to copy the figure and paste it to a and y-axes can be set. Typing axis ([0 1 -1 1]) in the command
Word document. window results in the figure above. The XMAX is changed from
1.5 to 1, creating a two-cycle cosine wave.
IV. CONCLUSION
Familiarizing MATLAB is very essential for aspiring
engineers. As an electrical engineering student, having a solid
foundation in this programming environment is necessary when
dealing with circuit analysis, differential equations, and
advanced engineering mathematics. The tutorial was able to
clearly deliver the topics in a pace similar to the user’s learning
ability. It explained the subtleties in the software’s adopted
syntax and their distinct effects to the output. MATLAB has a
wide range of help commands making it appealing to
programming amateurs. MATLAB encourages creative
Fig.25 Plot of a cosine wave with 3 cycles at 2 Hz thinking in solving various problems with the help of the
numerous tools at its disposal.
Typing x = 0:0.02:1.5, y = cos(2*pi*2*x), figure, plot(x,y),
grid, xlabel(‘Time, sec’), ylabel(‘Signal value’), title(‘Cosine REFERENCES
wave’) in the command window results in the figure above. [1] Mathworks.com. (2019). Getting Started with MATLAB. [online]
𝑦(𝑡) = 𝐴𝑐𝑜𝑠(2𝜋𝑓𝑡 + 𝜙) Available at: https://www.mathworks.com/help/matlab/getting-
started-with-matlab.html [Accessed 5 Sep. 2019].
Eq. 1 Cosine wave equation
[2] Cyclismo.org. (2019). Introduction to Vectors in Matlab — Matlab
Equation 1 shows the most basic form of cosine wave as a Tutorial 3.0 documentation. [online] Available at:
https://www.cyclismo.org/tutorial/matlab/vector.html#basic-
function of time where: A is amplitude, f is frequency, 2πf is operations-on-vectors [Accessed 5 Sep. 2019].
angular frequency, and ϕ is phase angle. In the y equation found
in Figure 25, time x is multiplied by 2 in order to achieve a
frequency of 2 Hz.

You might also like