You are on page 1of 11

LAB SESSION 01

INTRODUCTION TO MATLAB

OBJECTIVES:
(a) To get acquainted with MATLAB’s environment
(b) To use MATLAB as a calculator
(c) To learn to initialize and manipulate vectors, matrices
(d) To learn to create MATLAB variables and basic plots

PRE-LAB THEORY

What is MATLAB?
MATLAB is a software program that allows you to do data manipulation and
visualization, calculations, math and programming. It can be used to do very
simple as well as very sophisticated tasks. It can solve large systems of equations
efficiently and it is therefore useful for solving differential equations and
optimization problems. It also provides excellent means for data visualization and
has symbolic capabilities. Whilst simple problems can be solved interactively with
MATLAB, its real power shows when given calculations that are cumbersome or
extremely repetitive to do by hand!

Where can we use MATLAB?


MATLAB is recognized as the interactive program for numerical linear algebra
and matrix computation. In industries, MATLAB is used for research and to solve
practical engineering and mathematical problems. Also, in automatic control
theory, statistics and digital signal processing (Time-Series Analysis) one can use
MATLAB. The following tool boxes make it useful in soft computing at various
industrial and scientific areas:
(i) Neural Networks (ii) Optimization (iii) Genetic Algorithms
(iv) Wavelets (v) Fuzzy Logic (vi) Control systems
(vi) Signal processing (vii) Communication Systems
Starting MATLAB
you can enter MATLAB by double-clicking on the MATLAB shortcut icon on the
Window. After starting MATLAB, the MATLAB Desktop will appear. The default
three-part window looks similar to the figure below. The Desktop is also known as
an Integrated Development Environment (IDE). Clicking the Start button in the
lower left-hand corner will display a list of MATLAB tools, shortcuts, and
documentation.

Figure 1.1 GUI view of MATLAB


Using MATLAB as a Calculator:
As an example of a simple interactive calculation, just type the expression you
want to evaluate. Let’s start at the very beginning. For example, in order to
calculate the following expression 1+2*3, one needs to type it at the prompt
command (>>) as follows:
>> 1+2*3
ans=
7
Lab Task:
Try the following and record your observations:
>>3+7.5
>>18/4
>>3*7
>>3^2
Observations:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
Mathematical Functions:
The following table lists some commonly used standard MATLAB functions:

Function MATLAB Syntax


ex exp(x)
√x sqrt (x)
Ln x log(x)
Log10 (x) log10(x)
Cos x cos(x)
Sin x sin(x)
Tan x tan(x)
Cos-1 x acos(x)
Sin-1 x asin(x)
Tan-1 x atan(x)
Example 1:
y= e-a sin(x) + 10√y
for a=5, x=2, and y=8 is computed by
>>a=5; x=2; y=8;
>>y=exp (-a)*sin(x) +10*sqrt(y)
y=
28.2904
Example 2:
To calculate sin (pi/4) and e10
>>sin(pi/4)
ans=
0.7071
>>exp(10)
ans=
2.2026e+004

Variables, Expressions & Statements:


Variable names must be a single word containing no space and up to 31 characters.
Variables names are case sensitive and a variable name must start with a letter.
Punctuation characters are not allowed.

MATLAB’s statements are of the form:


>> variables = expression or
>> expression
Expressions are composed of operators, function and variable names. After
evaluation the value is assigned to the variable and displayed. If the variable name
and = sign are omitted, a variable ans (for answer) is automatically created and the
result is assigned to it.
Rules of Precedence:
Expressions are evaluated from left to right with exponential operation having the
highest precedence, followed by multiplication and division having equal
precedence, followed by addition and subtracting having equal precedence.
Parentheses can be used to alter this ordering in which case these rules of
precedence are applied within each set of parentheses starting with the innermost
set and proceeding outward. The most recent values assigned to the variables you
used in the current session are available. For example,
if you type a at the prompt you get the output as:
>> a
a=
7
The display of numerical values can have different format as we see below:
>> e= 1/3
e=
0.3333
>> format long (long decimal format)
>> e
e=
0.33333333333333
>> format short e (short exponential format)
>> e
e=
3.3333 e-01
>>format long e (long exponential format)
e=
3.33333333333333 e-04
>>format ( default format)
>>e
e=
0.333
Entering Matrices:
You can enter matrices in various different ways:
• Enter an explicit list of elements.
• Load matrices from external data files.
• Generate matrices using built-in functions.
• Create matrices with your own functions and save them in files.
MATLAB works with essentially only one kind of objects, i.e. a rectangular
numerical matrix with possibly complex entries. All variables represent matrices.
If you want to store a matrix 1 2 3 in a variable a in the MATLAB’s current
memory, you type the following:
456
789
>> a = [1 2 3; 4 5 6; 7 8 9]
a=
123
456
789
The rows are separated by semicolons and elements are separated by space or by
comma. That is, the above matrix can also be stored by the following command.
>> a = [1,2,3;4,5,6;7,8,9];
or by the statement
>> a = [
123
456
789]
The semicolon at the end of a command suppresses the output.
The matrix a is transposed and is stored in b by the following command
>> b = a’
b=
147
258
369
The following matrix operations are available in MATLAB:

Matrix Initialisation and Addressing:


>>A=[1 2 3 4;5 6 7 8]
A=
1234
5678
>>A( : , : )
Ans=
1234
5678
>>A(1,2)
Ans=
2
>>A(1, :)
Ans=
1234
>>A(: ,[1,3])
Ans=
13
57
>>A=(: ,end)
Ans=
3
7
Selected portion of one matrix can be assigned to a new matrix
>>B= A(: , 3 :end)
B= 3 4
78
To select the elements along main diagonal,
>>Diag(B)
Ans=
3
8
Changing and deleting matrix elements:
>>A=1:5
A=
12345
>>A(2) = 6
A=
16345
>>A([1 3]) = 0
A=
06045
Manipulating Matrix:
>>A=[1 3 4; 5 7 8]
A=
134
578
For transpose,
>>A’
Ans=
15
37
48
>>fliplr (A)
Ans=
431
8 7 5 >>flipud(A)
Ans=
578
134

Matrix Operation:
>>A=[1 3 4 ; 5 7 8]
A=
134
578
>>2+A
Ans=
359
7 9 10
Element-by-element operations vs. Matrix Operations:
Matrix multiplication has a special procedure, and it is different from simple
element-to-element multiplication. This is unlike addition operation, where two
matrices cab be added by simple element-byelement addition, this is adding
corresponding elements of the two matrices in question. Thus, we have a special
set of operators that distinguish Matriz operations from element-by-element
operations. When you intend to perform matrix operation, you simply use, * for
multiplication, / for division, ^ for exponentiation and so on. But if you intend to
perform element-by-element operations, you have to use, .* for multiplication, ./
for division and .^ for exponentiation.
Example,
>>A=[2 3 4;5 6 7;2 1 0];
>>B=[1 2 3;5 6 2;0 0 5];
>>A*B
Ans=
17 22 32
40 52 59
7 10 8
>>A.*B
Ans=
2 6 12
25 42 12
0 0 0

You might also like