Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

Introduction to Matlab

MATLAB is created by the MathWorks in 1984, is a computer program that provides the user with a
convenient environment in performing many types of calculations. It is a program for doing numerical
computations. It was originally designed for solving linear algebra type problems using matrices. Its
name matlab is derived from matrix laboratory.

1. Define the acronyms matlab.

2. Use common matlab commands and special characters in solving equation/s.

3 Discover matlab mobile in smart phone.

Student will be able to familiarize and use matlab commands in solving of root of equation/s.

Common Matlab Commands

The most common way to operate matlab is by entering commands one at a time in the command
window. Some basic editing commands in the main matlab are as follows:

>> rref means to solve system of equations.

>> clc means to clear the command window.

>> rats means to change decimal to fraction.

>> roots means to solve variables

>> clear means delete all variable


Special Characters

Some special characters used in matrix

[ ] ⟶ used to form vectors and matrices

; ⟶ for each end row of matrix

' ⟶ to transpose a matrix

Matlab required a personal computer or laptop to install the program. Fortunately, matlab can be
download in your smart phone by play store. Using matlab on smart phone must have data or wifi
connection because its not offline application. The computation on pc it almost same as our smart
phone. On play store just search "Matlab Mobile".

Evaluate x2 + xy + y2, if x = 2, y = 3

in command window encode

>> x = 2; y = 3;

>> x^2+x*y+y^2

ans =

19

Using Matlab mobile its look like this


Factor x2 - 7x + 10

write the polynomial expression by numerical coefficient [1 -7 10]. On command window encode

>>t = [1 -7 10]

>>roots(t)

ans =

Using Matlab mobile its look like this


Factor x3 +1

Notice on the expression that their is a missing x 2 and x terms, then replace it by "0"

answer is -1.000 + 0.000i only because the other two are imaginary number, therefore x = -1

Solve 2x + 3y = 11

3x - 4y = -9

change the system of linear equations to augmented matrix name it matrix A

A= , and encode to your matlab mobile just like below


he answers are in the last column x = 1 and y = 3

Solve x – y – 2z = –1

2x + 3y + z = –2

5x + 4y + 2z = 4

change the system of linear equations to augmented matrix name it matrix B and encode to your
Matlab mobile just like below

Solve x – y – 2z = –1

2x + 3y + z = –2

5x + 4y + 2z = 4

change the system of linear equations to augmented matrix name it matrix B and encode to your
Matlab mobile just like below

the answers are in the last column x = 2, y = -3, and z = 3


https://youtu.be/pywm7Lb2pmc

https://youtu.be/lY-9vyFtb44

MatLab is a high-level programming language with an interactive environment


for visualization, numerical computation and programming function. Matlab can be applicable at
numerous instances like

• Allows matrix manipulations


• Plotting of functions and data
• Implementation of algorithms
• Creation of user interfaces
• Analyze data
• Develop algorithm

Activity: Introduction to Matlab

The availability of technical computing environment such as MATLAB is now reshaping the role and
applications of computer laboratory projects to involve students in more intense problem-solving
experience. This availability also provides an opportunity to easily conduct numerical experiments and
to tackle realistic and more complicated problems.

1 To encode matrices into MATLAB.

2 To determine the sum of row, column and diagonal of a matrix using MATLAB.

3 To create a matrix that has same sum of row, column and diagonal of different dimensions using
MATLAB
Students will be able to encode matrices on matlab and create magic square.

Encoding matrices using MATLAB

1. Surround the entire list of element with square brackets, [ ]

2. Separate each elements of a row with commas or space

3. Put a semicolon ( ; ) at the end of each row

1. On the command window, type matrix A

A=
[0 7 23 19 11;18 14 1 5 22; 6 20 17 13 4; 12 3 9 21 15; 24 16 10
2 8]

Using matlab the desktop displays matrix A


A = 0 7 23 19 11
18 14 1 5 22
6 20 17 13 4
12 3 9 21 15
24 16 10 2 8

Finding the Sum of rows, columns and diagonals of matrix A.

1. Sum of columns of matrix A, type on the command prompt sum (A)


2. Sum of rows of matrix A, type on the command prompt sum (A’)’
3. Sum of diagonal of matrix A, type on the command prompt sum (diag(A))
What is the sum of columns? _____ _____ _____ _____ _____
What is the sum of rows? _____ _____ _____ _____ _____
What is the sum of diagonal? _____

2. Determine the sum of rows, columns and diagonal of matrix B using matlab.
Type on command window
>>B = (10*rand(10, 10))

What is the sum of columns?


___ ___ ___ ___ ___ ___ ___ ___ ___ ___
What is the sum of
rows? ___ ___ ___ ___ ___ ___ ___ ___ ___ ___
What is the sum of diagonal? _____

3. Determine the sum of row, column and diagonal of matrix C using matlab.
Type on command window
>>C = magic (10)

What is the sum of columns?


___ ___ ___ ___ ___ ___ ___ ___ ___ ___
What is the sum of
rows? ___ ___ ___ ___ ___ ___ ___ ___ ___ ___
What is the sum of diagonal? _____
What do you observe on the sum of rows, columns and diagonal? __________
https://youtu.be/1bPW-PnBN8c

4. Make a magic square of different dimensions without using negative numbers, without repeating
numbers, avoid copying and adding/subtracting numbers on the default magic square of MATLAB.

An n-by-n magic square is an array containing the integers from 1 to n 2, arranged so that each of the
rows, each of the columns, and the two principal diagonals have the same sum. For each n > 2, there are
many different magic squares of order n, but the MATLAB function magic(n) generates a particular one.
Write the sum of rows, columns and diagonal in the blank.

Matlab allows you to

• Symbolic computation can be easily done

• Implement and test your algorithms easily

• Develop the computational codes easily

• Process still images and create simulation videos easily

• Perform extensive data analysis and visualization

• Develop application with graphics user interface


1.0 Intended Learning Outcomes (ILOs)

At the end of the session the students should be able to :

1. Identify a linear equation and system of linear equation classification based on its solution set.

2. Solve system of linear equations algebraically.

3. Apply Gaussian Elimination and Gauss-Jordan Elimination in solving systems of linear equations.

4. Reflect on how systems of linear equation can help solve simple real life situations.

5. Investigate the difference between Gaussian elimination and Gauss Jordan Elimination method.

6. Manipulate MATLAB to solve simple arithmetic problem.

7. Create a practical/ real life solution that will require the use of the system of linear equations.

TOPICS :

1. Linear Equation

2. System of Linear Equation

3. Gaussian and Gauss-Jordan Elimination


1.1 Introduction to System of Linear Equation

In this module we will be learning about linear equation and system of linear equation, its solution
set and the method of elimination.

To give you a background let me define what is a system?

A system has these properties:

• It consists of several parts which interact and affect one another.

• It produces an effect or output as a result of some cause or input.

One example is a business organization. This system’s inputs are its capital, employees, raw
materials and factories. Its outputs are its products. Management decides what the interactions among
the inputs should be to give the maximum output (for example, how many factories should make which
products, etc.)

A linear system is a system where the output is proportional to the input. For example, if the
business organization is a linear system, then if we double the capital, employees, raw materials and
factories (the inputs) then we expect to get double the production (the output).

We can describe mathematically how the parts of a linear system relate to one another and to
the input using a system of linear equations. If a linear system has n parts (where n is some number),
then we can describe it with a system of n linear equations in n unknowns or variables. The unknowns in
these equations are the values of the inputs. If we did the analysis correctly then there will be a unique
solution for the values of the inputs.

This module also introduced Gaussian elimination as a procedure for solving a system of linear
equations. In this module, you will study this procedure more thoroughly, beginning with some
definitions

Rewriting a system of linear equations in row-echelon form usually involves a chain of equivalent
systems, each of which is obtained by using one of the three basic operations. This process is called
Gaussian elimination, after the German mathematician Carl Friedrich Gauss (1777–1855).

https://mathonweb.com/help_ebook/html/systems_0.htm
1.3 Gaussian and Gauss -Jordan Elimination

Hi, did you know that Carl Friedrich Gauss championed the use of row reduction, to the extent that it
is commonly called Gaussian elimination. It was further popularized by Wilhelm Jordan, who attached
his name to the process by which row reduction is used to compute matrix inverses, Gauss-Jordan
elimination.

Gaussian Elimination and Gauss-Jordan Elimination

In Gaussian elimination, you apply elementary row operations to a matrix to obtain a (row-equivalent)
row-echelon form. In Gauss-Jordan elimination continues the reduction process until a reduced row-
echelon form is obtained.

Let us define some important terms in solving the system of linear equation using Gaussian
Elimination and Gauss-Jordan Elimination

Matrix

Is a rectangular array of numbers denoted by:


columns.

Elementary Row Operations

a. Interchange two rows.

b. Multiply a row by a nonzero constant.

c. Add a multiple of a row to another row.

Row- Echelon Form (REF)- A matrix in row-echelon form has the following properties.

a. All rows consisting entirely of zeros occur at the bottom of the matrix.

b. For each row that does not consist entirely of zeros, the first nonzero entry is 1 (called a leading
1).

c. For two successive (nonzero) rows, the leading 1 in the higher row is farther to the left than the
leading 1 in the lower row.

Take note : A matrix in row-echelon form is in reduced row-echelon form (RREF) if every column that
has a leading 1 has zeros in every position above and below its leading 1.

Steps in Gaussian Elimination and Gauss- Jordan Elimination

1. Write the augmented matrix of the system of linear equations.

2. Use elementary row operations to rewrite the augmented matrix in row-echelon form.

3. Write the system of linear equations corresponding to the matrix in row-echelon form, and use
back-substitution to find the solution.

R E M A R K: In Gauss- Jordan elimination continue the process of reduction until a reduced row-echelon
form is obtained.
1.4 Watch & Learn System of Linear Equation

For additional information, you can watch the following videos.

After watching this video click " Practice Exercise 1.1 " for your Exercises on the System of Linear
Equation. Click " Practice Exercise 1.2 " for your Exercises on Gaussian and Gauss- Jordan Elimination.

Check if you understand the lessons.

Questions and clarification can be asked by clicking " Ask and Learn ".

https://youtu.be/vA-55wZtLeE

https://youtu.be/lP1DGtZ8Wys

1.5 Fundamentals of MATLAB

Intended Learning Outcomes :

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

Fundamentals of MATLAB

MATLAB- MATrix LABoratory.

• Is a versatile piece of computer software with linear algebra capabilities as its core

• The code employed by MATLAB is written in C Language, however, many of the routines and
functions are written in the MATLAB language and are upgraded as new versions of MATLAB are
released.

• MATLAB is a numerical computing environment and programming language. Created by The


MathWorks (Links to an external site.), MATLAB allows easy

–matrix manipulation,

–plotting of functions and data,

–implementation of algorithms,

–creation of user interfaces, and

–interfacing with programs in other languages.

• MATLAB is available for Windows, Macintosh and UNIX systems. It is used by more than one
million people in industry and academia.

Why use Matlab?

• It allows quick and easy coding in a very high-level language.

• Rich data types: Complex number, Three dimensional matrix, structure, cell array, etc

• Lots of nice functions and toolboxes: fminsearch, fsolve, normcdf, norminv, etc; garch,
optimization, symbolic, …

• Lots of users: economists, mathematicians, engineers, …

• High-quality graphics and visualization facilities are available.

• MATLAB M-files are completely portable across a wide range of platforms.

• EXTENSIVE documentation (type ‘helpwin’)

MATLAB WINDOWS

• Command Window
The command window is located in the center pane of the default view of the MATLAB ® screen, as
shown in Figure 1 . The command window offers an environment similar to a scratch pad. Using it allows
you to save the values you calculate, but not the commands used to generate those values. If you want
to save the command sequence, you will need to use the editing window to create an M-file .

• Command History

The command history window records the commands you issued in the command window. When you
exit MATLAB ® , or when you issue the clc command, the command window is cleared. However, the
command history window retains a list of all your commands. You may clear the command history with
the edit menu. If you work on a public computer, as a security precaution, MATLAB ® ’s defaults may be
set to clear the history when you exit MATLAB ®

• Workspace Window

The workspace window keeps track of the variables you have defined as you execute commands in
the command window. These variables represent values stored in the computer memory, which are
available for you to use.

• Current Folder Window

The current folder window lists all the files in the active directory. When MATLAB ® either accesses files
or saves information, it uses the current folder unless told differently. The default for the location of the
current folder varies with your version of the software and the way it was installed

Solving Problems with Matlab

–The command window environment is a powerful tool for solving engineering problems

• Using Variables

Giving names to the values you are using. MATLAB uses the naming conventions that are common to
most computer programs.

• Scalar Operations

MATLAB ® handles arithmetic operations between two scalars much as do other computer programs
and even your calculator.

A single equals sign (=) is called an assignment operator in MATLAB ® . The

assignment operator causes the result of your calculations to be stored in a computer

memory location. In the preceding example, x is assigned a value of 8. If you

enter the variable name

into MATLAB ® , you get the following result:

x=
8

The assignment operator is significantly different from an equality.

Order of Operations

In all mathematical calculations, it is important to understand the order in which operations are
performed. MATLAB ® follows the standard algebraic rules for the order of operation.

Note: Be sure to create account for Mathworks and install MATLAB software or the MATLAB mobile
app so you may be able to do the MATLAB Activity

After reading and understanding this page click "Mark as Done". However if you have questions and
clarification click " Ask and Learn ".

Then you can proceed to the next topic by clicking "Next"

1.6 Summary

A linear equation is an equation involving one or more variables in which only the operations of
multiplication by real numbers and summing of terms are allowed. When several linear equations
involving the same variables are considered together, a system of linear equations is obtained. Many
methods are available for finding the complete solution set for a given linear system.

Gaussian elimination with back-substitution works well for solving systems of linear equations by
hand or with a computer. For this algorithm, the order in which you perform the elementary row
operations is important. Operate from left to right by columns, using elementary row operations to
obtain zeros in all entries directly below the leading 1’s.

With Gaussian elimination, you apply elementary row operations to a matrix to obtain a (row-
equivalent) row-echelon form. A second method of elimination, called Gauss-Jordan elimination, after
Carl Friedrich Gauss and Wilhelm Jordan (1842–1899), continues the reduction process until a reduced
row-echelon form is obtained.
2. 0 Intended Learning Outcomes
(ILOs)

the end of the session the students should be able to:

1. Perform operations on matrices


2. Apply the properties of matrices
3. Solve the inverse of a matrix using elementary row operations.
4. Apply inverse of a matrix in finding the solution set of a system of linear
equations.
5. Relate the operations on matrices to actual situations.
6. Manipulate MATLAB to perform operations on matrices
TOPICS :

1. Operation on Matrices and its Properties


2. Matrix Inverse
3. Solving System of Linear Equation Using Matrix inverse

Click " Next" for the discussion of the topics

2.1 Introduction to Matrices


In the previous module you used matrices to solve systems of linear equations.
In this module you will be introduced to some fundamentals of matrix theory and further
applications of matrices.
It is standard mathematical convention to represent matrices in any one of the

following three ways.


This module will also discuss the mechanics of the three basic matrix operations:
matrix addition, scalar multiplication, and matrix multiplication. This begins to develop
the algebra of matrices. You will see that this algebra shares many (but not all) of the
properties of the algebra of real numbers.
This module further develops the algebra of matrices to include the solutions of
matrix equations involving matrix multiplication. To begin,consider the real number
equation ax = b To solve this equation for x multiply both sides of the equation by a −1
provided a is not equal to 0

2.2 Matrix Operation


In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or
expressions, arranged in rows and columns. Matrices are commonly written in box brackets. The
horizontal and vertical lines of entries in a matrix are called rows and columns, respectively. The
size of a matrix is defined by the number of rows and columns that it contains. A matrix
with m rows and n columns is called an m × n matrix or m-by-n matrix, while m and n are called
its dimensions.The dimensions of the following matrix are 2×3 up(read “two by three”), because
there are two rows and three columns.
MATRIX OPERATIONS
Addition of matrix
The sum A+B of two m-by-n matrices A and B is calculated entry wise (A + B)i,j = Ai,j + Bi,j,
where 1 ≤ i ≤ m and 1 ≤ j ≤ n.
Properties:
Let A, B and C be m x n matrices

1. A+B =B+A
2. A+(B+C)=(A+B)+C
3. There is a unique m x n matrix O (zero matrix) such that A + O =A for any mxn matrix
A.
4. For each mxn matrix A, there is a unique mxn matrix D such that A+ D= O. Where
matrix D is –A (negative of A)

Scalar multiplication
The scalar multiplication cA of a matrix A and a number c (also called a scalar in the
parlance of abstract algebra) is given by multiplying every entry of A by c: (cA)i,j = c · Ai,j.
Properties:
If r and s are real numbers and A and B are matrices of the appropriate size then:

1. r(sA)=(rs)A
2. (r+s)A=rA+sA
3. r(A+B)=rA+rB
4. A(rB)=r(AB)=(rA)B

Matrix multiplication
Multiplication of two matrices is defined only if the number of columns of the left matrix
is the same as the number of rows of the right matrix. If A is an m-by-n matrix and B is
an n-by-p matrix, then their matrix product AB is the m-by-p matrix whose entries are
given by dot-product of the corresponding row of A and the corresponding column of B:
R E M A R K S:

1. BA may not be defined; this will take place if n≠m


2. If BA is defined, which means that m=n, then BA is pxp while AB is m xm if m≠
p, AB and BA are of different sizes.
3. If AB and BA are both of the same size they may be equal or unequal

Properties
If A and B are matrices (with sizes such that the given matrix products are defined)
and c is a scalar, then the following properties are true.

1. A(BC)=(AB)C
2. A(B+C)=AB+AC
3. (A+B)C=AC+BC
4. c(AB)=(cA)B=A(cB)
https://youtu.be/WR9qCSXJlyY

https://youtu.be/TbaltFbJ3wE

https://youtu.be/kT4Mp9EdVqs

https://youtu.be/r9aTLTN16V4

2.5 Matrix in MATLAB


Intended Learning Outcomes:
The student shall be able to :

1. Input matrices with the correct display format


2. Generate matrices
3. Perform matrix operation
3. To assign a matrix, but surpress the display of its entries. follow the closing
square bracket, ]. with a semicolon. Thus:

A= [4 5 8;0 -1 6];
assigns the same matrix to name A as previously but no display appears. To assign a
currently defined matrix a new name, use the assignment operator = . Command Z = A
assigns the contents of A to Z. Matrix A is still defined

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

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

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

• If the matrix is quite large, the screen display will scroll too fast for you to see
the matrix.
• To see a portion of a matrix, type command more on followed by ENTER.
then type the matrix name or a command to generate it. Press the Space Bar
to reveal more of the matrix. Continue pressing the Space Bar until the "--
more--" no longer appears near the bottom of the screen
• To see the (2, 3) entry of A, type A(2,3)
• To see the fourth row of A, type A(4,:)
• To see the first column of A, type A (:, 1)
• In the preceding situations. the: is interpreted to mean "all." The colon can also
be used to represent a range of rows or columns. For example. Typing 2:8

displays
ans=
2 3 4 5 6 7 8

• We can use this feature to display a subset of rows or columns of a matrix. As


an illustration. to display rows 3 through 5 of matrix A. type A (3:5,:)
• Similarly, columns I through 3 are displayed by the command A (:, 1:3)
• For more information on the use of the colon operator, type help colon. The
colon operator in M ATLAB is very versatile, but we will not need to use all of
its features.

Display Formats
MATLAB stores matrices in decimal form and does its arithmetic computations using a
decimal-type arithmetic. This decimal form retains about 16 digits, but not all digits must
be shown.

• If the matrix contains all integers, then the entire matrix is displayed as
integer values; that is, no decimal points appear
• If any entry in the matrix is not exactly represented as an integer, then the
entire matrix is displayed in what is known a. format short. Such a display
shows four places behind the decimal point, and the last place may have been
rounded. The exception to this is zero. If an entry is exactly zero, then it is
displayed as an integer zero. Enter the matrix
Matrix Operations

• + addition
• - subtraction
• * multiplication
• ^ power
• ‘ transpose for a real matrix and complex –conjugate transpose for a complex
matrix (transpose for a complex matrix is .’)
• \ left division, / division

x = A \ b is the solution of A * x = b
Matrix multiplication
–Dimensions must agree
Transpose of Matrix

• In MATLAB the transpose operator (or symbol) is the single quotation mark, or
prime, '. Using the matrices in Example I find C’ and A’

• The following are the function syntax that can be used to manipulate
matrices:
• Matrix Manipulation

Cat -Concatenate array

reshape -reshape array


Diag -diagonal matrices and diagonals of matrix

Tril -extract lower triangular part

Triu -extract upper triangular part

Fliplr -flip matrix in left/right direction

flipud -flip matrix in up/down direction

flipdim -flip matrix along specified dimension

rot90 -rotate matrix 90 degree

: -regularly spaced vector and index into matrix

find -find indices of non zero elements

end -last index

size -gives the size of the matrix

sum -calculate the sum of the entry in the column of the matrix

• Generating special matrices:

blkdiag (Links to an external


- Construct block diagonal matrix from input arguments
site.)
eye (Links to an external
- Identity matrix
site.)
linspace (Links to an
- Generate linearly spaced vectors
external site.)
logspace (Links to an
- Generate logarithmically spaced vectors
external site.)
ones (Links to an external
- Create array of all ones
site.)
rand (Links to an external
- Uniformly distributed pseudorandom numbers
site.)

- Create magic square matrix whose row, column and


magic
diagonal sum are equal

- Create a matrix whose elements are based on pascal’s


pascal
triangle

zeros (Links to an external


- Create array of all zeros
site.)
horzcat (Links to an
- Concatenate arrays horizontally
external site.)
vertcat (Links to an external
- Concatenate arrays vertically
site.)

2.6 Summary

A matrix (whose plural is matrices) is a rectangular array of numbers, symbols, or


expressions, arranged in rows and columns. A matrix with m rows and n columns is
called an m×n matrix or m-by-n matrix, where m and n are called the matrix
dimensions. Matrices can be used to compactly write and work with multiple linear
equations, that is, a system of linear equations.

• When performing addition, add each element in the first matrix to the corresponding
element in the second matrix.
• When performing subtraction, subtract each element in the second matrix from the
corresponding element in the first matrix.
• Addition and subtraction require that the matrices be the same dimensions. The
resultant matrix is also of the same dimension.
• Scalar multiplication of a real Euclidean vector by a positive real number multiplies
the magnitude of the vector without changing its direction.
• If A is an n×m matrix and B is an m×p matrix, the result AB of their multiplication
is an n×p matrix defined only if the number of columns m in A is equal to the
number of rows m in B.
• The product of a square matrix multiplied by a column matrix arises naturally in linear
algebra for solving linear equations and representing linear transformations.

So, what is the inverse of a matrix?


Well, in real numbers, the inverse of any real number a was the number a-1, such
that a times a-1 equaled 1. We knew that for a real number, the inverse of the number
was the reciprocal of the number, as long as the number wasn't zero.
The inverse of a square matrix A, denoted by A-1, is the matrix so that the product of A
and A-1 is the Identity matrix. The identity matrix that results will be the same size as the
matrix A. Wow, there's a lot of similarities there between real numbers and matrices.
That's good, right - you don't want it to be something completely different.
A(A-1) = I or A-1(A) = I
There are a couple of exceptions, though. First of all, A-1 does not mean 1/A. Remember,
"There is no Matrix Division!" Secondly, A-1 does not mean take the reciprocal of every
element in the matrix A.
Requirements to have an Inverse

1. The matrix must be square (same number of rows and columns).


2. The determinant of the matrix must not be zero (determinants are covered in section
6.4). This is instead of the real number not being zero to have an inverse, the
determinant must not be zero to have an inverse.

A square matrix that has an inverse is called invertible or non-singular. A matrix that
does not have an inverse is called singular.
A matrix does not have to have an inverse, but if it does, the inverse is unique.

You might also like