Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Introduction To MATLAB: Lecturer: Cik Sitinoor Adeib Binti Idris Room: Level 5 CONTACT NUMBER: 03-5543 6362

This document provides an introduction to MATLAB. It covers topics like generating matrices, simple arithmetic, obtaining online help, and using MATLAB functions. Matrices can be entered explicitly by listing elements separated by spaces/commas within square brackets. Functions like sin() can be applied to matrices element-wise. MATLAB supports standard arithmetic operators and functions have multiple input/output arguments. Matrix operations include transposition, arithmetic on scalars, and left/right division.

Uploaded by

seddeswerty
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Introduction To MATLAB: Lecturer: Cik Sitinoor Adeib Binti Idris Room: Level 5 CONTACT NUMBER: 03-5543 6362

This document provides an introduction to MATLAB. It covers topics like generating matrices, simple arithmetic, obtaining online help, and using MATLAB functions. Matrices can be entered explicitly by listing elements separated by spaces/commas within square brackets. Functions like sin() can be applied to matrices element-wise. MATLAB supports standard arithmetic operators and functions have multiple input/output arguments. Matrix operations include transposition, arithmetic on scalars, and left/right division.

Uploaded by

seddeswerty
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to MATLAB

LECTURER : CIK SITINOOR ADEIB BINTI IDRIS


ROOM: LEVEL 5
CONTACT NUMBER: 03-5543 6362
Topics which will be covered in this session
are:
 Matrices Generation
 Simple Arithmetic
 Obtaining on-line help
 Using MATLAB functions
a) Matrices generation
 Most of the operations which can be performed in
MATLAB require that data is present in matrix form.
 For example, a typical data set obtained from an
industrial applications may contain a series of 20
measurements of 2 process variables.
 For use in MATLAB this data would be converted
into a matrices with 20 rows and 2 columns.
 Matrices can be entered into the MATLAB
memory in a number of different ways:
 Enter an explicit list of elements
 Generate a matrix using one of the standard
MATLAB expressions
 Load the matrices from disk
 When using small matrices, i.e. up to a size of
approximately 5 x 5 (5 rows by 5 columns) it
is easiest to enter the matrix explicitly.
 In this case, the list of elements must be
separated by blanks/spaces or commas and
the individual rows must be separated by the
semicolon (;)
 The elements of the matrix must also be
surrounded by square parentheses [ ]
Example 1
Elements separate by blanks/space
 Enter the following statement into MATLAB
 >> x = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]

 The results will be the following output :


 x=
1 2 3
4 5 6
7 8 9
Example 2
Elements separate by comma
 Enter the following statement into MATLAB
 >> A = [ 9,8,7 ; 6,5,4 ; 3,2,1 ]

 The results will be the following output :


 A=
9 8 7
6 5 4
3 2 1
 Large matrices can be entered over a number
of lines.
 In this case the carriage return is used to
replace the semicolon.
 Try entering the following statement:
 >> x = [ 1 2 3
4 5 6
7 8 9]
 Individual matrix elements can be referenced
with indices inside parentheses ( )
 For example
 >> a = x ( 1 , 3 )

Produces
a=
3
 The output means the element of x in the
first row and the third column is assigned to
the variable a.
 REMEMBER! When using this form of
expression that the ROW NUMBER is referred
to FIRST and the COLUMN NUMBER SECOND
Example 3
 >> x ( 3 , 3 ) = x ( 1 , 3 )

 Returns
 x=
1 2 3
4 5 6
7 8 3
 When referencing a vector, i.e. a matrix with
a single column or row only a single figure is
required in the parentheses.
 If required further columns and/or rows can
be appended to a matrix with relative ease.
 For example if we define a new matrix y as
follows:
 >> y = [ 10 11 12 ]
 Then enter the following command:
 >> x = [ x ; y ]

The resulting output is:


x=
1 2 3
4 5 6
7 8 3
10 11 12
 It is also possible to extract small matrices from
large matrices using the colon, ‘:’
 For example
 >> x = x ( 1 : 3 , : )

takes the rows 1 through to 3 and every column in


the variable x and assigns them to be matrix x.
therefore the variable x is returned to its original
size.
 If we wish to enter a MATLAB expression but do not
wish the output to be displayed on the screen then
a semicolon can be placed at the end of an
expression, for example if we had entered the
following command earlier:
 >> x = [ x ; y ];

the variable x would have been stored but the


contents of the element would not have been
displayed on the screen.
Problems
1. Generate the following matrix and store it as variable
‘example’
[ 3 5 1
1 2 5
6 1 0 ]

2. Append the following column of data to this matrix


[ 1 2 4]

3. Set the values of the first column of ‘example’ equal to the


values in the third column.
b) Simple Arithmetic Expressions
 Try entering these following statement:
 >> 5 + 5
The output would be
ans =
10
 Next, try enter this statement:
 >> b = 5 * 5
b=
25
Usual arithmetic operators
 + addition
 - subtraction
 * multiplication
 / division
 ^ power
 Typical functions that can also be used in the
MATLAB environment
 >> sin (b)
Would yield:
ans =
-0.1324

=>Single input and output argument


Example 4
 Functions can be applied to each and every
element in a matrix, for example:
 >> sin (x)
Results in
ans =
0.8415 0.9093 0.1411
-0.7568 -0.9589 -0.2794
0.6570 0.9894 0.1411
-0.5440 -1.000 -0.5366
c) MATLAB Functions
1) More than one input and/or output
argument
E.g.
>> g = gcd ( 20 , 90 )
calculates the greatest common denominator
of integers 20 and 90.
2) Two output values
Whenever there are more than one output value,
the names of the output variables must be
surrounded by square parentheses, and separated
by commas
E.g
>> a = [ 1 2 5 3 1 ];
>>[ m , i ] = max ( a )
returns the maximum value of a, m and also the
index of the maximum value i.e. the column which
contains the maximum value, i.
3) Functions which calculate multiple outputs
can also return fewer output arguments.
e.g.
>> max (a)
merely returns the maximum value stored in
variable a
Matrix Arithmetic and Manipulation
1) Transposing Matrices
= performed by using the (‘) character.
= for example
a) >> a = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
>> b = a’

b) >> x = [ 1 0 2 ] ‘
2) Matrix Arithmetic
= is performed exactly as you would expect
= for example:
>> c = a + b
>> c = a * b
It is possible to multiply, divide, add and
substract matrices by scalars (i.e. single
numbers).
For example:
>> y = x * 2

>> y = x - 1
Matrix Division
1) Right division 2) Left division
= / character = \ character

X = A / B is the solution to X = A \ B is the solution to


X*B=A [ X = A * inverse (B) ] A*X=B [ X = inverse (A) * B ]

You might also like