You are on page 1of 21

Ch.

1 Computations with Matlab

02212471
Semester I - 2011

1
Matlab
[24may2011 rev.1.2]
1.1 Matlab ?
Matlab Visualization
Matlab MATrix LABoratory MathWorks
Matlab (Matrix Manipulation and
Computation) Matlab (interactive) Basic
QBasic compiled mode C Pascal Matlab

Matlab


(strings) array
matrix (scalar) 1x1
1 (Row vector)
1 (Column vector) Matlab
vector matrix
[A]{x} = {b}
( x) Matlab
- Matlab
2 3
- Matlab (command line)
(script file)
- Matlab
C, Pascal, Fortran
- Matlab (built-in function)

Control, Image Processing, Artificial Neural


Network Matlab toolbox function
1

Ch.1 Computations with Matlab

02212471
Semester I - 2011

- Matlab Dynamic Link Excel


C Visual Basic Windows

1.2 Matlab
Matlab desktop Matlab Desktop
(window) GUI (Graphic User Interface) Matlab
Matlab desktop 1.1
Command window prompt, >>
prompt cursor Matlab

1.1 Matlab Desktop

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.3 Matlab
Matlab
Enter
>> 10+13
ans =
23

Matlab (ans =)
variables
>> john = 10
john =
10
>>sam = 13;
>> john + sam
ans =
23

Matlab sam = 13 Enter semicolon


Matlab
ans variable
Casio fx-5500L

Sine 90
>> sin(pi/2)
ans =
1

Matlab (radian) (degree)


pi Matlab ~3.1416

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.4 Vector(s) Matrix (Matrices)


Matlab matrix (an array of numbers)
m n (m-row n-column) m=3
n=2 32 Matlab Square brackets
[] Space (
Colon , ) ( 2, 3, )
Semicolon ;
>> A=[1 2;3 4;5 6]
A =
1
2
3
4
5
6

= 1 2 Row Vector (
B) Column Vector ( C)
>> B = [1 2 3]
B =
1
2

>> C = [1;2;3]
C =
1
2
3

(Properties) transpose B
C C=B Matlab single quote () transpose
>> B
ans =
1
2
3

1
Matlab
>> T = [42.1 43.6 39.5 38.2 40.4 41.7 41.9]
T =
42.1000
43.6000
39.5000
38.2000
40.4000

41.7000

41.9000

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.4.1
1. syntax
matrixname(row#,column#)

Parentheses, () Square brackets, []


>> T(1,3) %Displays the element in the 1st row and 3rd column of T
ans =
39.5000

% Matlab comment
Matlab code

>> A(:,2) %Displays the 2nd column of A


>> A(3,:) %Displays the 3rd row of A

colon Matlab
: row A(:,2) 2
A 2
2. syntax
>> A(2:3,1:2)
ans =
3
4
5
6

% 2 3 2

: 2:3
2 3
3. (major diagonal main diagonal) diag( )
square matrix
>> Adiag = diag(A)
Adiag =
1
4

Ch.1 Computations with Matlab

02212471
Semester I - 2011

4. assign square brackets, []



>> A(:,2)=[]
A =
1
3
5

5. (Concatenation)
>>D=[3 7 4;5 9 2;4 6 1];
>>E=[A A+12; A*3 A/2]
B =
3.0000
7.0000
4.0000
5.0000
9.0000
2.0000
4.0000
6.0000
1.0000
9.0000 21.0000
12.0000
15.0000 27.0000
6.0000
12.0000 18.0000
3.0000

15.0000
17.0000
16.0000
1.5000
2.5000
2.0000

19.0000
21.0000
18.0000
3.5000
4.5000
3.0000

16.0000
14.0000
13.0000
2.0000
1.0000
0.5000

1.4.2
3 zero matrix,
the identity matrix matrixes/vectors of ones
>> F=zeros(3,3)

%displays a 3x3 matrix of zeros

>> G=eye(4,4)

%displays a 4x4 identity matrix

>> H=ones(3,1)

%displays a 3x1 matrix of ones

1.5
Matlab built-in functions (trigonometry
functions) 1 Sine 1
365 t 365
>> t=1:1:365
>> temp=15*sin(2*pi*t/365)+12

Ch.1 Computations with Matlab

02212471
Semester I - 2011

Syntax Row vector 1 365 step


+1 syntax t=1:365 step default +1
syntax Sine t t 365
temp 365 ( Matlab )
1.2 Matlab
>> plot(t,temp);

1.2 sine (365 )


step step = 5
Matlab options
+ x y 1.3
>> t=1:5:365;

Ch.1 Computations with Matlab

02212471
Semester I - 2011

>> temp=15*sin(2*pi*t/365)+12;
>> plot(t,temp,+);
>> xlabel(Time, days);
>> ylabel(Temperature, degree C);
>> title(Annual Temperature Distribution);

1.3 sine
built-in function help
>>help plot

Ch.1 Computations with Matlab

02212471
Semester I - 2011

function Matlab lookfor


keyword topic function help

>>lookfor interpolation

1.6 Matlab
Matlab
Matlab Windows
ASCI text file Matlab

0.0700
0.1400
0.2100
0.2700
0.3400
0.4100
0.4800
0.5500
0.6200
0.6800
0.7500
0.8200
0.8900
0.9600
1.0300
1.0900
1.1600
1.2300
1.3000
1.3700
1.4400
1.5000

0.02
0.02
0.02
0.02
0.02
0.02
0.02
0.02
0.02
0.000
0.0000
0.0000
0.02
0.1
0.44
0.78
0.66
0.3
0.14
0.06
0.02
0.02

bt.dat Notepad text editor Microsoft


Windows load Matlab
>>load bt.dat

load bt.q
bt.xxx
9

Ch.1 Computations with Matlab

02212471
Semester I - 2011

>>load bt

load bt.mat .mat default Matlab


bt matrix 222 bt
x_freq y_ampli ( 1.4)
>>x_freq=bt(:,1)
>>y_amplitude=bt(:,2)
>>plot(x_freq,y_ampli,or,x_freq,y_ampli,b)
>>xlabel(Frequency)
>>ylabel(Amplitude)

1.4 bt.dat

10

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.7 (Matrix Operations)


(Mathematical operations)
(conformable)

+
*
/ or \

(addition)
(subtraction)
(multiplication)
(division)

e.g., >> A+B


e.g., >> A-B
e.g., >> A*B
e.g., >> A/B or A\B for right division or
left division

inv( )

det( )

(matrix inversion) e.g., >> Cinv


(transposition) e.g., >> C
(determinant) e.g., >> det(C)

= inv(C)

1.7.1


>> A=[1 2 3; 4 5 6]
A =
1 2 3
4 5 6
>> B=[7 8 9; 10 11 12]
B =
7 8 9
10 11 12
>> A+B
ans =
8 10 12
14 16 18
>> A-B
ans =
-6 -6 -6
-6 -6 -6


>> 2*A
ans =
2 4 6
8 10 12

11

Ch.1 Computations with Matlab

02212471
Semester I - 2011

Matlab sin, cos, exp, log, sqrt


(on each element)

>> sqrt(A)
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495

-
1.1
1.1
Properties of Matrix Addition and Scalar Multiplication
1. A + B = B + A
2. A + (B + C) = (A + B) + C
3. A + 0 = 0 + A = A
0 is the zero matrix
4. c(A + B) = cA + cB
5. (a + b)C = aC + bC
6. a(bC) = (ab)C
1.7.2
(inner dimensions) CD = Cm x n Di x j
n = i m j ( outer dimensions)
A2 x 3 B3 x 2 Matlab
b1 b4
a1 a 2 a3

A=
, B = b2 b5
a
a
a
5
6
4
b3 b6
(a1b1 + a 2 b2 + a3b3 ) (a1b4 + a 2 b5 + a3b6 )
A B =

(a 4 b1 + a5 b2 + a6 b3 ) (a 4 b4 + a5 b5 + a6 b6 )
>> A*B
??? Error using ==> *
Inner matrix dimensions must agree.

A2 x 3 B2 x 3 Matlab Error
B transpose B A
12

Ch.1 Computations with Matlab

02212471
Semester I - 2011

>> BT=B'
BT =
7 10
8 11
9 12
>> A*BT
ans =
50 68
122 167

1.2
1.2
Properties of Matrix Multiplication
1. A(BC) = (AB)C
2. A(B + C) = AB + AC
3. (A + B)C = AC + BC
4. AIn = InA = A
5. c(AB) = (cA)B = A(cB)

In is the identity matrix

AB BA (not
communicative)
1.7.3 (Multiplication on an element-by-element basis)

A A A A^2
conform Matlab dot
operation ( dot product )
>> A^2
>> A.^2
>> A.*B
ans =
7
40

%Performs the usual matrix multiplication A*A


%Squares each element of the matrix A

16
55

27
72

1.7.4 transpose
transpose A, AT A
1.3 transpose

13

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.3
Properties of Transpose
1. (A + B) = A + B
2. (cA) = cA
3. (AB) = BA
***
4. (A) = A
transpose (square matrix)
Symmetric matrix symmetric matrix
mirror main diagonal line
2
5

5
-4

0
1
-4

1
7
8

-4
8
3

1.7.5 (The Inverse of a Matrix)


(multiplicative inverse of
a real number) 1/4 (:= b) inverse 4 (:= a) (1/4)4 = 4(1/4) = 1
ab = 1 ba = 1
A B In identity matrix
AB = BA = In
A Nonsingular matrix B
A A-1
AA-1 = A-1A = In
A A Singular matrix

(eqn. 1.1)

inverse matrix 2 (1) Cofactor (2) Row Reduction


Cofactor
Matrix Algebra
- Inverse Matrix Row Reduction
inverse matrix nonsingular matrix A row
reduction A identity matrix, In inverse A
14

Ch.1 Computations with Matlab

02212471
Semester I - 2011

4 10
A=

10 30

identity matrix, In inverse


A identity matrix row reduction ( Gauss-Jordan
) In inverse A
- [A] [In]
1. 4 10 1 0
2.

10 30 0 1

1
2
.
5
0
.
25
0

10 30 0
1

3.

1 2.5 0.25 0
0 5 2.5 1

4.

1 2.5 0.25 0
0 1 0.5 0.2

5.

1 0 1.5 0.5
0 1 0.5 0.2

- Row1 1
1 4 (Row1)/4
- Row1 Column1
0 Row2 (10 x Row1)
- Row 1
(Row2)/5
- upper triangular 0
Row1 (2.5 x Row2) [In][A]-1

[A] [A]-1 In
4 10 1.5 0.5 6 + (5) 2 + 2 1 0
10 30 0.5 0.2 = 15 15 5 + 6 = 0 1

inverse matrix 3 x 3
row reduction inverse matrix
Matlab inv()
singular matrix inverse Matlab

.

>> A=[1 4;8 9];


>> inv(A)
ans =
-0.3913
0.1739
0.3478 -0.0435
>> B=[1 2;2 4];
>> inv(B)
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
ans =
Inf
Inf
Inf
Inf

15

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.7.5
matrix
X/Y = X * 1/Y = X * Y-1 / Y
* Y-1 Matlab function 2
- / ( slash) MATLAB right matrix division
A / B = A * B-1
- Matlab
A scaling
A

- \ ( back slash) MATLAB left matrix division


- B A
A \ B = A-1 * B
B * A-1
B A-1

left division (system of linear


equations)
[A]{x} = {b} Ax = b
solutions unknown {x} x
A A-1
A-1Ax = A-1b
Ix = A-1b
x = A-1b

-identity matrix, I
(eqn. 1.2)

A-1b solutions Matlab A-1b A\b



1.7.6 determinant
determinant scalar
determinant (square matrix) determinant A
det A | A | 1.3 determinant 2 x 2
a11 a12
A=

a 21 a 22
16

Ch.1 Computations with Matlab

02212471
Semester I - 2011

| A | = a11a22 a21a12
3 x 3 determinant (1.4)
b11 b12 b13
B = b21 b22 b23
b31 b32 b33
b22 b23
b21 b23

| B | = b11

b32

b33

b12

b31 b33

+b13

b21 b22
b31 b32

(eqn. 1.3)

(eqn. 1.4)

4 x 4 determinant
...
determinant square matrix a Matlab det() determinant
singular matrix determinant Matlab
>> a=[1 2 3; 2 7 6; 5 4 8];
>> det(a)
ans =
-21

1.8 Matlab
Matlab Numerical Method
Matlab Command window

Matlab
Command
window Matlab Matlab 2
1.8.1 (Script M-files)
Matlab
(*.m) M-file ASCII text file M-file

(text) editor
data bt.dat
text editor Notepad M-file editor Matlab plotbt.m
17

Ch.1 Computations with Matlab

02212471
Semester I - 2011

%% file name: plotbt.m


%% M-file for plotting data in bt.dat
%%
load bt.dat
x_freq=bt(:,1)
y_ampli=bt(:,2)
plot(x_freq,y_ampli,or,x_freq,y_ampli,b)
xlabel(Frequency)
ylabel(Amplitude)

plotbt.m dir ls Matlab command prompt


run enter
>> plotbt.m

1.4
code

command window

Pythagoras
% filename : pytha.m
% my Pythagoras
a = input(Please input the first side );
b = input(Please input the second side );
c = sqrt(a^2 + b^2);
disp(The third side = );
disp(c);

M-file
Control Flow
For Loops, While Loops If-Else-End control flow
control flow

18

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1. For Loops
execute
file

M-

clear;
%this clears all variables from the workspace
n=10;
beta=zeros(n,1); %Create an nx1 vector to hold Beta vector
for i=1:n
%set # of times to execute the following commands
beta(i,1)=i+1; %formula for the ith element of beta
end
%end the For loop
beta
%print beta in the command window

2. While Loops
execute (Control expression)

clear;
b=0;t=0;
while 2^b<200
b=b+1;
t=t+2^b;
end
b
t

%Enter the initial values of variables b and t


%Enter the controlling expression
%These two lines are the commands to be executed
%End the While Loop
%show the value of b
%show the value of t

3. If-Else-End
logical expression If (true) execute
logical expression (false) else

clear;
b=randn;
if b>0
count=1;
else
count=0;
end
count

%pick b from the N(0,1) distribution


%The variable count will be equal to 1 if b>0
%count is zero otherwise
%show count in the command window

control flow (nested


structure)

19

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.8.2 (Function M-files)


M-file

(*.m)
input arguments output arguments
Matlab workspace workspace command window
function save file
.m myfunc.m
function a = myfunc(c,d)

a
c, d

output scalar matrix


input parameter

function[a,b,c] = yourfunc(c,d,e,f)

function file yourfunc (save yourfunc.m) input d, e, f g


a, b c output
sine
sinedeg sinedeg.m
% filename : sinedeg.m
% This function computes Sine with degree input
function x = sinedeg(deg)
rad = deg*pi/180;
x=sin(rad);

command prompt
>> sinedeg(30)
ans =
0.5000

20

Ch.1 Computations with Matlab

02212471
Semester I - 2011

1.9 (Closures)
Matlab
Matlab Command window
Matlab
quit exit
clc
clf
clear
save
edit
Ctrl + c

Matlab
Command Window
Graphic Window

harddisk
M-file (.m)
Matlab

Current Directory Search Path


M-file command prompt Matlab
Matlab Current Directory Search Path
- dir ls
current directory
- pwd
current directory
- cd <dir>
directory
- path(path, dir) search path >> path(path, z:\)
command line ellipsis continuation
symbol 3
>> a = [1, 2, 3, ...
4 ,5, 6]

(help)
- help Matlab function help
- help function Matlab function
- helpwin Help Windows
- demo Matlab
*****
21

You might also like