Saturday, June 11, 2011

Matrices in Matlab

Matrices in Matlab

Matrix manipulation is probably the most important part of Matlab. It is what most people will use it for. In this post we shall learn how to create matrices, perform basic manipulations, and to solve simultaneous equations using Matlab.

Creating a Matrix:- 

>> a=[1,4,-1;1,1,-6;3,-1,-1];
a

a =

     1     4    -1
     1     1    -6
     3    -1    -1

>>

Transposing the matrix :-

>> a'

ans =

     1     1     3
     4     1    -1
    -1    -6    -1

>>

Inverse of a Matrix:-

>> inv(a)

ans =

    0.0986   -0.0704    0.3239
    0.2394   -0.0282   -0.0704
    0.0563   -0.1831    0.0423

>>

>> a*inv(a)

ans =

    1.0000   -0.0000         0
   -0.0000    1.0000         0
    0.0000    0.0000    1.0000

>> The identity matrix as you would expect.

Now let's try and solve the given equation using Matlab:-

1) x + y + z =9
2)2x - 3y + 4z =13
3)3x + 4y + 5z=40

Create two matrices a & b:-

>> a=[1,1,1;2,-3,4;3,4,5]

a =

     1     1     1
     2    -3     4
     3     4     5

>> b=[9;13;40]

b =

     9
    13
    40

>>
This equation can be represented as 
AX=B
=>inv(A) * A * X= inv(A) * B multiplying both sides by inv(A)
=> X=inv(A)* B 

>> inv(a)*b

ans =

    1.0000
    3.0000
    5.0000

>>
which means 
x=1
y=3
z=5

Now, let's try it the other way round.

The same equation can be represented as X'*A'=B' where ' represents the Transpose.
multiplying both sides by inv(A')
=>X'*A'*inv(A')=B'*inv(A')
=>X'=B'*inv(A')


>> b'*inv(a')

ans =

    1.0000    3.0000    5.0000

which means 
x=1
y=3
z=5



No comments:

Post a Comment