Sei sulla pagina 1di 19

Matlab = Matrix Lab.

The world around Matlab is a matrix


world with matrices and vectors.

>> y=7.0
y=
7
>> y=7.0;
>> help rand()
rand() not found.
>> help rand
RAND Uniformly distributed pseudorandom numbers.
R = RAND(N) returns an N-by-N matrix containing pseudorandom values
drawn
from the standard uniform distribution on the open interval(0,1).
RAND(M,N)
or RAND([M,N]) returns an M-by-N matrix. RAND(M,N,P,...) or
RAND([M,N,P,...]) returns an M-by-N-by-P-by-... array. RAND returns a
scalar. RAND(SIZE(A)) returns an array the same size as A.

>> x=rand(6)

yield a 6 6 random matrix

x=
0.8147
0.9058
0.1270
0.9134

0.2785
0.5469
0.9575
0.9649

0.9572
0.4854
0.8003
0.1419

0.7922
0.9595
0.6557
0.0357

0.6787
0.7577
0.7431
0.3922

0.7060
0.0318
0.2769
0.0462

0.6324 0.1576 0.4218 0.8491 0.6555 0.0971


0.0975 0.9706 0.9157 0.9340 0.1712 0.8235
>>>> x=rand(1,6)

A single vector with 6 elements in it

x=
0.6948 0.3171 0.9502 0.0344 0.4387 0.3816
>> y=magic(3)

A matrix whose elements (from 1 to n 2 ) form same


column, row, and diagonal sums.

y=
8
3
4

1
5
9

6
7
2

Notice!

And

sum(y) would give us the row vector 15

sum( y ) =

>> zeros(4)
ans =
0
0
0
0

0
0
0
0

0
0
0
0

>>
>> z=eye(5)
z=

0
0
0
0

i=n2

i =1

n 2 (n 2 + 1)
i=
2

15

15.

1
0
0
0
0

0
1
0
0
0

0
0
1
0
0

0
0
0
1
0

>>>> y=hilb(5)

0
0
0
0
1
% A Hilbert matrix

y=
1.0000
0.5000
0.3333
0.2500
0.2000

0.5000
0.3333
0.2500
0.2000
0.1667

0.3333
0.2500
0.2000
0.1667
0.1429

0.2500
0.2000
0.1667
0.1429
0.1250

0.2000
0.1667
0.1429
0.1250
0.1111

>>>> x=[1 2 3; 4 5 6; 7 8 9] %user designed


x=
1
4
7

2
5
8

3
6
9

Again!
>> a=[1, 3, 5];
>> b=[7, 8, 9];
>> c=[a b]
c=
1

>> d=[a;b]
d=
1
7

3
8

5
9

>>
>> a=[eye(2);zeros(2)]

append these as rows

append them as columns

a=
1
0
0
0

0
1
0
0

But
>> a=[eye(2) zeros(2)]
a=
1
0

0
1

0
0

0
0

>>
Some predefined constants
>> pi
ans =
3.1416
>> i
ans =
0 + 1.0000i
>> eps
ans =

2.2204e-016
>> Inf
ans =
Inf
>> NaN
ans =
NaN
For instance,
>> y=2+3*i
y=
2.0000 + 3.0000i
>> x=sqrt(y)
x=
1.6741 + 0.8960i

>>
>> format long
>> pi
ans =
3.141592653589793
>> eps
ans =

2.220446049250313e-016
In general, we could tweak formatting in this way:

format short
format long
format short e
format long e

fixed point with 4 decimal places (the default)


fixed point with 14 decimal places
scientific notation with 4 decimal places
scientific notation with 15 decimal places

>> who
Your variables are:
a ans x

y z

>>>> whos
Name

Size

a
ans
x
y
z

4x2
1x1
3x3
5x5
5x5

Bytes Class
64
8
72
200
200

double
double
double
double
double

>> % functions
>> a= magic(6)
a=
35 1
3 32
31 9
8 28
30 5
4 36
>> y=a'

6 26 19 24
7 21 23 25
2 22 27 20
33 17 10 15
34 12 14 16
29 13 18 11

This is a transpose

Attributes

y=
35 3 31 8 30 4
1 32 9 28 5 36
6 7 2 33 34 29
26 21 22 17 12 13
19 23 27 10 14 18
24 25 20 15 16 11
>> x=[1 2 3 4 5 6];
>> x
x=
1

>> y=x'
y=
1
2
3
4
5
6
Some tidy functions on vectors and matrices.
>>x =
-2

>> y=find(x)

get all indices where an element is non-zero.

y=
1

>> x=[6,3,9,11];
>> y=[14,2,9,13];
>> values = x(x<y)

report those elements in x which are < y

values =
6

11

>> indices = find(x<y)

indices =
1

>>
>>
>> y=magic(5)
y=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> [m,i]= max(y)
m=
23

24

25

21

22

i=
2

>> min(y)

report their positions

ans =
4

>> a=magic(5)
a=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> b=ones(5,1)
b=
1
1
1
1
1
Matrix addition, Matrix multiplication, Matrix division.
a. Two matrices of same sizes could be added, subtracted.
>> a=rand(4)
a=
0.4733
0.3517
0.8308
0.5853

0.5497
0.9172
0.2858
0.7572

>> b=rand(4)
b=

0.7537
0.3804
0.5678
0.0759

0.0540
0.5308
0.7792
0.9340

0.1299
0.5688
0.4694
0.0119

0.3371
0.1622
0.7943
0.3112

0.5285
0.1656
0.6020
0.2630

0.6541
0.6892
0.7482
0.4505

0.8868
1.0794
1.0801
1.0684

1.2823
0.5461
1.1698
0.3388

0.7080
1.2200
1.5273
1.3846

>> c=a+b
c=
0.6032
0.9205
1.3002
0.5972

>> d=rand(2)
d=
0.0838
0.2290

0.9133
0.1524

>> e=a+d
??? Error using ==> plus
Matrix dimensions must agree.
To multiply matrix A by matrix B from the right, A and B must be of
dimensions m x n and n x p, the inner two integer must be the same for
matrix multiplication.
>> a=[1 2 3; 4 -1 2; 3 1 -1]
a=
1
4
3

2 3
-1 2
1 -1

>> b= [3 2 1; 2 -1 4; -1 -1 2]

b=
3 2
2 -1
-1 -1

1
4
2

>> c=a*b
c=
4 -3 15
8 7 4
12 6 5
>> d=b*a
d=
14 5 12
10 9 0
1 1 -7
Note that, in general, a*b

b*a (non-commutative).

An inverse matrix, if it exists, has the following property: For any square
1
1
1
matrix A , A is its inverse such that AA = A A = eye(n) if A
is of dimension n n .
In Matlab,

A1 = inv( A )

a=
1
4
3

2 3
-1 2
1 -1

>> b=inv(a)

b=
-0.0250 0.1250 0.1750
0.2500 -0.2500 0.2500
0.1750 0.1250 -0.2250
>> a*b
ans =
1.0000
0.0000
0.0000

0 0.0000
1.0000
0
0 1.0000

>> b*a
ans =
1.0000 0.0000 0.0000
0 1.0000
0
0 -0.0000 1.0000
Consider now the division of a matrix by another. This could be done in
two ways. Let
>> a
a=
1
4
3

2 3
-1 2
1 -1

>> b
b=
2
1

1
2

-1
1

-1

-1

>> c1=a/b

matrix right division

c1 = AB 1

c1 =
0.3333 1.3333 1.0000
4.1667 -0.8333 3.5000
1.8333 -0.1667 0.5000
>> a*inv(b)
ans =
0.3333 1.3333 1.0000
4.1667 -0.8333 3.5000
1.8333 -0.1667 0.5000
>> c2=a\b

matrix left division

>> inv(a)*b

ans =
-0.1000 0.0500 0.5000
0
-0.5000
0
0.7000 0.6500 -0.5000
>>
c2 =
-0.1000 0.0500 0.5000
0.0000 -0.5000
0
0.7000 0.6500 -0.5000

c 2 = A 1 B

There are two types of operations we need to be concerned about.

Where an operation is meant to be scalar (element by element) op


where an operation is meant to be a matrix operation
(done above)
Here are some examples:
New operators are:
.* = (element by element scalar multiplication)
./ = (element by element scalar division)
.^ = (element by element scalar exponentiation)
>> x=[0.5 0.7 0.9];
>> y=sin(x).^3.*cos(x).^5

for

sin 3 x cos 5 x

y=
0.0574

0.0700

0.0446

But
>> 2+log(x)
ans =
1.3069

1.6433

Again. To compute

1.8946

x4 + 5 x 2

for each element of

>> y=x.^4+5*x-2
y=
0.5625

1.7401

3.1561

>> y=(x.^3-6)./(x.^2-7*x-9)

for

x3 6
x2 7 x 9

x , we have to do

y=
0.4796

0.4218

0.3638

>> y=1./(1+1./(1+1./x))

for

1+

y=
0.7500

0.7083

0.6786

>>a =
1
-2
-3

2
3
2

3
1
4

>> b=a.*a
b=
1
4
9

4
9
4

9
1
16

>> c=a*a
c=
-12
-11
-19

14
7
8

17
1
9

>> x=-2.0:0.5:1.5; % colon notation


>> x'

1
1+

1
x

ans =
-2.0000
-1.5000
-1.0000
-0.5000
0
0.5000
1.0000
1.5000
>> length(x)
ans =
8
>> k=a(2,3)
k=
7
>> a(2,:)
ans =
23

>> a(:,5)
ans =
15
16
22
3
9
>> a(2:4, 3:5)

14

16

ans =
7 14 16
13 20 22
19 21 3
>> a(1:2:5,:)
ans =
17 24 1 8 15
4 6 13 20 22
11 18 25 2 9
>> b=rand(5)
b=
0.7655
0.7952
0.1869
0.4898
0.4456

0.6463
0.7094
0.7547
0.2760
0.6797

0.6551
0.1626
0.1190
0.4984
0.9597

0.3404
0.5853
0.2238
0.7513
0.2551

0.5060
0.6991
0.8909
0.9593
0.5472

>> b([1 2],:)=a([1 2],:) % replacing the first two rows of b by that of a
b=
17.0000
23.0000
0.1869
0.4898
0.4456

24.0000
5.0000
0.7547
0.2760
0.6797

1.0000
7.0000
0.1190
0.4984
0.9597

8.0000
14.0000
0.2238
0.7513
0.2551

15.0000
16.0000
0.8909
0.9593
0.5472

Polynomials in Matlab.
A polynomial like y
the following form.

= 5 x6 + 3 x 2 14 x + 2

is expressed as a vector in

>> y=[5 0 0 0 3 -14 2];


If we want to find its roots, we do the following.
>> roots(y)

note that complex roots occur in pairs.

ans =
-1.0380 + 0.7788i
-1.0380 - 0.7788i
0.4037 + 1.1284i
0.4037 - 1.1284i
1.1210
0.1475
We can build a polynomial from its roots (the reverse process).
>> r=[1, -1, 2, -2, 6];
>> p=poly(r)
p=
1

-6

-5

30

4 -24

>> roots(p)
ans =
6.0000
-2.0000
-1.0000
2.0000
1.0000
polyval(a,x) evaluates a polynomial stored in a for a specific value of x.
>> p
p=

-6

-5

30

>> polyval(p,6.001)
ans =
1.1208

4 -24

Potrebbero piacerti anche