Sei sulla pagina 1di 8

Experiment No.

1
MATLAB® Familiarization
Fe Shannen B. Cinco
Brgy. Canjulao, Lapu-Lapu, 6015 Cebu, Philippines
feshannenc@.gmail.com
+
Department of Electrical and Electronics Engineering, University of San Carlos
Talamban Campus, Nasipit, Talamban, Cebu City, 6000 Philippines

Abstract— This report intents the primary use of a programming The command will be processed by pressing the
platform, MATLAB. It stands for matrix laboratory, a Enter key. To do a simple mathematical calculation,
programming language and a numerical computing application ‘2+5’was typed at the prompt. After hitting the Enter
with access to symbolic computing abilities. Evidently, equipped key, MATLAB will display the result.
by engineers and scientist where they can express problems and
2. A semicolon was used after the command in order to
solutions in a familiar mathematical notation. The experiment
provides a mere introduction unto how the platform function and suppress the display of the output.
the adaptation to its applications to the solution of problems that 3. MATLAB variable names can be composed of letters,
arises in the analysis and design of feedback systems. It aims to numbers and the underscore character. The first
help the use understand through a step-by-step process of character however should be a letter.
learning unto the basic commands, matrices, and plot functions. 4. The command whos was typed so as to view the
Hence, the level of familiarization to the basic functions of this variables in the MATLAB workspace. Also, clear
programming language will serve as the basis of output to this which allows to remove all variables in the
experiment. workspace and clc to clear the screen.
5. MATLAB has built-in variables like pi (π), i and j.
Keywords— mathematical notation, MATLAB, matrices, These built-in variables or keywords (enter
numerical computing, plot functions.
iskeyword in the command window) should not be
I. INTRODUCTION used as variable names in an assignment.
6. The ‘arrow up’ key was pressed repeatedly enabling
The experiment primarily designed to teach the basics of
MATLAB. There are following tutorials which intends to get you up
to browse the command history browser.
and run the software. Several topics are discussed, but the treatment 7. More than one command may be entered on a single
for each topic is merely introductory. The discussion on MATLAB line in the command window if these are separated by
functions for example is limited to what is only necessary for a first commas.
encounter. 8. Comments may be added to a line by preceding the
The objective as stated above also necessitates that discussion comment with ‘%’.
for a certain topic be spread out on the whole material and not just 9. To look at the available operators, help ops was
concentrated in a single section. The intention is to bring out only typed at the command prompt. Alternately, helpwin
what is important as you read and implement the MATLAB in the command window can be entered and
commands discussed in the tutorial. If you want to know more on a matlab\ops in the Help Window that opened was
certain topic, please make use of MATLAB’s Help facility. clicked.
10. To see available set of built-in elementary math
functions, help elfun was typed.
II. METHODOLOGY
The students have undergone a step-by-step learning
process unto the basic commands, matrices, and plot functions. B. Matrices
A. Getting Started MATLAB started as a software designed for
manipulating matrices, hence the name, which comes
So as to get start in running MATLAB, some basic from “MATrix Laboratory”. Every variable in MATLAB
functions and syntaxes were introduced. is a matrix.

1. MATLAB commands are typed next to the prompt.


1. A scalar variable for example is a 1x1 matric which 12. To extract the 3x4 submatrix in the upper left corner
can be verified by typing whos and looking under of the matrix B, “>> B,M = B(1:3,1:4)” was typed at
Size. the prompt.
2. To enter a 2x4 matrix, either of the following was
done.
 >> A = [1,2,3,4;5,6,7,8] C. Plots
 >> A = [1 2 3 4;5 6 7 8] MATLAB plots are done using the function
 >> A = [1 2 3 4 5 6 7 8] plot(x,y). The function arguments are vectors and a figure
Note: that matrix elements are entered row-by-row, window will open plotting y versus x.
with each row separated by a semicolon or a newline.
Elements within a row are separated by a comma or a 1. The increment value was set as “x=0:0.1:1” while
space. The whole array must be enclosed by square the function “y=sin(2*pi*1*x) was typed. In order
brackets. to plot “>> plot(x,y)” was entered at the prompt
2. To obtain a finer plot, a smaller increment value was
3. An element in a matrix is indexed according to its row used such as “>> x = 0:0.02:1”
and column numbers. 3. To view a plot in another figure window and to have
4. To modify a matrix element, “>> A(1,4) = 15” was a red-colored curve “>> figure, plot(x,y,'r')” was
typed at the prompt entered.
5. To test some function for building special matrices, 4. The following were done and the accomplished
the following commands were tried. modifications were observed
 >> ones(2,3)  >> figure(1), grid
 >> zeros(4,4)  >> xlabel('Time, sec')
 >> eye(3)  >> ylabel('Signal value')
6. Large matrices can be constructed using smaller  >> title('Sine Wave')
matrices, such as “>> B = [zeros(2,1) A; eye(3)  >> figure(5), plot(x,y,'x')
ones(3,2)]”  >> figure, plot(x,y,'g',x,y,'ko')
7. The following operations can be performed
between properly defined matrices:
5. In plotting discrete-time signals, a 'lollipop' or stem
Table I plot is often desired by entering,”>>figure,
Mathematical Operations stem(x,y)”. Also, the figures done previously were
Operation Operator docked and it can be done by clicking on the curved
Addition + arrow just below the 'X' or Close button of a figure
- window. Then, the figures were tiled to display a
Subtraction 2x2 array using the Window Arrangement buttons
Multiplication * located on the upper right portion of the Figures
Division / window.

Power ^ D. Exercises: Primarily given to assess the knowledge unto


Transpose ‘ the fundamentals of plotting and its functions in MATLAB.

1. The tools located in the figure window were used to


8. In performing scalar multiplication, a dot in
zoom in (and out) on a small region of the plot.
front of the multiplication operator was placed
2. Figure 2 was clicked to make it active. Under the
9. The colon operator (:) was used for creating
Tools menu the Edit plot was clicked. The white
evenly incremented vectors. A vector is a
space of the plot was right clicked and Grid was
matrix with only one column, or a matrix with
choosen. In order for the red curve to be selected it
only one row.
was clicked, then right clicked to display editing
10. The default increment is 1. If desired, an increment
options. The following color of the curve was
can be specified using the following syntax
changed to purple, the line width to 2.0, line style to
start:increment:end
‘dot’, and put size to 5 square markers. The curve
11. Also, colon operator was used in extracting
was deselected when clicked outside.
submatrices from a larger matrix. To extract
3. A blank document was opened in Microsoft Word.
the second row from matrix A and call the
Then, under the Edit menu of Figure 2, Copy
extracted vector as L, “>> A,L = A(2,1:4)”
Figure was clicked.. The plot of Figure 2 was pasted
was typed at the prompt.
to the Word Document.
4. Going back to the MATLAB command window. 4. Look at the Workspace browser. (a) What does it
Three cycles of a cosine wave with a frequency of 2 contain? (b) What information is given? Double click
Hz was plotted. To display only two cycles the axis on an item in the Workspace browser. What happens?
function was used. The Workspace Browser enabled the contents
of the workspace in MATLAB to be viewed and
interactively managed. It also displayed statistics for
each variable or object in the workspace. Whenever an
III. RESULTS AND DISCUSSIONS item in the Workspace browser was double-clicked it
showed the variable in the workspace.
Through the aid of experiment laboratory guide, it allows
the students to acquire the desired outputs by the given guided 5. What do you think is the output for the line given below?
questions and exercises. Similarly, the results and discussion >>20-3^2*5+6/2
are divided into four parts same as the methodology. For the line “20–3^2*5+6/2” resulted to an answer of -22

A. Getting Started
1. What information does the command whos give?
The command whos contained the variable’s
information such as name, size, bytes, class and its
attributes.
Fig. 4 Answer for the line “20-3^2*5+6/2”

6. What is the difference between the sin and sind functions?


The help information usually includes a See also
portion in case you need to explore related functions. For
Fig.1 Variable’s information in the workspace
example, it is used to compare sin and sind functions. The
difference was sin was used for angles that are in radians
2. What is the value of eps?
while sind was used for angles in degrees.
The value of eps is 2.2204 x 10-16
7. How do you obtain the cube root of 216?
The cube root of 216 was obtained through
evaluation. The cube root of 216 is 6.

Fig.2 Value of eps

3. Look at the Command History browser. What does it


contain? Double click on a line in the Command Fig.5 Cube root of 216
History. What happens?
Pressing the ‘arrow up’ key repeatedly opened The cube root of 216 was also obtained using
the Command History Browser the nthroot function that requires multiple arguments.

Fig. 3 Command History Browser

The Command History browser contained Fig. 6 Cube root of 216 using the nthroot function
line of statements that were ran in the current and
previous sessions in MATLAB. Double Clicking a 8. What happens if you enter only a single argument in
line in the Command History selected previous using the function nthroot?
statements and rerun it. Command History window An error occurred when entering only a single
displays a log of statements that you ran in the current argument.
and previous MATLAB sessions.
4. The angle θ2 for the point (–3,–2) in the Cartesian
plane

Fig. 7 Using nthroot with only a single argument

9. What is fourth root of -81?


Using nthroot function a negative number like
-81 resulted to an error. Fig. 13 The angle for the point (-3, -2) in the Cartesian plane is -
146.3099 degrees

5. e–2

Fig. 9 Nthroot function with a negative number

Evaluate the following using the appropriate elementary


math functions:
Fig. 14 Euler’s number raise to the power of negative 2
1. cos 45o
6. log 400

Fig. 10 The answer of cos(45) is 0.5253 rads

–1 Fig. 15 Common logarithm of 400 is 2.6021


2. sin 0.8 (for angles, give the answer in degrees)
7. ln 5

Fig. 11 The value of inverse sin of 0.8 Fig. 16 Natural logarithm of 5 is 1.6094

8. The magnitude and angle representation for the


3. The angle θ1 for the point (3,2) in the Cartesian complex number 3 + j4
plane

Fig. 12 The angle for the point (3,2) in the Cartesian plane is 56.3099 degrees
2. Matrix multiplication is commutative

Fig. 17 The complex number’s magnitude and angle Fig.20 Commutative matrix multiplication
representation
3. If a matrix is square, it can be
multiplied to itself by using the
9. The real and imaginary components of 8∠150° ^ operator.

Fig. 21 Multiplication using ^


operator

4. Are F^2 and F.^2 the same? Why or Why not?

Fig. 18 Complex number equivalent of 8∠150°

B. Matrix:

Fig. 22 F^2 and F.^2 comparison


1. Did A+C give the correct element-by-element
addition?

5. Are J and K the same?

Fig.19 Correct element by element addition


Fig.23 J and K comparison

C. Plots

MATLAB plots are done using the function plot(x,y). Fig. 25 Sample plot a finer sine wave

1. Below are the commands use to plot a sine wave 3. To view a plot in another figure window and to have
>> x = 0:0.1:1 a red-colored curve
>> y = sin(2*pi*1*x)
>> plot(x,y) >> figure, plot(x,y,’r’)

Fig.26 Red sine wave

Fig. 24 Sample plot of a Sine wave

2. To obtain a finer plot, use a smaller increment value


>> x = 0:0.02
>> y = sin(2*pi*1*x)
>> plot(x,y)
4. Each of the following were done and the
modifications were observed.

Fig. 27 First Modification of the sine wave

Fig.29 Sine wave with circles as a point

5. In plotting discrete-time signals, a ‘lollipop’ or stem


plot is often desired
>> figure, stem(x,y)

Fig. 28 Sine wave with x-marks as a point

Fig. 30 Stem plot of the sine wave


6. Docking figures 1,3,4, and 5

Fig. 33 Cosine wave (2Hz, 3 cycles)

Fig. 31 Docked and tiled plots

IV. CONCLUSION

D. Exercise This experiment provides an outcome that introduces unto


the basic commands, matrices, and plot functions of MATLAB.
The exercise was done to test the depth of understanding The tools provided by the software have made it easier to
towards plotting functions in MATLAB. perform the exercises and answering the guided questions. The
outputs can be instantly obtain by entering the appropriate
Below is the edited plot of Figure 2 which comprises the commands. Then, the results would then be transferred,
Grid, curve to purple, line width to 2.0, line style to dot, and a particularly the figures, into a word document. The screenshots
size 5 square markers. were done to show how the code looked like in MATLAB.
Summing up, MATLAB provides a variety of solutions
upon solving any engineering problems and is time-efficient.
This programming platform may look like difficult to use due
to the complexity of problems needed to solve, on the contrary,
it is not that difficult to understand as long as the user learned
the basics of software.

REFERENCES
[1] (2001). Enterprise Information Technology Services. Retrieved from
https://eits.uga.edu/hardware_and_software/software/mathworks_matla
b/
[2] MathWorks. (2019). MATLAB. Retrieved from
https://www.mathworks.com/products/matlab.html
[3] Rabuya, I. (2016, November 15). MATLAB FAMILIARIZATION.

Fig.32 Edited sine wave

Three cycles of a cosine wave with a frequency of 2 Hz


was plotted. To display only two cycles the axis function was
used.

Potrebbero piacerti anche