Docsity

MATLAB: Understanding Matrices and Vector Operations, Study notes of Engineering

Toggle sidebar
bg1
MATLAB is a software package that makes it easy to manipulate
matrices. Vectors can also be easil
y
handled as a s
p
ecial case.
yp
A matrix in MATLAB is just a 2-D array of numbers. Example:
>> A =[1 2; 3 5; 2 4; 6 7]
>>
A
=[1
2;
3
5;
2
4;
6
7]
A =
1 2
3 5
2 4
The dimensions of a matrix is (NxM) where N is the number of rows
and N is the number of columns. The matrix A above has
dimensions (4x2)
6 7
dimensions
(4x2)
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download MATLAB: Understanding Matrices and Vector Operations and more Study notes Engineering in PDF only on Docsity!

MATLAB is a software package that makes it easy to manipulatematrices. Vectors can also be easily handled as a special case.

y^

p

A matrix in MATLAB is just a 2-D array of numbers. Example:

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

The dimensions of a matrix is (NxM) where N is the number of rowsand N is the number of columns. The matrix A above hasdimensions (4x2)

dimensions (4x2)

We can add or subtract matrices by simply adding or subtracting eachelement in the arrays located at the same position. Example:

>> A = [ 1 2; 3 4]A =^1

2 3 4 >> B = [3 3; 5 5]B =^3

(^35 5) C A B >> C=A+BC =^4

5 8 9

To add matrices they must be of the same size

8

9

Example:

>> A = [ 2 3; 1 5];>> B =[2 2; 6 3];>> B =[2 2; 6 3];>> C=A*BC =^22

which comes from our multiplication rule

11

11

11

12

21

12

11

12

12

22

C^

A B

A B

C^

A B

A B

=^

+^

=^

+^

=^

+^

=^

+^

21

21

11

22

21

22

21

12

22

22

C^

A B

A B

C^

A B

A B

=^

+^

=^

+^

=^

+^

=^

+^

We can consider vectors to be just examples of matrices where one ofthe dimensions =1. We can either have row or column vectors:

>> v1 =[ 1 2 3 5]

(1x4) (row) vector

v1 =

>> v2 =[1;2;3;5]v2 =

(4x1) (column) vector

Now consider reversing the order:

>> v1 =[ 1 2 3 4];>> v2 =[1;2;3;4];>> v2 =[1;2;3;4];>> p= v2*v (4x1)

(1x4)

p = (4x1)

(1x4)

1

(4x4)

Here

2

1

mn^

m^

n

p^

v^

v

We can also multiply matrices with vectors if we follow the same rule:>> M=[1 2; 3 4]M =

1

2 1

2 3

4 >> x= [5;6]x =x^56

(2x2)

(2x1)

>> b= M*xb =^17

(2x1)

39

This is equivalent to the linear system of equations:

M^

x^

M^

x^

b

+^

11 1

12

2

1

21 1

22

2

2

M^

x^

M^

x^

b

M^

x^

M^

x^

b

+^

+^

If we want to get the same result for b as before but now in the form of arow vector we need to multiply x by the transpose of M, M

T , where

p y

y^

p^

,^

if^

11

12 M^

M

M^

⎡^

⎤

= ⎢^

⎥ 21

22 11

21 12

22

M^ T

M^

M M^

M

M^

M^

M ⎢^

⎥

⎣^

⎦

⎡^

⎤

= ⎢^

⎥

⎣^

⎦

(interchange rows and columns)

This works since for any two matrices or vectors A, B, where theproduct is defined we have

(AB)

T^ =B

TTA

so if we take the transpose of

11

12

1

1

M^

M^

x^

b

M^

M^

x^

b

⎡^

⎤ ⎡^

⎤^

⎡^

⎢^

⎥ ⎢^

⎥^

⎢^

⎣^

⎦ ⎣^

⎦^

⎣^

21

22

2

2

M^

M^

x^

b

⎣^

⎦ ⎣^

⎦^

⎣^

we get

[^

]^

[^

]

11

21

1

2

1

2

12

22 M^

M

x^

x^

b^

b

M^

M

⎡^

⎢^

⎣^

In MATLAB, the transpose is M

T^ =M

'

>> b=x*M'b =^17

same b as obtained originally

same b as obtained originallybut now in terms of a row vector

In solving Statics equilibrium problems, we will typically haveto solve a set of linear equations for a set of unknown forces ormoments. We can do this easily in MATLAB:Example Consider the following system of two equations forExample Consider the following system of two equations forthe two unknowns x

and x 1

2

C^

C^

b

⎡^

⎤ ⎡^

⎤^

⎡^

11

12

1

1

21

22

2

2

C^

C^

x^

b

C^

C^

x^

b

⎡^

⎤ ⎡^

⎤^

⎡^

⎢^

⎥ ⎢^

⎥^

⎢^

⎣^

⎦ ⎣^

⎦^

⎣^

If we define the C matrix of coefficients and the vector b in MATLABthen the solution is given by using a backslash operator:

>> C = [ 1 2; 3 7];>> b=[1;1];>> x =C\bx = 5.0000-2.