Uploaded by Rahafnaserd

Handout-03

advertisement
COE213 – Introduction to Programming with MATLAB
Prof. Naveed Bin Rais
AJMAN UNIVERSITY
College of Engineering and Information Technology
LAB 03 (Solving Equations)
Lab objectives:
After you finish this lab, you will be able to do the following:
1. Learn the use of MATLAB in solving equations
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Introduction:
Before we start, there are some important manipulations that we can perform using MATLAB:
1) Use of colon operation (:)
Along with the use of colon operator (:) in extracting rows or columns of a matrix that we learnt in the
previous lab, the colon operator (:) can be used to convert a matrix into a column vector. Consider the
matrix A as given below:
2) linspace() function
The function linspace can be used as an alternate to (:) operator to generate the intervals between two
values given.
This statement generates a row vector y of 100 points linearly spaced between and including a and b.
2
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
This statement generates a row vector y of n points linearly spaced between and including a and b.
For example, the statement:
Generates 1000 intervals divided equally between 0 and 2π.
3) Deleting a row or a column
We can delete a row or a column of a matrix by assigning that row (or column) an empty vector [] as
under:
Exercise 1: Restore the matrix A in its original form using the current matrix A.
4) Continuation
In order to continue writing a command on the next line, we can use three dots (…). For example,
consider the definition of Matrix below:
3
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Solving Linear Equations:
Suppose we need to solve the following expression:
The coefficient matrix A and the vector B from the equations can be written as:
With matrix notation, a system of simultaneous linear equations can be written as:
Ax = b
And we solve these equations for the variable x as under:
x = A-1b
Solving Linear Equations in MATLAB:
First Method:
Second Method:
4
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Solving Other Equations:
Suppose we want to compute the value of y as per the following equation:
where a = 5, x =2 and y = 8
Solving Logarithms:
log (x)
log10 (x)
log2 (x)
National Logarithm
Common Logarithm
Base 2 Logarithm
Another way to computer log2 from log10 is given in the following. Suppose, you want to compute log2(16)
from log10() function. We can achieve this by typing the following:
>> log10(16) / log10(2)
Some Common Matrix Operations:
Some common matrix operations that can be used to solve linear equations are given in Table 1:
Table 1
5
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Exercise 2: Assume that matrix_1 is defined as shown below:
Perform the following operations on this matrix:
a. Generate any 2x3 matrix from matrix_1.
b. Generate any 4x3 matrix from matrix_1.
c. Generate any 3x3 matrix from matrix_1.
d. Generate any 4x4 matrix from matrix_1.
e. Generate a matrix where the rows of matrix_1 are reversed (the last becomes first and so on)
f. Generate a matrix where the columns of matrix_1 are reversed (the last becomes first and so on)
g. Delete the second row of matrix_1.
h. Add the following row to matrix_1 as the second row: [5 10 15 20 25]
i. Delete the last column of matrix_1.
j. Add the following column to matrix_1 as the first column: [6 12 18 24]
k. Generate a matrix where each element of matrix_1 is multiplied by 5.
l. Generate a matrix where each element of matrix_1 is squared.
m. Determine the rank of the matrix A
Exercise 3: Let A = [3, 6, 9], B=[10, 20, 40], C=[8, 12], D=[16, 28]
Create the following matrix by concatenation the above matrices:
Exercise 4: Solve the following set of linear equations:
2x + y + 3z = 1
2x + 6y + 8z = 3
6x + 8y + 18z = 5
Exercise 5: Solve the following set of linear equations:
2x + y + z = 2
–x + y – z = 3
x + 2y + 3z = –10
6
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Exercise 6: Consider the following square matrix:
Determine the following:
a.
b.
c.
d.
Determinant of B
Inverse of B
Rank of B
Eigen Vectors of B
Exercise 7: Consider a signal over the time interval [-10, 10] with an increment period of 0.1, implement
the following equations:
a.
b.
c.
Exercise 8: Implement the following equation in MATLAB using operation on matrices:
ā„Ž(šœƒ) = š‘„0šœƒ0 + š‘„1šœƒ1 + š‘„2šœƒ2 + ā‹Æ + š‘„nšœƒn
where
n = 5,
Assume arbitrary values of š‘„i and šœƒi. You are not required to use operator ‘+’ to solve this problem.
7
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Answers to Exercises and Observations:
Exercise 1:
>> B=[1 2 3; 4 5 6]
B=
1
4
2
5
3
6
>> A=[B; 7 8 9]
A=
1
4
7
2
5
8
3
6
9
Exercise 2:
>> matrix_1= [ 22 8 12 24 10; 14 34 18 6 4; 20 32 5 6 28; 3 17 33 42 26]
matrix_1 =
22 8 12 24 10
14 34 18 6 4
20 32 5 6 28
3 17 33 42 26
>> matrix_1(1:2,1:3)
ans =
22
14
8 12
34 18
>> matrix_1(1:4,1:3)
ans =
8
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
22 8 12
14 34 18
20 32 5
3 17 33
>> matrix_1(2:4,3:5)
ans =
18
5
33
6 4
6 28
42 26
>> matrix_1(1:4,2:5)
ans =
8 12 24 10
34 18 6 4
32 5 6 28
17 33 42 26
>> matrix_1=flip(matrix_1)
matrix_1 =
3 17 33 42 26
20 32 5 6 28
14 34 18 6 4
22 8 12 24 10
>> matrix_1=flipud(matrix_1)
matrix_1 =
22
8 12
24
10
9
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
14 34 18 6 4
20 32 5 6 28
3 17 33 42 26
>> matrix_1=fliplr(matrix_1)
matrix_1 =
10
4
28
26
24
6
6
42
12 8 22
18 34 14
5 32 20
33 17 3
>> matrix_1(2,:)=[]
matrix_1 =
10
28
26
24 12 8 22
6 5 32 20
42 33 17 3
>> matrix_1(2,:)=[5 10 15 20 25]
matrix_1 =
10 24 12 8 22
5 10 15 20 25
26 42 33 17 3
>> matrix_1(:,5)=[]
matrix_1 =
10 24 12 8
5 10 15 20
26 42 33 17
10
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
>> matrix_1(1:4,1)=[6 12 18 24]
matrix_1 =
6 24
12 10
18 42
24 0
12 8
15 20
33 17
0 0
>> matrix_1*5
ans =
30 120 60 40
60 50 75 100
90 210 165 85
120 0 0 0
>> matrix_1.^2
ans =
36
144
324
576
576
100
1764
0
144
225
1089
0
64
400
289
0
>> x=rank(matrix_1)
x=
4
Exercise3:
>> A = [3, 6, 9], B=[10, 20, 40], C=[8, 12], D=[16, 28]
A=
11
COE213 – Introduction to Programming with MATLAB
3
6
Dr. Naveed Bin Rais
9
B=
10
20 40
C=
8
12
D=
16
28
>> matrix=[A C; B D; C A; D B]
matrix =
3 6
10 20
8 12
16 28
9 8 12
40 16 28
3 6 9
10 20 40
Exercise4:
>> A=[2 1 3; 2 6 8; 6 8 18]
A=
2
2
6
1 3
6 8
8 18
>> b=[1; 3; 5]
12
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
b=
1
3
5
>> x=inv(A)*b
x=
0.3000
0.4000
-0.0000
Exercise5:
>> A=[2 1 1; -1 1 -1; 1 2 3]
A=
2
-1
1
1 1
1 -1
2 3
>> b=[2; 3; -10]
b=
2
3
-10
>> x=inv(A)*b
x=
3
1
-5
13
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Exercise 6:
>> A=[ 5 7 1; 2 9 6; 3 8 4]
A=
5
2
3
7
9
8
1
6
4
>> A=[ 5 7 1; 2 9 6; 3 8 4]
A=
5
2
3
7
9
8
1
6
4
>> d=det(A)
d=
-1.0000
>> x= inv(A)
x=
12.0000 20.0000 -33.0000
-10.0000 -17.0000 28.0000
11.0000 19.0000 -31.0000
14
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
Exercise2:
>> rows =input('enter number of rows: ')
enter number of rows: 3
rows =
3
>> cols=input('enter number of coloms: ')
enter number of coloms: 5
cols =
5
number=input('provide any number from 1 to 100: ')
provide any number from 1 to 100: 44
number =
44
>> rand_matrix = randi([1,number],rows,cols)
rand_matrix =
36
40
6
41 13 43 43
28 25 7 22
5 43 43 36
>> rand_matrix >= number/2
ans =
1
1
0
1
1
0
0
1
1
1
0
1
1
1
1
>> larger_rand_matrix = randi([number/2,number],rows,cols)
larger_rand_matrix =
25
31
43
40 22
44 41
37 43
37 31
39 37
39 25
>> smaller_rand_matrix = randi([1,number/2],rows,cols)
smaller_rand_matrix =
16 2 16 1 17
1 3 7 10 18
7 19 21 9 5
15
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
>> floor_rand_matrix=floor(rand_matrix)
floor_rand_matrix =
36
40
6
41 13 43 43
28 25 7 22
5 43 43 36
>> ceil_rand_matrix=ceil(rand_matrix)
ceil_rand_matrix =
36
40
6
41 13 43 43
28 25 7 22
5 43 43 36
>> Rand_Mat=randi([0,number],rows,cols)
Rand_Mat =
22
20
29
31 30 5 15
33 29 22 26
12 7 43 10
>> disp(rows)
3
>> disp(cols)
5
>> disp(rand_matrix)
36 41 13 43 43
40 28 25 7 22
6 5 43 43 36
>> disp(larger_rand_matrix)
25 40 22 37 31
31 44 41 39 37
43 37 43 39 25
>> disp(smaller_rand_matrix)
16 2 16 1 17
1 3 7 10 18
7 19 21 9 5
>> disp(floor_rand_matrix)
36 41 13 43 43
40 28 25 7 22
6 5 43 43 36
>> disp(ceil_rand_matrix)
36 41 13 43 43
40 28 25 7 22
6 5 43 43 36
16
COE213 – Introduction to Programming with MATLAB
Dr. Naveed Bin Rais
>> disp(Rand_Mat)
22 31 30 5 15
20 33 29 22 26
29 12 7 43 10
17
Download