Sunteți pe pagina 1din 35

Lecture 6

Introduction to Matlab
Dr. Norhazwani Md Yunos

Department of Intelligent Computing & Analytics


Faculty of Information & Communication Technology
Universiti Teknikal Malaysia Melaka

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 1 / 34


Lecture Contents

Lecture Contents:

1 MATLAB Introduction and Working Environment


Introduction
MATLAB Working Environment

2 Numerical Computation with MATLAB


Variables and Operators
Matrix Variables
Matrix Operations
Elementary Row Operation
Vector
Eigenvalues and Eigenvectors

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 2 / 34


MATLAB Introduction and Working Environment

Lecture Contents:

1 MATLAB Introduction and Working Environment


Introduction
MATLAB Working Environment

2 Numerical Computation with MATLAB


Variables and Operators
Matrix Variables
Matrix Operations
Elementary Row Operation
Vector
Eigenvalues and Eigenvectors

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 3 / 34


MATLAB Introduction and Working Environment Introduction

MATLAB Introduction

MATLAB started as an interactive program for doing matrix calculations


(the name MATLAB is short for “Matrix Laboratory”).
Now, it has grown to a high level mathematical language that can solve
integrals and differential equations numerically and plot a wide variety
of two and three dimensional graphs.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 4 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

The MATLAB Working Environment

The following table summarizes the main components of the MATLAB


working environment.

Tool Description
Command Window This is the window in which you execute MATLAB
commands.
Command History This presents a history of the functions introduced in the
Command Window and allows you to copy and execute
them.
Workspace This shows the present contents of the workspace and
allows you to make changes to it.
Help This allows you to search and read the documentation for
the complete family of MATLAB products.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 5 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 6 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

The MATLAB Prompt >>

Look at the command window and you will see the cursor flickering
after the prompt ‘>>’.

This means that MATLAB is waiting for further instructions.


Just type the command
>> demo (and then press Enter or Return key)
and you will be directed to the following page as shown in slide no 8.
This briefly shows you some of the things that MATLAB can do.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 7 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 8 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Help in MATLAB

You can find help for MATLAB via the help button in the toolbar,

and you will be directed to the following page as shown in slide no. 10

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 9 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 10 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 11 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

In addition, support can also be obtained via MATLAB commands.


The command ‘help general’ provides help on general
information on MATLAB commands, as shown in figure in slide no. 13.
By clicking on any of them, you can get more specific help.
This is also applied to other help commands, such as
Command Topic
help ops - Operators and special characters.
help lang - Programming language constructs.
help elmat - Elementary matrices and matrix multiplication.
help elfun - Elementary math functions.
help specfun - Specialized math functions.
help matfun - Matrix function, numerical linear algebra.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 12 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 13 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 14 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Operators and Precedence

There are many operators in MATLAB which can be used in expressions.


Some of arithmetic operators include:
+ addition
− subtraction, negation
∗ multiplication
/ division (divide by)
\ division (divide into, e.g., 3\ is 4)
∧ exponentiation
Putting a semicolon, ; at the end of a statement will suppress the output
from that statement. This means that the command will be carried out,
but MATLAB will not show the result yet.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 15 / 34


MATLAB Introduction and Working Environment MATLAB Working Environment

Clear Command Window and Workspace

Use ‘clc’ to clear the command window.


Use ‘clear’ to clear the workspace window - clear all the declared
variables.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 16 / 34


Numerical Computation with MATLAB

Lecture Contents:

1 MATLAB Introduction and Working Environment


Introduction
MATLAB Working Environment

2 Numerical Computation with MATLAB


Variables and Operators
Matrix Variables
Matrix Operations
Elementary Row Operation
Vector
Eigenvalues and Eigenvectors

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 17 / 34


Numerical Computation with MATLAB Variables and Operators

Basic Numerical Computation

You can use MATLAB as a powerful numerical computer.


While most calculators handle numbers only to a preset degree of
precision, MATLAB performs exact calculations to any desired degree of
precision.
In addition, unlike calculators, we can perform operations not only with
individual numbers, but also with objects such as arrays.
Here are some examples of numerical calculations with MATLAB.
(As we know, to obtain the results it is necessary to press Enter once the
desired command has been entered after the prompt ‘>>’.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 18 / 34


Numerical Computation with MATLAB Variables and Operators

For example, we simply calculate 4 + 7 to obtain the result 11. To do


this, just type 4 + 7, and then Enter.
>> 4 + 7
ans =
11
To find the value of 3100 :
>> 3∧ 100
ans =
5.1538e+47
We can use the command ‘format long e’ to obtain results to 16
decimal figures more than the power of 10 required.
>> format long e
>> 3∧ 100
ans =
5.153775207320113e+47
Dr. Wanie MY BITI 1213 L06: Intro to Matlab 19 / 34
Numerical Computation with MATLAB Variables and Operators

We can also work with complex number.


To find the result of the operation (2 + 3i)10 :
>> (2 + 3i)∧ 10
ans =
-3.415250000000000e+05 -
1.456680000000000e+05i

The previous result is also available in short format, using the ‘format
short’ command1 .
>> format short
>> (2 + 3i)∧ 10
ans =
-3.4153e+05 - 1.4567e+05i

1
1. format short = Delivers results to 4 decimal places, 2. format long = Delivers
results to 16 significant decimal figures
Dr. Wanie MY BITI 1213 L06: Intro to Matlab 20 / 34
Numerical Computation with MATLAB Variables and Operators

Variables and Assignment Statements

MATLAB does not require a command to declare variables.


A variable is created simply by directly allocating a value to it.
An assignment statement takes the form:
variable = expression
where the name of the variable is on the left-hand side, while the
expression is on the right-hand side, and the assignment operator is =.
Defined variables can be seen in the workspace window.
For examples,
>> mynum = 20 + 30 >> x = 201 + 101
mynum = x =
50 302

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 21 / 34


Numerical Computation with MATLAB Matrix Variables

Matrix Variables
MATLAB defines arrays by inserting in brackets all its row elements
separated by a semicolon.
Elements in a row can be entered by separating each element by spaces
or by commas, and a semi-colon indicates the end of each row.
The entire list of elements has to be surrounded by square brackets [ ].
For example, a 3 × 3 matrix variable can be entered in the following two
ways:
>> M = [a11 a12 a13 ; a21 a22 a23 ; a31 a32 a33 ]
>> M = [a11 , a12 , a13 ; a21 , a22 , a23 ; a31 , a32 , a33 ]

MATLAB displays the matrix:


M = a11 a12 a13
a21 a22 a23
a31 a32 a33

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 22 / 34


Numerical Computation with MATLAB Matrix Variables

Similarly we can define an array of variable dimension (m × n).


Once a matrix variable has been defined, MATLAB enables many ways
to insert, extract, renumber and generally manipulate its elements.
For example,
zeros(m, n) - Creates the zero matrix of order m × n.
>> A = zeros(4, 2)
A =
0 0
0 0
0 0
0 0

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 23 / 34


Numerical Computation with MATLAB Matrix Variables

eye(n) - Creates the identity matrix of order n.


>> A = eye(4)
A =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

ones(n) - Creates the matrix of order m × n with all its elemets


equal to 1.
>> A = ones(4)
A =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Dr. Wanie MY BITI 1213 L06: Intro to Matlab 24 / 34
Numerical Computation with MATLAB Matrix Variables

rand(n) - Creates a uniform random matrix of order n.


>> A = rand(3)
A =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575

Some examples for other matrix variables:


A(i,:) - Defines the i-th row of the matrix A.
A(:,j) - Defines the j-th column of the matrix A.
A’ - Returns the transpose of the matrix A.
tril(A) - Returns the lower triangular part of the matrix A.
triu(A) - Returns the upper triangular part of the matrix A.
Det(A) - Returns the determinant of the matrix A.
Inv(A) - Returns the inverse of the matrix A.
[A,B,C,. . .] - Defines the matrix formed by the matrices A, B, C, . . .
Dr. Wanie MY BITI 1213 L06: Intro to Matlab 25 / 34
Numerical Computation with MATLAB Matrix Operations

Matrix Operations
Sum of matrices A and B: Difference of matrices A and B:

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 26 / 34


Numerical Computation with MATLAB Matrix Operations

Product of matrices A and B: Product of a scalar with matrix A:

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 27 / 34


Numerical Computation with MATLAB Matrix Operations

Determinant of matrix F: Inverse of matrix F:

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 28 / 34


Numerical Computation with MATLAB Elementary Row Operation

Elementary Row Operation

Recall, the following 3 × 3 system of linear equations:

3x1 + 2x2 + x3 = 8
2x1 − x2 + 4x3 = 5
4x1 − 2x2 + 2x3 = 10

can be written in matrix form:


    
3 2 1 x1 8
2 −1 4 x2  =  5 
4 −2 2 x3 10

To solve for x, we start with the augmented matrix [A b] and perform


forward elimination by finding EROs to get it to rref.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 29 / 34


Numerical Computation with MATLAB Elementary Row Operation

In MATLAB, to solve Ax = b, we begin by augmenting [A b]:

MATLAB has a function rref to


reduce:

The answer is x1 = 2.5714,


x2 = 0.1429, and x3 = 0.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 30 / 34


Numerical Computation with MATLAB Vector

Vector

A vector variable on n elements can be defines in MATLAB in the


following ways:
>> V = [v1 v2 v3 . . . vn ]
>> V = [v1 , v2 , v3 , . . ., vn ]

Once a vector variable has been


defined, MATLAB enables
many ways to insert, extract,
renumber and generally
manipulate its elements.
For example, to calculate the
norm of a vector:

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 31 / 34


Numerical Computation with MATLAB Eigenvalues and Eigenvectors

Eigenvalues and Eigenvectors

Returns the diagonal matrix D of


eigenvalues of A, and a matrix V
whose columns are the
corresponding eigenvectors, so
that A ∗ V = V ∗ D.

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 32 / 34


Numerical Computation with MATLAB Eigenvalues and Eigenvectors

Eigenvalues and Eigenvectors

Returns the diagonal matrix D of


eigenvalues of A, and a matrix V
whose columns are the
corresponding eigenvectors, so
that A ∗ V = V ∗ D.
 
−0.2320
λ1 = 16.1168, v1 = −0.5253
−0.8187
 
−0.7858
λ2 = −1.1168, v2 = −0.0868
0.6123
 
0.4082
λ3 = −0.0000, v3 = −0.8165
0.4082
Dr. Wanie MY BITI 1213 L06: Intro to Matlab 32 / 34
We have covered:

1 MATLAB Introduction and Working Environment


Introduction
MATLAB Working Environment

2 Numerical Computation with MATLAB


Variables and Operators
Matrix Variables
Matrix Operations
Elementary Row Operation
Vector
Eigenvalues and Eigenvectors

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 33 / 34


Next...

Introduction to Logic I

Dr. Wanie MY BITI 1213 L06: Intro to Matlab 34 / 34

S-ar putea să vă placă și