Você está na página 1de 17

Laboratory Activity 1

MATLAB FUNDAMENTALS

Objective
The activity aims to familiarize students with the basic functionality of MATLAB as a
mathematical tool in solving simple arithmetic problems.
Intended Learning Outcomes (ILOs)
The students shall be able to

1. Identify and manipulate various MATLAB windows

2. Create a variable name

3. Perform fundamental operations on an array

Discussion
MATLAB is an abbreviation for “MATrix LABoratory”. MATLAB is developed and published
by MathWorks founded in 1984, a leading developer and supplier of technical computing software in
the world. Is a versatile piece of computer software with Linear Algebra capabilities as its core.

MATLAB is a numerical computing environment and programming language that allows you
for easy matrix manipulation, plotting of function and data, implementation and development of features
of MATLAB is that it has mathematical functions for linear algebra, statistic, Fourier analysis, filtering,
optimization, numerical integration, and solving ordinary differential equations.

MATLAB can be used for a range of applications, including signal processing and
communications, image and video processing, control systems, test and measurement, computational
finance, and computational biology. Most of its users are engineers and scientists in industry and
academia.

Resources
PC with MATLAB Software
Procedure
Starting MATLAB for Windows
You can start MATLAB by double clicking on the MATLAB icon that should be on the desktop
of your computer. This brings up the Desktop as shown in Figure 1.1 in its default layout of your
MATLAB Environment. Navigate the basic functionality of MATLAB window.
Figure 1-1: The default view of MATLAB

The desktop includes the following:


 Current Folder is used to access your files.
 Command Window is used to enter commands at the command line, indicated by the prompt
(>>).
 Workspace is used to explore data that you create or import from files.

Unlike the previous versions of MATLAB, the environment of the R2018a version it does not
show the Command History -used to view or rerun commands that you entered at the command line
and does not feature the start button. Instead, a ribbon-type menu which displays all available menu
items for a particular category is used.
Each of the windows of the default MATLAB environment is undockable. Just click on the pull
down icon to display the options for the window.
Working with MATLAB windows
1. The command window is located in the center pane of the default view of the MATI.AB screen,
as shown in Figure 1-1. The command window offers an environment similar to a scratch pad.
To type a command, the cursor is placed next to the command prompt (>>), as shown in Figure
1-1.

2. The <Enter> key is pressed once a command is typed, and the command is automatically
executed. However, only the last command is executed. Everything executed previously is
unchanged.

3. Several commands can-be typed in the same line by typing a comma between the commands.
Once the< Enter> key is pressed the commands are executed in order from left to right. When
you type a semicolon (;) at the end of the command the Command window output is suppressed.

4. As you enter commands, MATLAB saves a number of the most recent commands in a stack.
Previous commands saved on the stack can be viewed in the command history window. The
number of commands saved on the stacks varies, depending on the length of the commands and
other factors.

5. You can recall previously typed command pressing up- and down-arrow keys ( ) and ( ).Press
the arrow keys either at an empty command line or after you type the first few characters of a
command. When the command is displayed on the command prompt, it can also be edited if
needed and executed.

6. If a command is too long to fit in one line, it can be continued to the next line by typing three
periods … (or ellipsis) and pressing the <Enter> key. The continuation of the command is then
periods typed in the new line.

7. If MATLAB recognizes an error after you have pressed ENTER to execute a command, then
MATLAB responds with a beep and a message that helps define the error. You can recall the
command line and edit it as you would any computer text.

8. As you type the command you need to specify the input and output variable name. then
MATLAB adds the variable name in the workspace pane if you do not specify a output variable
name MATLAB uses variable ans, short for answer to store your calculation.
Variables names are case-sensitive. Only alphanumeric characters, and the underscore character
are allowed to be used as variable names.

9. The command isvarname determines whether a variable name is valid or not,

10. You can check the contents of the Workspace by using the whos command or by clicking on the
Desktop menu in the Menu bar and checking Workspace from the pull-down menu.

11. You can determine if a variable name is a built-in function by using which command, while
iskeyword command determines if it is a keyword.

12. If you end a statement with a semicolon, MATLAB performs the computation, but suppresses
the display of output in the Command Window.
Understanding the MATLAB Math Syntax
The MATLAB syntax is a set of rules that you use to tell MATLAB what to do. In order to
communicate with MATLAB, you must understand its language, which is essentially a form of math.

Using MATLAB as Calculator


MATLAB is a math-based language, so it pays to review the basic rules for telling MATLAB
how to perform basic math tasks.
a. + or plus() - Adds two numbers.
b. - or minus() - Subtracts two numbers.
c. * or times() - Multiplies two numbers.
d. / or rdivide() -Performs right division.
e. \ or Idivide() - Performs left division, which is also called "goes into"
f. ^ - Exponents
g. eps - Specifies the accuracy of the floating-point precision (epsilon), which defaults to
2.2204e-16.
h. i - Contains an imaginary number, which defaults to 0.0000 + 1.0000i.
i. Inf - Defines a value of infinity, which is any number divided by O, such as 1/0.
j. NaN - Specifies that the numerical result isn't defined (Not a Number).
k. pi - Contains the value of pi, which is 3.1416 when you view it onscreen. Internally, MATLAB
stores the value to 15 decimal places so that you're assured of accuracy.
l. sqrt() -provides the square root of an array
m. parenthesis and bracket () & [] -are the only allowed grouping symbol

HELP SYSTEM
All MATLAB functions have supporting documentation that includes examples and describes
the function Inputs, outputs, and calling syntax.
There are several ways to access this information from the command line:
1. Open the function documentation in a separate window using the doc command.
doc mean
2. Display function hints (the syntax portion of the function documentation) in the
Command Window by pausing after you type the open parentheses for the function input
arguments.
mean()
3. View an abbreviated text version of the function documentation in the Command
Window using the help command,
help mean- Access the complete product documentation by clicking the help icon.

Saving Your Workspace


Workspace variables do not persist after you exit MATLAB. Save your data for later use with
the
save command, save myfile.mat
Saving preserves the workspace In your current working folder in a compressed file with a –mat
extension, called an MAT-file.

Exiting from MATLAB


You can quit MATLAB at any time with one of the following:
 Select Exit MATLAB from the desktop File menu.
 Enter quit or exit at the Command Window prompt.

Note: Do not click on the close box in the top right corner of the MATLAB desktop. This does not allow
MATLAB to terminate properly and, on rare occasions, may cause problems with your computer
operating software
NAME:
SCORE:

SECTION: ACTIVITY NO.:

INSTRUCTOR: DATE PERFORMANCE/SUBMITTED

ASSESSMENT TASKS:

Task 1: Answer the following questions.


1. What is/are the use(s) of the workspace window?

Work space window are the values from the answers in the command window
Are shown.

2. Give at least two ways to change the layout of your windows.

In the layouts tab, there are different ways to change the layout which default and two column
Are some of the examples.

3. Give the different ways to exit MATLAB properly.

To exit the Matlab properly, you can either exit the software from the desktop file menu
Or you can exit it at the command prompt.

4. What is the difference between docking and undocking windows?

The difference between docking and undocking is when it is docked, it has a tab or
Portion in the screen along with command window. Unlike when undocked, it is hidden
From the screen or work area.
5. What special character(s) is/are allowed to be used in assigning variable names?

The characters that are allowed to be used are alphanumeric keys such as letter and numbers
Provided that when labeling letters should go first before the number.

6. Are the variable names a and A the same? Why?

Variables “a” and “A” are not the same. Because it is not the same when the other
Is capitalized. When a value for “a” is entered, the “A” is entered it will not show the
Value that was assigned to “a”.

7. How will you determine if the variable name(s) is/are built-in function(s) in MATLAB?

You can determine if the variable name is/are a built function by using which command.

8. Give at least one way to determine all MATLAB functions supporting documentation that
includes examples and describes the function inputs, outputs, and calling syntax.

You can determine the functions by clicking the question work in upper tab. The drop down
In fix in the command window. You can right click the function and click help. It will show
The description, example and its function. There are also codes or key words to determine certain
Variables.

9. What is the difference between semi-colon syntax and colon syntax?


Colon syntax is used for vector while semi-colon is for suppressing or short form
Of the display of output in the command window.

Task 2: Which of following names are allowed to be a variable name, a keyword or a built in function
in MATLAB? Write V if it Is allowed to be a variable name, K for keyword and B If It Is a built in
function.
1. Test ___V_____

2. If ___K, B___

3. Sine ___K_____

4. linear_matlab ___V___

5. Thisisoneverylongnarnebutisitstjllallowed? _not allowed_


Task 3: Perform the Indicated operation using MATL4B syntax. Write the MATLAB syntax and output
on the space provided.

MATLAB SYNTAX MATLAB OUTPUT


5+3 (5+3)(9-1) 1
a.
9−1
4 (2^3)-(4/(5+3)) 7.5000 or 15/2
b. 23 −
5+3

52+1 3 ((5^(2+1))/(4-1))+((4*2+3)/4) 44.416 or 533/12


c. +2
4−1 4

1 2 ((((2*4)+1)/2)*(((3*5)+2)/3)) 25.5000 or 51/2


d. (4 ) (5 )
2 3

7 ((5+(6*(7/3))- 7.1427e+04
5+6( )−22 (2^2))/((2/3)*((3/(3*6))^(4+(1/2)))))
3
e. 1
2 2 4+2
( )((3)(6))
3

f. (√3 + 4) + 𝜋 2 − (sqrt(3+4))+(pi^2)- 10.0256 or 21906/2185


1
√3 + √2𝜋 + (3 − 4)2
(sqrt((3+(1/2)+sqrt((2*pi)+((3-
2 4)^2)))))
Laboratory Activity 2
MATRICES AND ARRAY

Objective
This activity aims to let the students understand matrices and arrays in MATLAB

Intended Learning Outcomes (ILOs)


The students shall be able to:

1. Generate arrays and matrices

2. Input matrices with the correct display format

3. Perform concatenation of matrices

4. Perform array indexing

Discussion

The MATLAB environment uses the term matrix to indicate a variable containing real or complex
numbers arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher
dimensional grid of numbers. All arrays in MATLAB are rectangular, in the sense that the component
vectors along any dimension are all the same length.

Resources
PC with MATLAB Software
Procedure
Part A. Generating and Deleting a matrix and an array
1. To create an array with several elements in a single row, type the elements and separate each
element with either a comma (J) or a space and enclose it with a square bracket. This type of
array is a row vector

Try and Verify!!!


>>a = [123]
a=
1 2 3

2. To create a matrix that has multiple rows, type the element for each row and separate these rows
with semicolons; then enclose the matrix with square brackets.

Try and Verify!!!

>>a = [1 2 3; 4 5 6; 7 8 9]
a=
1 2 3
4 5 6
7 8 9

3. Matlab has different functions that create different kinds of matrices. One way to create a matrix
is to use functions, such as ones, zeros, randn, and function will generate a matrix with all
elements equal to 1. The zeros on the other hand will generate a matrix with all elements equal
to O. The function rand a matrix with uniformly distributed elements while the randn function
generates normally distributed elements.

To generate a matrix of zeros type zeros. b) where a are the number of rows and b are the number
of columns or type zeros (x) where x is the order of the matrix for square matrices. The same
format is used for the ether commands.

Try and Verify!!!

>> zeros(1,5)
ans =
0 0 0 0 0

>> ones(3)
ans =
1 1 1
1 1 1
1 1 1

>> randn(1,2)
ans =
0.7172 1.6302

>> rand(1,3)
ans =
0.9649 0.1576 0.9706

4. Another function that will generate matrices are pascal and magic. The pascal function generates
symmetric matrices white the magic function will generate non- symmetric matrices. To create
symmetric matrices type pascal(x) where is the order of your matrix. Do the same to create nan-
symmetric matrices but using the magic function These functions generate square matrices only.

Try and Verify!!!

>> pascal(3)
ans =

1 1 1
1 2 3
1 3 6
>>𝑚𝑎𝑔𝑖𝑐(3)
ans =
8 1 6
3 5 7
4 9 2
5. An nxn Identity matrix is denoted by In. MATLAB has a command to generate In with the
syntax eye( ).

Try this and Verify!!!

eye(2) - displays a 2 x 2 identity matrix

eye(5) - displays a 5 x 5 identity matrix

𝐭 = 𝟏𝟎; eye(t) - displays a 10 x 10 identity matrix

eye(sizeA) - displays an identity matrix the same size as A

6. MATLAB has a command to build diagonal matrices when only the diagonal entries are
inputted. The command is diag(), and also works to “extract" a set of diagonal entries.

Try this and Verify!!!

A=
8 1 6

3 5 7

4 9 2

>> b = diag(A)

b=

2
>> 𝑐 = 𝑑𝑖𝑎𝑔(𝑑𝑖𝑎𝑔(𝐴))
c=

8 0 0

0 5 0

0 0 2
7. A matrix name can be reused. In such a case the "old" contents are lost.

8. To determine the matrix names that are in use, use the who command.

9. To delete a matrix, use the clear command, followed by a space and then the matrix name.

10. To determine the number of rows and columns in a matrix, use the size command.

Note: Notice that no brackets are displayed and that MATLAB has assigned this matrix the name ans.
Every matrix in MATLAB must have a name. If you do not assign a matrix name, then MATLAB assigns
it with ans which is the default variable name. Name each matrix with different variable names

If the matrix is quite large, the screen display will scroll too fast for you to see the matrix.

Part B. Display Formats

The format function controls the numeric format of the values displayed. The function affects
only how numbers are displayed, not how MATLAB software computes or saves them.

The different formats are format short, format long, format rat, format bank, format hex. the
default format in MATLAB is short.

Try this and Verify!!!

>>format short
>>𝑥 = [4/3 1.2345e − 6]
x=
1.3333 0.0000

>>format long e
>>𝑥 = [4/3 1.2345𝑒 − 6]
x=
1.333333333333333e+00 1.234500000000000e-06

>>format short e
>>x =[4/3 1.2345e-6)
x=
1.3333e+00 1.2345e-06

>>format rat
x = [4/3 1.2345e-61
x=
4/3 1/810045

>>format bank
x = [4/3 1.2345e-6]
x=
1.33 0.00

>>format hex
x = [4/3 1.2345e-6]
x=
3ff5555555555555 3eb4b6231abfd271

Part C. Concatenation
Concatenation is the process of joining arrays to make larger ones. A pair of square brackets [ ] is the
concatenation operator.
1. To concatenate horizontally use commas.
2. To concatenate vertically use semicolons.

Try and Verify!!!

a=
1 3
4 2

b=
1 1
1 2

>>𝐴 = [𝑎, 𝑏]
𝐴=
1 3 1 1
4 2 1 2

>>𝐵 = [𝑎; 𝑏]
𝐵=
1 3
4 2
1 1
1 2
Part D. Indexing
Every variable in MATLAB is an array that can hold many numbers. When you want to access
selected elements of an array, use indexing.

Try and Verify!!!

𝐴=
8 1 6
3 5 7
4 9 2

There are two ways to refer to a particular element in an array. The most common way is to specify row
and column subscripts
>> 𝐴(3,2)
𝑎𝑛𝑠 =
9
Another way is by the use of Linear Indexing where a single subscript that traverses down each column,
in order, to the desired element is used.
>>> 𝐴(6)
𝑎𝑛𝑠 =
9

To refer to multiple elements of an array, use the colon operator, which allows you to specify a range
of the form start:end.

>> 𝐴(3,2)
𝑎𝑛𝑠 =
6
7

>> 𝐴(2, : )
𝑎𝑛𝑠 =
3 5 7
The colon operator also allows you create an equally spaced vector of values using the more general
form start:step:end

>> 𝐵 = 𝑂: 10: 100


>> 𝐵 =

0 10 20 30 40 50 60 70 80 90 100
NAME:
SCORE:

SECTION: ACTIVITY NO.:

INSTRUCTOR: DATE PERFORMANCE/SUBMITTED

ASSESSMENT TASKS
Do as directed. Write all the syntax/commands and MATLAB output in the table for each activity.
use the rational format of MA TLAB unless stated otherwise.

Task 1: Write the syntax that will create the following matrices.
MATRIX MATLAB SYNTAX
1 1 1 0 0 >>[ones(2,3) zeros(2); eye (3) ones(3,1)
1 1 1 0 0 zeros(3,1)]
1. 1 0 0 1 0
0 1 0 1 0
[0 0 1 1 1]
𝜋 𝜋 𝜋 𝜋 𝜋 >>[pi*ones(1,5); ones (1,5); zeros (1,5); ones
1 1 1 1 1 (1,5)]
2. [ ]
0 0 0 0 0
1 1 1 1 1

5 0 0 >>[5*eye (3)]
3. [0 5 0]
0 0 5

√2 √2 √2 >>[sqrt(2)*ones(3)]
4. [√2 √2 √2]
√2 √2 √2

2 0 0 0 0 >>[2*eye(3) zeros(3,2); ones (1,5)]


0 2 0 0 0
5. [ ]
0 0 2 0 0
1 1 1 1 1

Task 2 : Create a 3x 3 magic matrix A concatenate horizontally with a 3 x 3 pascal matrix B. Write
the syntax and output that will...
1. Extract the element in row 3 column 4
2. Extract the four corner elements
3. Extract column 3
4. Extract the diagonal
5. Create a diagonal matrix using the result in no. 4 as the diagonal.
MATLAB Syntax MATLAB Output
Concatenated Matrix A and B A=8 1 6
>> A= magic (3) 357
492
>> B=pascal (3) B=1 1 1
123
>> C= [A,B] or >> C= [magic (3), pascal(3)] 136
C=8 1 6 1 1
35712
49213
1.
>>C(3,4) Ans=1

2. Ans=8
>>C(1), C(3), C(16), C(18) Ans=4
Ans=1
Ans=6
3.>> C (: , 3) Ans=6
7
2

4. Ans=8
>>diag(C) 5
2

5.>>diag (diag(C)) Ans= 8 0 0


050
002

Task 3:
a) In MATLAB, enter the following matrices

√5 𝑖 2√3 4∗2 2/2


−3
𝐴= 0 1 𝐵 = [ 1/201 5 − 8.2 ]
4
1
4𝑖 −𝑖 ] 0.0001 (9 + 4)/3
[ −3

b) Using the matrrces in (a) and the proper MATLAB commands, display

b.1 𝑎21 , 𝑏32 , 𝑏12 , 𝑎22 simultaneously arranges in a single row.

b.2 row 1 of A, column 2 of A and row 3 of B separately.

c) Concatenate A and B horizontally and vertically if possible.


d) Display matrix B in long format and matrix A in rational format

e) Compare the new format of A and B to the original format, how do they differ?

MATLAB Syntax MATLAB Output


a.>> A= [sqrt (5)] i 2*sqrt(3); (-3/4) 0 1; (1x-3) 4i – i] A=2889/1292+0i 0+1i 1351/390+0i
>> B=[4*2 2/3; 1/201 5-8.2; 0; 0001 ((9+4)/3] -3/4 +0i 0+0i 1+0i
-1/3 +0i 0+4i 0-1i

B= 8 2/3
1/201 -16/5
1/10000 13/3
b.1 Ans= 1 13/3 2/3 0
>>[A(2,3) B(3,2) B (1,2) A(2,2)]

b.2 Ans= 2889/1292+0i 0+1i 1351/390+0i


>>A (1, :), A(:, 2), B(3, i) Ans= 0+1i
0+0i
0+4i
Ans=1/10000 13/3
c.>>C[A,B] 2889/1292+0i 0+1i 1351/390+0i 2/3+0i
-3/4+0i 0+0i 1+0i 1/201+0i -16/5+0i
>>C=[A;B] dimension of array is not consistent -1/3+0i 0+4i 0-1i 1/10000+0i 13/3+0i

d.>>format long 8.000000000000000 0.666666666666667


>>B 0.004975124378109 -3.199999999999999
0.000100000000000 4.333333333333333

e.
>>format rat 2889/1292+0i 0+1i 1351/390+0i
-3/4 +0i 0+0i 1+0i
-1/3 +0i 0+4i 0-1i

>>A

Você também pode gostar