You are on page 1of 2

How to use matrix in matlab?

This assignment will help you to deal with matrixes in MATLAB.


For the given matrix equation:

Which can be written as :


Ax=b
Now A and b can be input as:
>> A = [2 -1 1; 1 2 3; 3 0 -1]
>> b = [8; 9; 3]
Example 1
Let us first enter some matrices into MATLAB:
>> A = [1, 2, 3, 4; 5, 6, 7, 8]
>> B = [8, 7, 6, 5; 4, 3, 2, 1]
We can add them by typing
>> A+B
ans =
9 9 9 9
9 9 9 9
Example 2
Enter the following matrices into MATLAB:
>> A = [1, 1; 1, 0]
>> B = [0, 1; 1, 1]
Now multiply them, simply by typing
>> A*B
ans =
1 2
0 1
What happens if we multiply B by A?
>> B*A
ans =
1 0
2 1
When you multiply two matrices A and B, it is often not true that AB = BA!

>> A-B
>> 2*A
>> A^3

(Subtracts two matrices.)


(Multiplies matrix A by a scalar 2.)
(Raises the matrix A to the third power, i.e., multiplies A by itself three times.)
(Multiplies the corresponding entries of the matrices together. We will not be using this command,
>> A.*B
but it is nice to know.)
>> rref(A) (Gives the reduced row echelon form of A.)
>> A'
(Gives the transpose of A, sometimes written as AT)
EXAMPLE 3
(a) Consider the system of equations:
2x1 + x2 + 5x3 = -1
x1 + 6x3 = 2
-6x1 + 2x2 + 4x3 = 3
On paper, convert this system of equations into a matrix equation of the form Cx = d.
(b) Enter the matrix C and the column vector d into MATLAB, and use the command
>> x = C\d
to check your solution.
(c) We would expect to get the column vector d in MATLAB if we ran the command C*x,
right? In other words, C*x - d should be zero. Enter this equation into MATLAB:
>> C*x-d
What do you get?