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

Lab 2

The document provides an overview of working with matrices in MATLAB. It describes how to define matrices, access matrix elements, perform basic arithmetic operations on matrices, and use MATLAB's built-in matrix functions. It also demonstrates how to solve systems of linear equations using matrices and MATLAB commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Lab 2

The document provides an overview of working with matrices in MATLAB. It describes how to define matrices, access matrix elements, perform basic arithmetic operations on matrices, and use MATLAB's built-in matrix functions. It also demonstrates how to solve systems of linear equations using matrices and MATLAB commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab-2

Working with Matrices in Matlab


2.1 Objective

• To give overview related to operating with matrices


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

2.2 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.

Table 2.1 Matlab Built-in Matrix Functions


2.3 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 elementwise, 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:

Figure 2.1

Then, the system of equations can be solved by pre-multiplying by the inverse of the first matrix to
solve x1 and x2:

Figure 2.2

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
2.4 Tasks

2.4.1 Define 2-D Matlab array of 5 elements each and perform the four basic arithmetic
operations on it. Paste the out of the results below

>> a = [1 2 3 4 5; 6 7 8 9 0]

a=

1 2 3 4 5

6 7 8 9 0

>> a+2

ans =

3 4 5 6 7

8 9 10 11 2

>> a-2

ans =

-1 0 1 2 3

4 5 6 7 -2

>> a.*2

ans =

2 4 6 8 10

12 14 16 18 0

>> a./2

ans =

0.5000 1.0000 1.5000 2.0000 2.5000

3.0000 3.5000 4.0000 4.5000 0

2.4.2 As you have defined 2-D array previously access elements on following indexes:

• First row fourth column.


• Second row first column
• First row third column
• All rows of second column
>> a(1,4)

ans =

>> a(2,1)

ans =

>> a(1,3)

ans =

>> a(:,2)

ans =

2.4.3 Make two 2-D matrices of your own choice and perform following operations on it:

• Multiply one matrix with zeros matrix of same size


• Divide one matrix with one matrix of same size
• Addition of one matrix with random number matrix
• Transpose and inverse of two matrices

>> a = [1 2 3 4 5; 6 7 8 9 10]

a=

1 2 3 4 5

6 7 8 9 10

>> b = [10 9 8 7 6; 5 4 3 2 1]

b=

10 9 8 7 6

5 4 3 2 1

>> c = zeros(2,5)

c=

0 0 0 0 0

0 0 0 0 0
>> d = rand(2,5)

d=

0.8147 0.1270 0.6324 0.2785 0.9575

0.9058 0.9134 0.0975 0.5469 0.9649

>> a.*c

ans =

0 0 0 0 0

0 0 0 0 0

>> a./b

ans =

0.1000 0.2222 0.3750 0.5714 0.8333

1.2000 1.7500 2.6667 4.5000 10.0000

>> b+d

ans =

10.8147 9.1270 8.6324 7.2785 6.9575

5.9058 4.9134 3.0975 2.5469 1.9649

>> transpose(a)

ans =

1 6

2 7

3 8

4 9

5 10

>> transpose(b)

ans =

10 5

9 4

8 3

7 2

6 1
Inverse can’t be taken. Because MATLAB demands a matrix must be a square matrix so that its inverse
can be calculated.

>> a = [2 5; 7 4]

a=

2 5

7 4

>> inv(a)

ans =

-0.1481 0.1852

0.2593 -0.0741

>> b = [8 3; 9 6]

b=

8 3

9 6

>> inv(b)

ans =

0.2857 -0.1429

-0.4286 0.3810

You might also like