0% found this document useful (0 votes)
4 views

Lec 1,2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lec 1,2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

An Introduction to MATLAB

Learning Objectives:

• Understand what MATLAB is.


• Know how to get started with MATLAB.
• Recognize the basics of MATLAB.
• know how to solve various problems
Be able to explore MATLAB on your own !
Contents
• Introduction
• Getting Started
• Vectors and Matrices
• Built in functions
• M–files : script and functions
Introduction
MATLAB – MATrix LABoratory
• Initially developed by a lecturer in 1970’s to help students learn
linear algebra.
• It was later marketed and further developed under MathWorks
Inc. (founded in 1984) – www.mathworks.com
• Matlab is a software package which can be used to perform
analysis and solve mathematical and engineering problems.
• It has excellent programming features and graphics capability –
easy to learn and flexible.
• Available in many operating systems – Windows, Macintosh,
Unix, DOS
• It has several tool boxes to solve specific problems.
Getting Started
Run MATLAB from Start  Programs  MATLAB

– command history, command window, workspace, etc


• For Matlab Student – only command window

Command window
• Main window – where commands are entered
Vectors and Matrices
Variables
ALL variables are matrices

e.g. 1 x 1 4x1 1x4 2x4


4 3 3 2 1 7 2 1 5 6
2 9 3 2 4
   
9 
 
3
Variables
•They are case–sensitive i.e x  X
•Their names can contain up to 31 characters
•Must start with a letter

Variables are stored in workspace


Rounding
• How do we assign a value to a variable?

>> v1=3 >> whos


v1 = Name Size Bytes Class
3 R 1x1 8 double array
>> i1=4 i1 1x1 8 double array
i1 = v1 1x1 8 double array
4 Grand total is 3 elements using 24 bytes
>> R=v1/i1 >> who
R= Your variables are:
0.7500 R i1 v1
>> >>
• Example:

A  1 2 3 4 5
>> A = [1 2 3 4 5] A row vector –
A = values are
1 2 3 4 5 separated by
>> spaces

>> B = [10;12;14;16;18]
B = 10
10 12
12
A column
vector –  
14 values are B  14 
 
separated
16 by semi–
18 colon (;) 16
>> 18
If we want to construct a vector of, say, 100
elements between 0 and 2 – linspace

>> c1 = linspace(0,(2*pi),100);
>> whos
Name Size Bytes Class
c1 1x100 800 double array
Grand total is 100 elements using 800 bytes
>>
If we want to construct an array of, say, 100
elements between 0 and 2 – colon notation
>> c2 = (0:(2*pi)/99:(2*pi))
>> whos
Name Size Bytes Class
c1 1x100 800 double array
c2 1x100 800 double array
Grand total is 200 elements using 1600 bytes
>>
• How do we assign values to matrices ?

>>> A=[1 2 3;4 5 6;7 8 9]


A =
1 2 3 1 2 3 
4 5 6  4 5 6
7 8 9  
>>> 7 8 9

Columns separated by Rows separated by


space or a comma semi-colon
Vectors and Matrices

• How do we access elements in a matrix or a vector?

Try the followings:


>>> A(2,3) >>> A(:,3)
ans = ans =
6 3
6
9

>>> A(1,:) >>> A(2,:)


ans = ans =
1 2 3 4 5 6

>> D = (0:5)
D=
0 1 2 3 4 5 >> D (3)
ans =
2
Vectors and Matrices
• Some special variables

>> 1/0
Warning: Divide by zero.
beep ans =

pi () Inf
>> pi
inf (e.g. 1/0)
ans =
i, j ( 1 ) 3.1416
>> i
ans =
0+ 1.0000i
Vectors and Matrices
• Arithmetic operations – Matrices

Performing operations to every entry in a matrix


Add and subtract
>>> A=[1 2 3;4 5 6;7 8 9] >>> A+3
A = ans =
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 10 11 12
>>>
>>> A-2
ans =
-1 0 1
2 3 4
5 6 7
>> X= sum (A)
>> x =sum (A)
x=
12 15 18
Vectors and Matrices

• Arithmetic operations – Matrices

Performing operations to every entry in a matrix


>>> A=[1 2 3;4 5 6;7 8 9] Multiply and divide
A =
1 2 3 >>> A*2
4 5 6 ans =
7 8 9 2 4 6
>>> 8 10 12
14 16 18

>>> A/3
ans =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
2.3333 2.6667 3.0000
Vectors and Matrices

• Arithmetic operations – Matrices

Performing operations to every entry in a matrix


Power
>>> A=[1 2 3;4 5 6;7 8 9]
A= To square every element in A, use
1 2 3 the element–wise operator .^
4 5 6
7 8 9 >>> A.^2
>>> ans =
1 4 9
16 25 36
49 64 81
>>> A^2
ans =
A^2 = A * A 30 36 42
66 81 96
102 126 150
Vectors and Matrices

• Arithmetic operations – Matrices

Performing operations between matrices


>>> A=[1 2 3;4 5 6;7 8 9] >>> B=[1 1 1;2 2 2;3 3 3]
A = B =
1 2 3 1 1 1
4 5 6 2 2 2
7 8 9 3 3 3

 1 2 3  1 1 1  14 14 14 
 4 5 6  2 2 2  32 32 32 
    =  
A*B 7 8 9 3 3 3 50 50 50 

 1x1 2x1 3x1  1 2 3


4 x 2 5x 2 6 x 2  8 10 12 
A.*B   =  
 7 x3 8x3 9x 3 21 24 27 
Vectors and Matrices

• Arithmetic operations – Matrices

Performing operations between matrices

 1 / 1 2 / 1 3 / 1 1.0000 2.0000 3.0000


A./B 4 / 2 5 / 2 6 / 2 = 2.0000 2.5000 3.0000
   
7 / 3 8 / 3 9 / 3 2.3333 2.6667 3.0000
Vectors and Matrices

• Arithmetic operations – Matrices

Performing operations between matrices

??? Error using ==> ^


A^B
At least one operand must be scalar

 11 21 31   1 2 3 
A.^B  2 2 2
=  16 25 36 
 4 5 6   
73 83 93  343 512 729
 

You might also like