Lab No. 1 math (1)
Lab No. 1 math (1)
Lab Outcomes:
To become familiar with the basics of Matlab.
Theory:
What is MATLAB
The name MATLAB is short for MATrix LABoratory. It is a commercial software
package whose main function is to perform calculations on matrices, row vectors and
column vectors. It is widely used in both industry and academic institutions. It possesses
many of the features of a high level, numerically oriented programming language, but in
addition has a large collection of built-in functions for performing matrix and vector
operations in a way that is very simple for the user. For example, to find the determinant
of a matrix A one need only enter:
det(A)
MATLAB commands can be entered one line at a time, or the user can write programs of
MATLAB code and define his or her own functions. In this session, we shall only use the
one-line-at-a-time “interpretive” mode for simplicity, but the regular user will find that
the power of the package is considerably extended by writing his or her own programs
and functions.
Entering and quitting MATLAB
To enter MATLAB, simply double click on the MATLAB icon. To leave MATLAB and
return to the PC’s operating system
Simply type: quit
Creating and manipulating matrices and vectors
MATLAB works on matrices and vectors that may be real or complex. Variables do not
have to be “declared”. Results can be displayed in a variety of different formats.
Let us enter a row vector into the MATLAB workspace. Type in:
v = [1 2 3 4]
This creates a variable v whose current value is a row vector with four elements as
shown. After pressing “return” the value of v will have been echoed back to you. To
suppress the echo, one uses a semi-colon following the command line. Thus
w = [5 6 7 8];
Creates another row vector w, but does not echo. To check that w has appeared in the
MATLAB workspace, type,
who
Which will display all the variables in the MATLAB workspace, and to check the value
of w simply type w
Operations on row vectors can be best illustrated by some simple exercises.
Equipment:
1) PC
2) MATLAB Version 7 or above
Procedure:
(i) z’ (b) z*v (c) [v; w] (d) v*z (e) [z; v’] (f) z + v’
(a) N = inv(M) (b) M*N (c) I = eye(2) (d) M + I (e) M*z(1:2) (f) v(3:4)*M
(g) M(1,1) (h) M(1:2,1:2) (i) M(:,1) (j) M(2,:)
Observations:
EXPERIMENT 1:
(a) v(2)
>> v=[1:4]
v=
1 2 3 4
>> v(2)
ans =
>> w=[5:8]
w=
5 6 7 8
(b) sum = v + w
6 8 10 12
(c) diff = v – w
-4 -4 -4 -4
(d) vw = [v w]
1 2 3 4 5 6 7 8
(e) vw(2:6)
2 3 4 5 6
(f) v’
1
2
3
4
EXPERIMENT 2:
(i) z’
z=[2;2;3;3]
z=
2
2
3
3
>> z'
ans =
2 2 3 3
(b) z*v
2 4 6 8
2 4 6 8
3 6 9 12
3 6 9 12
(c) [v; w]
>> [v;w]
ans =
1 2 3 4
5 6 7 8
(d) v*z
ans =
27
ans =
2
2
3
3
1
2
3
4
(f) z + v’
z+v'
ans =
3
4
6
7
EXPERIMENT 3:
(a) N = inv(M)
m=[4 4;2 1]
m=
4 4
2 1
>> N=inv(m)
N=
-0.2500 1.0000
0.5000 -1.0000
(b) M*N
>> m*N
ans =
1 0
0 1
(c) I = eye(2)
>> I=eye(2)
I=
1 0
0 1
(d) M + I
>> m+I
ans =
5 4
2 2
(e) M*z(1:2)
>> m*z(1:2)
ans =
16
6
(f) v(3:4)*M
>> v(3:4)*m
ans =
20 16
(g) M(1,1)
>> m(1,1)
ans =
4
(h) M(1:2,1:2)
m(1:2,1:2)
ans =
4 4
2 1
(i) M(:,1)
>> m(:,1)
ans =
4
2
(j) M(2,:)
>> m(2,:)
ans =
2 1
Conclusion:
References:
Solved by using Matrix on MATHLAB.