Download A Tutorial on MATLAB Objective: To generate arrays in MATLAB

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Capelli's identity wikipedia , lookup

System of linear equations wikipedia , lookup

Linear least squares (mathematics) wikipedia , lookup

Rotation matrix wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Four-vector wikipedia , lookup

Determinant wikipedia , lookup

Jordan normal form wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Orthogonal matrix wikipedia , lookup

Matrix calculus wikipedia , lookup

Gaussian elimination wikipedia , lookup

Matrix multiplication wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Transcript
A Tutorial on MATLAB
1. Objective: To generate arrays in MATLAB.
Procedure: The numbers 0, 0.1, 0.2, ..., 10 can be assigned to the variable ‘u’ by typing
u=[0:.1:10]. To compute w=5sinu for u=0 to 10,
>> u = [0:.1:10];
>> w = 5*sin(u);
The single line, w=5*sin(u), computed the formula w=5sin(u) 101 times, once for each
element of u in the array, to produce an array w that has 101 elements.
Because you typed a semicolon at the end of each line in the session, MATLAB does not
display the results on the screen. However, the values are stored in the variables u and w if
you need them. You can see the seventh value by typing u(7). Moreover, you can see the w
values in the same way.
>> u(7)
ans=
0.6000
>> w(7)
ans=
2.8232
You can do the same thing in different ways. For example, you can use ‘linspace’
%linspace(starting_point,end_point, number_of_step) or linspace(starting_point, end_point, length)
>> u = linspace(0,10,101);
You can use the length function to find out how many elements are in an array. For example,
>> m = length(w)
m=
101
You can find out the size of an array by using size function. For example,
>> n = size(w)
n=
1
101
EXERCISE: Use MATLAB to determine the size of arrays and how many elements are in the
array. By using MATLAB determine the 25th element.
1.1 K=0, 0.2, . . ., 20 and L=10cosK
1.2 M=0,1, 2, . . ., 5 and N=5cosM
1.3 H=2, 4, . . ., 180 and R=
1.4 [cos(0): 0.02: log10(100)]
2. Objective: To find the roots of a polynomial according to polynomial’s coefficients and to find
the polynomial’s coefficient by using roots in MATLAB.
1
Procedure: In MATLAB, polynomials can be described by using polynomial’s coefficients,
starting with the coefficient of the highest power of variable. For example, the polynomial
can be represented by the array a=[4, -8, 7, -5] . Moreover, polynomial
roots can be found with the roots(a) function where a is the polynomial’s coefficient array. The
result is represented with a column array that contains the polynomial’s roots.
For example; find the roots of
>> a = [1, -7,
>> roots(a) %
ans =
3.0000 +
3.0000 1.0000
40, -34];
you can use only roots([1,-7,40,-34]) without a=[1,-7, 40,-34]
5.000i
5.000i
The roots are x=1, x=3+5i and x=3-5i
The poly(r) function computes the coefficients of the polynomial which are specified by the
array r. For example, to find the polynomial whose roots are 1, 3+ 5i, 3- 5i the session is
>> r = [1, 3+ 5i, 3- 5i];
>> poly(r) % or you can use only poly([1,3+ 5i,3- 5i]) without r=[1,3+ 5i,3- 5i]
ans =
1
-7
40
-34
Thus the polynomial is
.
EXERCISE: Use MATLAB to find the roots of the polynomials. Then, use the poly function
verify the answers.
2.1
2.2
2.3
2.4
3. Objective: To decide magnitude of the numbers and compare the numbers according to their
magnitudes in MATLAB.
Procedure: In MATLAB, there are six relational operations to make comparison between
arrays. These operators are shown in Table 1. The result of a comparison using the relational
operator is either as 0 (if the comparison is false), or a 1 (if the comparison is true), and the
result can be used as a variable. For example, if x=2 and y=5, typing z=x<y returns the value
z=1, because x is less than y. Typing u=x==y returns the value u=0 because x does not equal
y.
Table 1: Relational Operators
Relational Operator
Meaning
<
Less than
<=
Less than or equal to
2
>
Greater than
>=
Greater than or equal to
==
Equal to
~=
Not equeal to
For example, suppose that x=[6, 3, 9] and y=[14, 2, 9]. The following MATLAB session shows
some examples.
>> z
z=
1
>> z
z=
0
>> z
z=
1
>> z
z=
0
>> z
z=
0
= (x < y)
0
0
= (x > y)
1
0
= (x ~= y)
1
0
= (x == y)
0
1
= (x > 8)
0
1
EXERCISE: Use MATLAB to find the z and examine the results as in the example above.
3.1 Suppose that x=[-9, -6, 0, 2, 5] and y=[-10, -6, 2, 4, 6]. What is the result of the following
operations?
a. z = (x < y)
b. z = (x > y)
c. z = (x ~= y)
d. z = (x == y)
e. z = (x > 2)
3.2 Suppose that x=[-4, -1, 0, 2, 10] and y=[-5, -2, 2, 5, 9]. What is the result of the following
operations?
a. z = (x <=y)
b. z = (x >= y)
c. z = (x ~= y)
d. z = (x == y)
e. z = (x > 0)
4.
Objective: Plotting a function, plotting multiple functions in the same figure in MATLAB.
Procedure: Let us plot the function y=sin(2x) for
. The increment step of x is
chosen 0.01 to generate more x values in order to produce a smooth curve. The function
3
plot(x,y) generates a plot with the x values on the horizontal axis and y values on the vertical
axis .
The session is,
>> x = [0 : 0.01 : 10];
>> y = sin(2*x);
>> plot(x,y)
and to assign a name for x and y axes;
>> xlabel('x axis name'), ylabel('y axis name')
>> title ('name of graph')
Moreover, to draw two graphs in one figure, you may use hold on .For example,
>>
>>
>>
>>
>>
>>
x = [0 : 0.01 : 10];
y = sin(2*x);
z = cos(2*x);
plot(x,y)
hold on
plot(x,z)
and you may change the plot-line features (as color, dash line,…) in plot function. For example,
>> x = [0 : 0.01 : 10];
>> y = sin(2*x);
>> plot(x,y,'k--') %k represents the colour and -- represents the type of line
or
>> plot(x,y,'m-o') %m represents the colour and -o represents the type of line
And if you want to specify the lines according to plots which appears in a box, you may use
legend function
. For example,
>>
>>
>>
>>
>>
>>
>>
x = [0 : 0.01 : 10];
y = sin(2*x);
z = cos(2*x);
plot(x,y)
hold on
plot(x,z)
legend('First plot’s features', 'Second plot’s features',2);
EXERCISE:
4.1 Use the MATLAB to plot the functions and assign the name of axis’s and titles.
4.1.1
a. Step size 1
b. Step size 0.1
c. Step size 0.01
4.1.2
4
a. Step size 0.2
b. Step size 0.5
c. Step size 0.01
4.1.3
a. Step size 0.5
b. Step size 0.05
4.2 Use MATLAB, to plot different functions given below in one figure for each sections a, b,
c, and d.
a.
b.
c.
d.
5. Objective: To create matrices by entering row/column elements and by forming from vectors.
Procedure: The most direct way to create a matrix is to type the matrix row by row, separating
elements in a given row with spaces or commas and separating the rows with semicolons. For
example, typing
>> A = [2, 4, 10; 16, 3, 7]
A =
2
4 10
16 3 7
You can also create a matrix from row or column vectors.
>> a = [1, 3, 5]
a =
1 3 5
>> b = [7, 9, 11]
b =
7 9 11
>> c = [a b]
c =
1 3 5 7 9 11
>> D = [a; b] %D=[[1,3,5];[7,9,11]] gives the same result
D =
1 3 5
7 9 11
EXERCISE:
5.1 Create a
a. 2x2 identity matrix,
b. 3x3 ones matrix,
c. 2x3 zeros matrix,
d. 4x4 diagonal matrix,
5.2 Create a matrix by using the vectors below;
5
a.
b.
c.
d.
Create 3x4 matrix by using A, B, C matrices.
Create 3x4 matrix by using B, C, D matrices.
Create 3x4 matrix by using A, B, D matrices.
Create a 4x4 matrix.
6. Objective: Basic Matrix operations such as summation, subtraction, etc. Taking the transpose
of a matrix.
Procedure: MATLAB allows matrix arithmetic operations: +, -, *, and ^ to be carried out on
matrices. Thus,
A+B or B+A is valid if A and B are of the same size
A*B
is valid if A’s number of column equals B’s number of rows
A^2
is valid if A is square and equals A*A
k*A or A*k
multiplies each element of A by k.
For example;
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> B = [1, 1, 1; 2, 2, 2; 3, 3, 3];
>> A + B
ans =
2
3
4
6
7
8
10 11 12
>> A - B
ans =
0
1
2
2
3
4
4
5
6
>> A^2
ans =
30
36
42
66
81
96
102 126 150
>> A*2 %2*A gives the same result
ans =
2
4
6
8
10 12
14 16 18
The transpose operation interchanges the rows and columns and' notation takes the transpose
of the matrix or vector in MATLAB. For example;
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A =
1
4
7
2
5
8
3
6
9
1
2
3
4
5
6
7
8
9
>> A'
ans =
6
EXERCISE:
6.1 Calculate the basic matrix operations with following matrices;
a.
b.
c.
d.
e.
A+B,
A-B,
AxB and BxA,
Ax2 and 2xA,
B2 and A3
6.2 Take the transpose of matrices below;
a.
b.
c.
d.
7. Objective: Array operations such as element-by-element multiplications, and so on.
Procedure: Dot (.) operator before an operator such as ', ^, * represents the array
operations for the matrix operations. The list of array operators is shown below in Table 2. If A
and B are two matrices of the same size with elements A=[aij] and B=[bij], then the command
Table 2: Array Operators
Array Operator
Meaning
.*
element-by-element multiplication
./
element-by-element division
.^
element-by-element exponentiation
>> C = A.*B
produces another matrix C of the same size with elements cij=aij bij . For example,
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> B = [2, 4, 6; 1, 3, 5; 7, 9, 8];
>> C = A.*B
C =
2
8
18
4
15 30
49 72 72
>> D = A./B
D =
0.5000
0.5000
0.5000
4.0000
1.6667
1.2000
1.0000
0.8889
1.1250
>> E = A.^B
E =
1
4
823543
16
125
134217728
729
7776
43046721
7
EXERCISE:
7.1 Calculate an element-by-element multiplication with
7.2 Calculate an element-by-element division with
8. Objective: To generate elementary matrices.
Procedure: The matrix of zeros, the matrix of ones, and identity matrix returned by the
functions zeros, ones, and eye respectively.
Table 3: Elementary Matrices
Operator
Meaning
eye(m,n)
mxn matrix with 1 on the main diagonal
eye(n)
nxn square identity matrix
zeros (m,n)
mxn matrix of zeros
ones (m,n)
mxn matrix of ones
diag(A)
Extract the diagonal of matrix A
rand(m,n)
mxn matrix of random numbers
For example;
>> b=ones(3,1)
b =
1
1
1
>> b=ones(1,3)
b =
1
1
>> b=ones(3)
b =
1
1
1
1
1
1
1
1
1
1
EXERCISE:
8.1 Produce elementary matrices,
a. 3x3 identity matrix,
b. 3x2 zero matrix,
c. 2x5 matrix and its all elements are 1,
d. 4x4 matrix and its all elements are random,
8