Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
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.
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.
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.
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:
ASSESSMENT TASKS:
Work space window are the values from the answers in the command window
Are shown.
In the layouts tab, there are different ways to change the layout which default and two column
Are some of the examples.
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.
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.
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.
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___
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
Objective
This activity aims to let the students understand matrices and arrays in MATLAB
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
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.
>>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.
>> 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.
>> 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( ).
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.
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.
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.
>>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.
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.
𝐴=
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
0 10 20 30 40 50 60 70 80 90 100
NAME:
SCORE:
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
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
Task 3:
a) In MATLAB, enter the following matrices
b) Using the matrrces in (a) and the proper MATLAB commands, display
e) Compare the new format of A and B to the original format, how do they differ?
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)]
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