0% found this document useful (0 votes)
6 views

Lab-2 BMS

The document provides an overview of working with matrices in MATLAB. It discusses defining and accessing matrix elements, built-in matrix functions, linear algebra operations like multiplication and inversion, and using matrices to solve systems of linear equations by inverting the coefficient matrix and multiplying it with the dependent variable vector.

Uploaded by

Kiran Iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab-2 BMS

The document provides an overview of working with matrices in MATLAB. It discusses defining and accessing matrix elements, built-in matrix functions, linear algebra operations like multiplication and inversion, and using matrices to solve systems of linear equations by inverting the coefficient matrix and multiplying it with the dependent variable vector.

Uploaded by

Kiran Iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab-2

Working with Matrices in Matlab


Objective

• To giver overview related to operating with matrices


• To understand built in matrix-functions
• To calculate drug concentrations by solving system of linear equations

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

Table 2.1 Matlab Built-in Matrix Functions

2.2 Linear Algebra Operations


As well as storing and accessing 2-D data, matrices allow us to perform a range of
different linear algebra operations. The following code illustrates the use of some of the common
MATLAB matrix operations.

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

You might also like