Professional Documents
Culture Documents
Lecturer:
Mr. Mohammed A. Sulaiman
2021-2022
CHAPTER ONE
AN INTRODUCTION
MATLAB® is a very popular language for technical computing used by students,
engineers, and scientists in universities, research institutes, and industries all over the
world. The software is popular because it is powerful and easy to use.
MATLAB is a powerful language for technical computing. The name MATLAB stands for
MATrix LABoratory. It is a numerical computing application proposing both a
programming language and its development environment. It is developed since 1984 by
Mathworks. It is composed of a basic set of functions which can be extended by
toolboxes (additional sets of functions). Typically, a toolbox is dedicated to a specific
field of application such as mechanics, mathematics, biology, etc. As the language is
based on matrix manipulation, it is particularly used for linear algebra and numerical
analysis.
MATLAB can be used for math computations, modeling and simulations, data analysis
and processing, visualization and graphics, and algorithm development. MATLAB is the
tool of choice for high-productivity research, development, and analysis.
1.Programming
2.Simulink
It is assumed that the software is installed on the computer, and that the user can
start the program. Once the program starts, the MATLAB desktop window opens.
The window contains four smaller windows: the Command Window, the Current
Folder Window, the Workspace Window, and the Command History Window. This is
the default view that shows four of the various windows of MATLAB. A list of several
windows and their purpose is given in Table 1-1. The Start button on the lower left
side can be used to access MATLAB tools and features.
Workspace Window Provides information about the variables that are used.
Commands are entered at the prompt which looks like two successive “greater than”
signs:
>>
The Command Window is MATLAB’s main window and opens when MATLAB is started. It
is convenient to have the Command Window as the only visible window, and this can be
done by either closing all the other windows (click on the x at the top right-hand side of
the window you want to close) or by first selecting the Desktop Layout in the Desktop
menu, and then selecting Command Window Only from the submenu that opens.
a) Type 2+3 after the >> prompt, followed by Enter, i.e. press the Enter key, as
indicated by <Enter>, below:
>> 2+3 <Enter>
Commands are only carried out when you press Enter. The answer in this case is, 5.
If you insist on using ∞ in a calculation, which you may legitimately wish to do, type
the symbol Inf (short for infinity).
e.g. try
13+Inf and
29/Inf
d) Another special value that you may meet is NaN, which stands for Not-a-Number. It is
the answer to calculations like 0/0.
When a command is typed in the Command Window and the Enter key is pressed, the
command is executed. Any output that the command generates is displayed in the
Command Window. If a semicolon ( ; ) is typed at the end of a command the output of the
command is not displayed. Typing a semicolon is useful when the result is obvious or
known, or when the output is very large.
If several commands are typed in the same line, the output from any of the commands
will not be displayed if a semicolon is typed between the commands instead of a comma.
Assign any values you like to two variables x and y. Now see if you can in a single
statement assign the sum of x and y to a third variable z. One way of doing this is
>> x = 2; y = 3; <Enter>
>> z = x + y <Enter>
There are special samples used with MATLAB for algebraic processes as shown in table.
Exponent ab a^b
1.234*105 = 1.234e5
180/π = 180/pi
2(1+2)/3 = 2^(1+2)/3
1/ 2𝜋 = 1/sqrt(2*pi)
>> sqrt(64)
ans =
8.00
>> sqrt(50+14*3)
ans =
9.59
>> sqrt(54+9*sqrt(100))
ans =
12.00
>> (15+600/4)/sqrt(121)
ans =
15.00
There are 20 words, called keywords, that are reserved by MATLAB for various purposes
and cannot be used as variable names. These words are:
Row vector: To create a row vector type the elements with a space or a comma between
the elements inside the square brackets (parentheses).
• if a semicolon where write at the end of MATLAB command, the program will enter the
values of the vector without viewing the vector.
• In a vector with constant spacing the difference between the elements is the same. use
the following form:
Name of the vector = [value of first element : amount of increase : value of last element]
A particular element in a vector is accessed using the name of the vector variable and the
element number (or index, or subscript) in parentheses
Vector Displaying:
A) To display all the content of a vector, writing the vector name and then click enter
button.
1) Length: This command is used to define the number of elements in the vector.
2) Max: This command is used to display the maximum value of the vector elements.
1) Addition (+):
Example >> x=[1 2 3 4];
>> y=[5 6 7 8];
>> s=x+y
s=
6 8 10 12
2) Subtraction (-):
2) Who: Displays a list of the variables currently in the memory (To display the
variables name used in work space).
Example >> who
Your variables are: The who command displays the
variables currently in the
f r x
workspace
>>|
Mechanical & Energy Eng. Dep. 46
3) Clc : The clc command (type clc and press Enter) clears the Command Window.
After working in the Command Window for a while, the display may become very
long. Once the clc command is executed a clear window is displayed. The command
does not change anything that was done before. For example, if some variables
were defined previously , they still exist and can be used. The up-arrow key can also
be used to recall commands that were typed before.
4) Clear: Removes all variables from the memory. [Used to delete the values of the
variables in the work space (cleaning work space)].
Command Outcome
>> kt(6)=[ ]
kt =
2 8 40 65 3 23 15 75 80
>> kt(3:6)=[ ]
kt =
2 8 23 15 75 80
Example If the volume of a cylinder of height h and radius r is given by V = πr2h, use
MATLAB to find the volume enclosed by a cylinder that is 12 cm high with a
diameter of 8 cm.
Example Create a MATLAB m file to display the results of sin(π/4), sin(π/3), sin(π/2) as
rational numbers.
Problems:
1- x = [0 : 0.1 : 1]
2- u = [100:–5:80]
3- a = [ 1 3 7 ];
a = [ a 0 -1]
4- t=[0:-2:-15]
Mechanical & Energy Eng. Dep. 50
MATRIXES
A two-dimensional array, also called a matrix, has numbers in rows and columns.
Matrices can be used to store information like the arrangement in a table. Matrices play
an important role in linear algebra and are used in science and engineering to describe
many physical quantities
The method of defining the matrix in MATLAB is nearly similar to that of vector,
because the matrix consists of a number of vectors. A matrix is a two-dimensional array
of numbers. In MATLAB a matrix can be defined by entering the elements of each row
with leaving a space between each element and semicolon at the end of each row,
1) Vector
Discussed in detail at Page No. 38
2) Matrix
The address of an element in a matrix is its position, defined by the row number and
the column number where it is located. For a matrix assigned to a variable ma, ma(k,p)
refers to the element in row k and column p.
In MATLAB there are some ready matrixes which make the work of programmer easier. Some
of these matrices are;
1. Zeros:
This commend use to generate a matrix with all its elements equal to zero. The general form
of this commend is,
zeros (m , n)
If only one dimension is given, then a square zero matrixes will generate.
ones (m , n)
If only one dimension is given, then square one matrixes will generate.
eye (m , n)
Magic (m)
Pascal (m)
(b – a)*rand(m,n) + a
8. Randn:
This command use to make a random matrix with its elements positive and negative values.
The general form of this command is,
A) Merging Vectors:
2.
>> a
a=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
For example, for the above matrix, to displaying the second row, writing
and to display the second and third element from the fourth row, writing
For example, for the above matrix (a), to displaying the first column, writing
and to display the third and fourth element from the second column, writing
For example to display the diagonal of the above matrix (a), writing
For the matrix (a) above, to display the reverse diagonal, writing
For example, to display the element which locate at 2nd row and 4th column, writing
a= b=
1 4 7 7 8 9
2 5 8 4 5 6
3 6 9 1 2 3
1) Addition ( + ):
The operations + (addition) and – (subtraction) can be used with arrays of identical size
(the same number of rows and columns ).The sum ,or the difference of two arrays is
obtained by adding .or subtracting, their corresponding elements.
In general ,if A and B are two arrays (for example 2x3 matrices)
then, the matrix that is obtained by the operation A*B has the dimension of 4x2 with
the elements:
also, it can raise the elements of a matrix to a power (the power may be constant or
matrix) as written below,
The determine of a matrix is a single value. To find the determine of a matrix the
general form is
4) Inverse:
The inverse matrix is a same matrix raised to power (-1). The matrix will have an inverse
if the matrix is a square matrix and its determine not equal to zero. The general form of
this command is,
The linear equation can be analyzed quickly and easily by using MATLAB program and
give exact results. There are three methods to solve the linear equations, which are:
1. Gramer's Rule
2. Using the method A.x=b
3. Using Reduced Row Echelon Form (rref)
The Gramer's rule can be used to solve linear equations by translating it to a matrix.
For example if there are two equations with two variables (X and Y) as shown below,
a11X + a12Y = b1 ------ (1)
a21X + a22Y = b2 ------ (2)
Where (a11, a12) and (a21, a22) are equation's constants and (b1 and b2) are constants
equal to equations (1) and (2), respectively.
Mathematical the above equations can be solved as follows