Lab-2 BMS
Lab-2 BMS
2.1 Matrices
At its simplest, a MATLAB array is a one-dimensional (1-D) list of data elements. Matrices
can also be defined, which are two-dimensional (2-D) arrays. In this case we use semi-colons to
separate the rows in the matrix, for example:
>> a = [1 2; 3 4];
>> b = [2, 4; 1, 3];
As with 1-D arrays, the row elements of matrices can be delimited by either spaces or commas.
Note also that we can press <RETURN> to move to a new line in the middle of defining an array
(either 1-D or 2-D). MATLAB will not process the array definition until we have closed the array
with the character.
MATLAB matrix elements can be accessed in the same way as 1-D array elements, except that
two indices need to be specified, one index for the row and one index for the column:
>> a(1,2)
>> b(2,2)
If we want to access an entire row or column of a matrix we can use the colon operator
>> a(:,2)
>> b(1,:)
MATLAB also provides several built-in functions specifically intended for use with matrices, and
these are summarized in Table 2.1.
.
1
c=a∗b
d=a+b
e = inv(a)
f = transpose(b)
Here, the ∗ and + operators automatically perform matrix multiplication and addition because
their arguments are both matrices. To explicitly request that an operation is carried out element-
wise, we use a ‘.’ before the operator. For example, note the difference between the following
two commands,
c=a∗b
d = a .∗ b
Here, d is the result of element-wise multiplication of a and b whilst c is the result of carrying out
matrix multiplication. Note that element-wise addition/subtraction are the same as matrix
addition/ subtraction so there is no need to use a dot operator with + and -.
Finally, matrices can also be used to solve systems of linear equations such as:
Then, the system of equations can be solved by pre-multiplying by the inverse of the first matrix
to solve x1 and x2:
The following MATLAB code implements the solution to the system of linear equations given
above. Enter the commands in MATLAB to find the values of x1 and x2.
M = [5 1; 6 3];
y = [5; 9];
x = inv(M) * y