You are on page 1of 101

MATLAB

Programming & Simulink

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.

Mechanical & Energy Eng. Dep. 1


MATLAB program divided into two basic branches

1.Programming
2.Simulink

1. Programming describes the overall process of developing programs in MATLAB. It


describes the various types of functions you can create, how to make calls to
functions, handle argument data, and use function handles.

2. Simulink is software for modeling, simulating, and analyzing dynamic systems. It


supports linear and nonlinear systems, modeled in continuous time, sampled time,
or a hybrid of the two. Systems can also be multirate, i.e., have different parts that
are sampled or updated at different rates.

Mechanical & Energy Eng. Dep. 2


MATLAB WINDOWSES

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.

Mechanical & Energy Eng. Dep. 3


Mechanical & Energy Eng. Dep. 4
Window Purpose

Command Window Main window, enters variables, runs

Workspace Window Provides information about the variables that are used.

Command History Window Logs commands entered in the Command Window.

Current Folder Window Shows the files in the current folder.

Figure Window Contains output from graphic commands

Editor Window Creates and debugs script and function files.

Help Window Provides help information.

Mechanical & Energy Eng. Dep. 5


Command Window:
The Command Window is MATLAB’s main window and can be used for executing
commands, opening other windows, running programs written by the user, and managing
the software.

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.

Mechanical & Energy Eng. Dep. 6


Mechanical & Energy Eng. Dep. 7
The Command History Window:
The Command History Window lists the commands that have been entered in the
Command Window. This includes commands from previous sessions. A command in the
Command History Window can be used again in the Command Window. By double-clicking
on the command, the command is reentered in the Command Window and executed. It is
also possible to drag the command to the Command Window, make changes if needed, and
then execute it. The list in the Command History Window can be cleared by selecting the
lines to be deleted and then selecting Delete Selection from the Edit menu (or right-click the
mouse when the lines are selected and then choose Delete Selection in the menu that
opens).

Mechanical & Energy Eng. Dep. 8


Workspace:
The MATLAB workspace consists of the set of variables (named arrays) that are defined
and stored during a MATLAB session. It includes variables that have been defined in the
Command Window and variables defined when script files are executed. This means that
the Command Window and script files share the same memory zone within the computer.
This implies that once a variable is in the workspace, it is recognized and can be used, and
it can be reassigned new values, in both the Command Window and script files.

Mechanical & Energy Eng. Dep. 9


Current Directory (Current folder):
Current Directory is shown in the “Current Directory” field in the desktop toolbar of the
Command Window, as shown in Figure below. If an attempt is made to execute a script
file by clicking on the Run icon (in the Editor Window) when the current folder (Current
Directory) is not the folder where the script file is saved.

Mechanical & Energy Eng. Dep. 10


Figure Window:
The Figure Window opens automatically when graphics commands are executed, and
contains graphs created by these commands. An example of a Figure Window is shown in
Figure below

Mechanical & Energy Eng. Dep. 11


Editor Window:
The Editor Window is used for writing and editing programs. This window is opened from
the File menu. An example of an Editor Window is shown in Figure below.

Mechanical & Energy Eng. Dep. 12


Help Window:
The Help Window contains help information. This window can be opened from the Help
menu in the toolbar of any MATLAB window. (F1)

Mechanical & Energy Eng. Dep. 13


CHAPTER TWO
WORKING IN THE COMMAND WINDOW
NOTES FOR WORKING IN THE COMMAND WINDOW:
• To type a command the cursor must be placed next to the command prompt ( >> ).
• Once a command is typed and the Enter key is pressed, the command is executed.
However, only the last command is executed. Everything executed previously (that might
be still displayed) is unchanged.
• Several commands can be typed in the same line. This is done by typing a comma
between the commands. When the Enter key is pressed the commands are executed in
order from left to right.
• It is not possible to go back to a previous line that is displayed in the Command Window,
make a correction, and then re-execute the command.
• A previously typed command can be recalled to the command prompt with the up
arrow key ( ↑ ). When the command is displayed at the command prompt, it can be
modified if needed and then executed. The down-arrow key ( ↓ ) can be used to move
down the list of previously typed commands.
• If a command is too long to fit in one line, it can be continued to the next line by typing
three periods … (called an ellipsis) and pressing the Enter key. The continuation of the
command is then typed in the new line. The command can continue line after line up to a
total of 4,096 characters.

Mechanical & Energy Eng. Dep. 14


➢ Since we have experience doing arithmetic, we want to examine if MATLAB does
this correctly. When applying computer tools this is a required step to gain
confidence in the tool and in our ability to use the tool.

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.

b) Next try the following:


>> 3-2 <Enter>
>> 2*3 <Enter>
>> 1/2 <Enter>
>> 2ˆ3 <Enter>
>> 2\1 <Enter>

Mechanical & Energy Eng. Dep. 15


c) How do you think MATLAB would handle 0/1 and 1/0? Try it. MATLAB is sensible
about anticipating some errors; it warns you in case you didn’t realize you were
dividing by zero, but still gives the answer Inf.

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.

Mechanical & Energy Eng. Dep. 16


The Semicolon ( ; )

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>

Mechanical & Energy Eng. Dep. 17


ALGEBRAIC PROCESS WITH MATLAB

There are special samples used with MATLAB for algebraic processes as shown in table.

Operation Algebraic Form MATLAB Form

Addition a+b a+b

Subtraction a-b a-b

Multiplication a×b a*b

Right division a/b a/b

Left division b/a a\b

Exponent ab a^b

Square root Sqrt(a)

Power 106 1e6

Mechanical & Energy Eng. Dep. 18


USING MATLAB AS A CALCULATOR
The simplest way to use MATLAB is as a calculator. This is done in the Command Window
by typing a mathematical expression and pressing the Enter key.

Mechanical & Energy Eng. Dep. 19


EXAMPLE:
Translate the algebraic form to the MATLAB form:

1.234*105 = 1.234e5
180/π = 180/pi
2(1+2)/3 = 2^(1+2)/3
1/ 2𝜋 = 1/sqrt(2*pi)

Mechanical & Energy Eng. Dep. 20


EXAMPLE:

>> 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

Mechanical & Energy Eng. Dep. 21


DISPLAY FORMATS:
The user can control the format in which MATLAB displays output on the screen. The
output format is fixed-point with four decimal digits (called short), which is the default
format for numerical values.
The format can be changed with the format command. Several of the available formats
are listed and described in Table below

Command Description Example

Fixed-point with 4 decimal >> 290/7


format short digits for: ans =
Otherwise display format short e. 41.4286

Fixed-point with 15 decimal >> 290/7


format long digits for: ans =
Otherwise display format long e. 41.428571428571431

Mechanical & Energy Eng. Dep. 22


Command Description Example
>> 290/7
Scientific notation with 4
format short e ans =
decimal digits.
4.1429e+001
>> 290/7
Scientific notation with 15
format long e ans =
decimal digits.
4.142857142857143e+001
>> 290/7
Best of 5-digit fixed or
format short g ans =
floating point.
41.429
>> 290/7
Best of 15-digit fixed or
format long g ans =
floating point.
41.4285714285714
>> 290/7
format bank Two decimal digits. ans =
41.43
>> format rat
>> 5.33
format rat rational expression
ans =
533/100

Mechanical & Energy Eng. Dep. 23


Mechanical & Energy Eng. Dep. 24
ELEMENTARY MATH FUNCTIONS

Mechanical & Energy Eng. Dep. 25


The inverse trigonometric functions are asin(x), acos(x), atan(x), acot(x) for the angle in
radians; and asind(x), acosd(x), atand(x), acotd(x) for the angle in degrees

Mechanical & Energy Eng. Dep. 26


RULES ABOUT VARIABLE NAMES:

A variable can be named according to the following rules:

• Must begin with a letter.


• Can be up to 63 characters long.
• Can contain letters, digits, and the underscore character.
• MATLAB is case sensitive: it distinguishes between uppercase and lowercase letters.
For example, AA, Aa, aA, and aa are the names of four different variables.
• No spaces are allowed between characters (use the underscore where a space is
desired).

There are 20 words, called keywords, that are reserved by MATLAB for various purposes
and cannot be used as variable names. These words are:

break case catch classdef continue else elseif


end for function global if otherwise parfor
persistent return spmd switch try while

Mechanical & Energy Eng. Dep. 27


Mechanical & Energy Eng. Dep. 28
ONE-DIMENSIONAL ARRAY (VECTOR)

A one-dimensional array is a list of numbers arranged in a row or a column. A vector can


be either a row vector or a column vector. Vectors can have any number of elements.
The vector is created by typing the elements (numbers) inside square brackets [ ].

variable_name = [type vector elements]

Row vector: To create a row vector type the elements with a space or a comma between
the elements inside the square brackets (parentheses).

Mechanical & Energy Eng. Dep. 29


Or using commas

Mechanical & Energy Eng. Dep. 30


Column vector: To create a column vector type the left (parentheses) square bracket [ and
then enter the elements with a semicolon between them, or press the Enter key after each
element. Type the right square bracket ] after the last element.

Or other way to create vertical vector

Mechanical & Energy Eng. Dep. 31


Note:

• 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]

Mechanical & Energy Eng. Dep. 32


Mechanical & Energy Eng. Dep. 33
Mechanical & Energy Eng. Dep. 34
• The set of x values can be used to create a list of points representing the values of
some given function. For example, suppose that y = ex. Then we have:

Mechanical & Energy Eng. Dep. 35


LINSPACE FUNCTION
linspace function creates a linearly spaced vector; linspace(xi,xf,n) creates a vector with n
values in the inclusive range from xi to xf.

Mechanical & Energy Eng. Dep. 36


For example, the following creates a vector with five values linearly spaced between 3 and
15, including the 3 and 15:

Mechanical & Energy Eng. Dep. 37


REFERRING TO AND MODIFYING ELEMENTS

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.

Mechanical & Energy Eng. Dep. 38


B) To display the value of an element in a vector following this form;
a(sequence of the element)

Mechanical & Energy Eng. Dep. 39


C) To display regular values of a vector with equal increase, following this form;
Vector name (sequence of the start element : amount of increase : sequence of the
end element)

Mechanical & Energy Eng. Dep. 40


Mechanical & Energy Eng. Dep. 41
SOME FUNCTIONS USED WITH VECTORS

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.

Mechanical & Energy Eng. Dep. 42


3) Min: This command is used to display the minimum value of the vector elements.

4) Sort: To arrange the elements of the vector ascending.

5) Sum: This command use to summing the vector elements.

Mechanical & Energy Eng. Dep. 43


ALGEBRAIC PROCESS ON VECTOR

1) Addition (+):
Example >> x=[1 2 3 4];
>> y=[5 6 7 8];
>> s=x+y
s=
6 8 10 12

2) Subtraction (-):

Example >> w=[10 9 8 7];


>> z=[1 2 3 4];
>> p=w-z
p=
9 7 5 3

Mechanical & Energy Eng. Dep. 44


3)1) Multiply (*): This command is used to multiplying the vector by a constant.
Example >> b=[4 5 6 1];
>> h=b*pi
h=
12.5664 15.7080 18.8496 3.1416

4)2) Dividing ( / ) or ( \ ): This command is used to dividing the vector by a constant.


Example >> x=[4 7 8 9 12];
>> f=x/2
f=
2.0000 3.5000 4.0000 4.5000 6.0000
>> f=2\x
f= 2.0000 3.5000 4.0000 4.5000 6.0000

Mechanical & Energy Eng. Dep. 45


SOME COMMANDS
1) Whos: Displays a list of the variables currently in the memory and their sizes
together with information about their bytes and class.( to display all the variables
used in work space in details)
Example >> whos
Name Size Bytes Class
The whos command displays the
f 1x5 40 double
variables currently in the
r 5x1 40 double workspace, and information
about their size and other
x 1x5 40 double information.
Grand total is 15 elements using 120 bytes

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

clear Removes all variables from the memory.

clear x y z Removes only variables x, y, and z from the memory.

Mechanical & Energy Eng. Dep. 47


DELETING ELEMENTS

An element or a range of elements, of an existing variable can be deleted by reassigning


nothing to these elements. This is done by using square brackets with nothing typed in
between them. By deleting elements a vector can be made shorter and a matrix can be
made to have a smaller size

Vector name (sequence of the element to be deleted) = [ ]

Example >> kt = [2 8 40 65 3 55 23 15 75 80]


kt =
2 8 40 65 3 55 23 15 75 80

>> kt(6)=[ ]
kt =
2 8 40 65 3 23 15 75 80

Mechanical & Energy Eng. Dep. 48


Example >> kt = [2 8 40 65 3 55 23 15 75 80]
kt =
2 8 40 65 3 55 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.

Solution >> V=pi*(r^2)*h


V=
603.1858

Mechanical & Energy Eng. Dep. 49


Example Use MATLAB to compute the sin of π/3 expressed as a rational number.

Solution >> format rat


>> sin(pi/3)
ans =
1170/1351

Example Create a MATLAB m file to display the results of sin(π/4), sin(π/3), sin(π/2) as
rational numbers.

Solution % File to calculate sin of numbers


x = [pi/4,pi/3,pi/2];
format rat
y = sin(x)

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,

Variable_name=[1st row elements; 2nd row elements; 3rd row elements;


... ; last row elements]

Mechanical & Energy Eng. Dep. 51


For example, consider

Mechanical & Energy Eng. Dep. 52


ARRAY ADDRESSING (DISPLAYING):

Elements in an array (either vector or matrix) can be addressed individually or in subgroups.

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.

Mechanical & Energy Eng. Dep. 53


Mechanical & Energy Eng. Dep. 54
For a matrix:
A(:,n) Refers to the elements in all the rows of column n of the matrix A.
A(n,:) Refers to the elements in all the columns of row n of the matrix A.
A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A.
A(m:n,:) Refers to the elements in all the columns between rows m and n of the matrix A.
A(m:n,p:q) Refers to the elements in rows m through n and columns p through q of the
matrix A.

Mechanical & Energy Eng. Dep. 55


Mechanical & Energy Eng. Dep. 56
Mechanical & Energy Eng. Dep. 57
MATRIXES GENERATION

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.

Mechanical & Energy Eng. Dep. 58


2. Ones:
This commend use to generate a matrix with all its elements equal to one. The general
form of this commend is,

ones (m , n)

If only one dimension is given, then square one matrixes will generate.

Mechanical & Energy Eng. Dep. 59


3. Eye:
This command use to make a matrix with its diagonal equal to (one) and other elements
equal to (zero). The general form of this command is,

eye (m , n)

Mechanical & Energy Eng. Dep. 60


Mechanical & Energy Eng. Dep. 61
4. Magic:
This command used to make a magic matrix. The matrix is called magic matrix if the sum
of its x column equal to the sum of its x row. The general form of this command is,

Magic (m)

Mechanical & Energy Eng. Dep. 62


5. Pascal:
This command used to make a symmetric matrix. . The matrix is called pascal matrix if
the elements of x column are the same as the elements of the x row. The general form
of this command is,

Pascal (m)

Mechanical & Energy Eng. Dep. 63


6. rand
The rand command generates uniformly distributed random numbers with values
between 0 and 1. The command can be used to assign these numbers to a scalar, a
vector, or a matrix, as shown in Table below
Command Description Example
rand Generates a single random real number >> rand
between 0 and 1. ans =
0.2311
rand(1,n) Generates an n-element row vector of >> a=rand(1,4)
random real numbers between 0 and 1. a=
0.6068 0.4860 0.8913 0.7621

rand(n) Generates an n × n matrix with random >> b=rand(3)


real numbers between 0 and 1. b=
0.4565 0.4447 0.9218
0.0185 0.6154 0.7382
0.8214 0.7919 0.1763
rand(m,n) Generates an m × n matrix with random >> c=rand(2,4)
real numbers between 0 and 1. c=
0.4057 0.9169 0.8936 0.3529
0.9355 0.4103 0.0579 0.8132
randperm(n) Generates a row vector with n elements >> randperm(8)
that are random permutation of integers ans =
1 through n. 8 2 7 4 3 6 5 1
Mechanical & Energy Eng. Dep. 64
• Random numbers that are distributed in a range (a,b)[ To generate a random real number
in the range from low to high if a=low number and b=high number]

(b – a)*rand(m,n) + a

Mechanical & Energy Eng. Dep. 65


7. Random integer
A- randi
The randi command generates uniformly distributed random integer. The command
can be used to assign these numbers to a scalar, a vector, or a matrix, as shown in
Table

Command Description Example


randi(imax) (imax is Generates a single random >> a=randi(15)
an integer) number between 1 and imax. a=
9

randi(imax, n) Generates an n × n matrix with >> b=randi(15,3)


random integers between 1 and b =
imax. 4 8 11
14 3 8
1 15 8

randi(imax, m,n) Generates an m × n matrix with >> c=randi(15,2,4)


random integers between 1 and c=
imax.. 1 1 8 13
11 2 2 13

Mechanical & Energy Eng. Dep. 66


B- randint

randint ( No.row , No column, range of random integer)

Mechanical & Energy Eng. Dep. 67


randint ( No.row , No column , [ first range , end range ] )

8. Randn:
This command use to make a random matrix with its elements positive and negative values.
The general form of this command is,

randn (dim1 , dim2)

Mechanical & Energy Eng. Dep. 68


Exercise:

Mechanical & Energy Eng. Dep. 69


Friction experiment (element-by-element calculations)

Mechanical & Energy Eng. Dep. 70


MATRIX SIZE ENLARGING
Matrix size will be enlarged by merging vectors or merging matrixes or merging vector with
matrix with taking in consideration the dimensions of them before merging.

A) Merging Vectors:

Mechanical & Energy Eng. Dep. 71


B) Merging Matrixes:

2.

Mechanical & Energy Eng. Dep. 72


C) Merging Matrix with a Vector:
If (a) is a vector and (b) is a matrix which their elements are,

Mechanical & Energy Eng. Dep. 73


MATRIX DISPLAYING
To display a matrix or part of matrix, following the below commends

1) Displaying whole matrix:


To display a matrix with all its elements, writing the matrix name then clicking Enter
button.

>> a
a=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Mechanical & Energy Eng. Dep. 74


2) Displaying a row:

To display any row from a matrix, following the below command,

Matrix name (row number to be displayed , : )

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

Mechanical & Energy Eng. Dep. 75


3) Displaying a column:
To display any column from a matrix, following the below command,

Matrix name ( : , column number to be displayed )

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

Mechanical & Energy Eng. Dep. 76


4) Displaying diagonal:

To display the matrix diagonal, the general form is

diag (matrix name)

For example to display the diagonal of the above matrix (a), writing

Mechanical & Energy Eng. Dep. 77


5) Displaying reverse diagonal:

The general form to display the reverse diagonal of a matrix is

diag (fliplr(matrix name))

For the matrix (a) above, to display the reverse diagonal, writing

Mechanical & Energy Eng. Dep. 78


6) Displaying element:

The general form for displaying an element from a matrix is,

Matrix name (element row number , element column number)

For example, to display the element which locate at 2nd row and 4th column, writing

Mechanical & Energy Eng. Dep. 79


THE ALGEBRAIC PROCESSES ON MATRIX
Assume having two matrixes (a) and (b) which there elements are,

a= b=
1 4 7 7 8 9
2 5 8 4 5 6
3 6 9 1 2 3

Now the algebraic processes on these two matrixes are:

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)

Mechanical & Energy Eng. Dep. 80


Then, the matrix that is obtained by adding A and B is:

It is possible to adding two matrixes if there dimensions where identified, also it is


possible to add a constant to a matrix.

Mechanical & Energy Eng. Dep. 81


2) Subtraction ( - ):
It is possible to subtract two matrixes if its dimensions where identified, also it can to
subtract a constant from a matrix

Mechanical & Energy Eng. Dep. 82


3) Multiplication ( * ):
The multiplication operation * is executed by MATLAB according to the rules of linear
algebra. This means that if A and B are two matrices, the operation A*B can be carried
out only if the number of columns in matrix A is equal to the number of rows in matrix
B . The result is a matrix that has the same number of rows as A and the same number
of columns as B .For example ,if A is a 4x3 matrix and B is a 3x2 matrix:,

then, the matrix that is obtained by the operation A*B has the dimension of 4x2 with
the elements:

Mechanical & Energy Eng. Dep. 83


A numerical example is:

Mechanical & Energy Eng. Dep. 84


Mechanical & Energy Eng. Dep. 85
4) Division ( / ) or ( \ ):
In MATLAB it is possible to divide each elements of first matrix by the corresponding
elements of the second matrix, or dividing the matrix by a constant, as written below

Mechanical & Energy Eng. Dep. 86


5) Power ( ^ ):
It is possible to raise the order of a matrix to a power as in the below example,

also, it can raise the elements of a matrix to a power (the power may be constant or
matrix) as written below,

Mechanical & Energy Eng. Dep. 87


SOME PROCESSES ON MATRIXES
1) Transpose:
A matrix known as a transpose matrix if its rows changed to columns, and to find the
transpose matrix, let (a) be a matrix,

Mechanical & Energy Eng. Dep. 88


2) Determine:

The determine of a matrix is a single value. To find the determine of a matrix the
general form is

det (matrix name)

Mechanical & Energy Eng. Dep. 89


3) Summation:
This command will sum all the elements in a column and put it in a vector, and the
general form of this command is

sum (matrix name)

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,

inv (matrix name)

Mechanical & Energy Eng. Dep. 90


Mechanical & Energy Eng. Dep. 91
APPLICATIONS ON MATRIX
Solution of Linear Equations

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)

1. Using Gramer's Rule:

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

Mechanical & Energy Eng. Dep. 92


a11 a12
A= = a11  a22 − a12  a21
a21 a22
b1 a12
A1 = = b1  a22 − a12  b2
b2 a22
a11 b1
A2 = = a11  b2 − b1  a21
a21 b2
A1
X=
A
A2
Y=
A
Using MATLAB program to solve the above two equations
>>A = [a11 a12 ; a21 a22];
>>A1 = [b1 a12 ; b2 a22];
>>A2 = [a11 b1 ; a21 b2];

Mechanical & Energy Eng. Dep. 93


Mechanical & Energy Eng. Dep. 94
2. Using The Form A.X=b

Mechanical & Energy Eng. Dep. 95


Mechanical & Energy Eng. Dep. 96
3. Using Reduced row echelon form (rref):

Mechanical & Energy Eng. Dep. 97


Mechanical & Energy Eng. Dep. 98
CHANGING THE SEQUENCE OF COLUMN AND ROW

Mechanical & Energy Eng. Dep. 99


Assume a matrix (a) which its elements are:
>> a = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ; 13 14 15 16]
a=

Mechanical & Energy Eng. Dep. 100

You might also like