Professional Documents
Culture Documents
Gkogi 0340
Gkogi 0340
Introduction:
The purpose of this exercise is to introduce MATLAB to students, its basic commands and functions.
MATLAB originally stood for Matrix Laboratory. Today it is regarded as high-performance language for
technical computing. This software incorporates many features that are used for many complex
mathematical computations, simulations and modeling.
A =
-2 3 1
5 7 -1
-9 0 2
1
1. Define the following matrices in MATLAB. Note that the variable names are case sensitive.
Q1.1. Write the commands that you issued to define the matrices above.
clc
clear all
% Matrix B
B=[4 1 0;1 3 2;0 2 5]
% Matrix C
C=[0 2 -8;-2 0 6;8 -6 0]
% Matrix D
D=[2;1;4]
% Matrix E
E=[9 0 5]
% Matrix F
F=[3 2;4 1]
% Matrix G
G=[0 -2;4 5]
% Matrix H
H=[3 0 2;4 0 1]
% Matrix J
J=[6 1 -5;5 -2 13]
Q1.2 Place a semi colon after defining one of the matrices above. What happens?
The matrices won’t appear if a semi-colon is placed after a matrix. There would be no output
because the semi-colon suppressed the output.
Q1.3 Which of the matrices defined above is/are row matrice/s? Column matrice/s? Square
matrice/s?
Matrices B, C, F, G are square matrices, D is a column matrix, and E is a row matrix
2
Part 2 – Matrix operation
The following are MATLAB symbols for matrix operation
Operator Function
Element-by-element addition and subtraction, respectively. Matrices to be
+ or -
added or subtracted should be of the same dimension
Matrix multiplication and division, respectively. Matrices to be multiplied or
* or / divided should be conformable. If one of the operands is a scalar, the
operation becomes scalar.
^ Matrix exponentiation. One of the operands must be scalar
Element-by-element multiplication, division and exponentiation, respectively.
.* or ./ or .^ Matrices to be operated on an element-by-element basis should be of the
same dimension.
size(A) Determines the dimensions of matrix A.
A’ Determines the transpose of matrix A.
inv(A) Determines the inverse of matrix A. The matrix should be non-singular.
det(A) Evaluate the determinant of matrix A.
Q2.1 Using the matrices defined in part 1, perform the indicated operation. Record the output.
a. A*B, B*A b. H + J, H - J
Ans=[-5 9 11; 27 24 9; -36 -5 10] Ans=[9 1 -3; 9 -2 14]
Ans=[-3 19 3; -5 24 2; -35 14 8] Ans=[-3 1 7; -1 2 -12]
3
Q2.2 Which operations above are invalid (i.e. those that returned error messages)? Explain why
each errors are returned.
Operations I because it is an incorrect dimension for matrix multiplication. While operation F for
det(D) and det(F) comes up as error because they are not considered square matrices only rows
and columns.
>> M = 3 + 4i
The table below summarizes other functions of MATLAB and their syntax
Operation Function
sin(M),
Obtains the value of sine, cosine and tangent of M. M is assumed to be in radians.
cos(M),
M can be a complex number.
tan(M)
exp(M) Computes the value of . M can be a complex number.
log(M) Computes the logarithm of complex number M to the base e.
sinh(M),
cosh(M), Computes the hyperbolic sine, cosine and tangent of a complex number M.
tanh(M)
4
real(M),
Determines the real and imaginary parts of complex number M, respectively.
imag(M)
abs(M), Determines the magnitude and angle in radians of complex number M,
angle(M) respectively.
pi The value of constant
1. 2.
X+JY= 23.000-14.000i X+JY=(-9.6e+1)+(2.8e+02i)
r<θ=26.9258<-31.3287 r<θ=17.5983<173.4744
3. 4.
X+JY=0.1600-0.1200i X+JY=-7.000-26.000i
r<θ= 0.2<-36.8699 r<θ=26.9258<-1.8338
5. Re and
Re(z1^3)= -44
(Re(z1))^3= 64
3. 4.
X+JY=0.000+11.5487i X+JY=9.6667-2.7991i
5.
X+JY=7.7411-26.1865i
5
be defined. For example to plot the line for , a row vector of x from -10 to 10
must be defined. You can do that by constructing a row matrix from -10 to 10 but a shorter way to do it is
through the use of increment operator. Type the line below in MATLAB and see the output
>> x = [-10:10]
Using the plot function, a line graph can be generated. Typing the line below in MATLAB’s workspace
>> plot(x,y)
30
20
10
-10
-20
-30
-40
-10 -5 0 5 10
6
Q4.3 What features are added to the plot when the following commands are issued?
a. grid on
- a grid is added on the plot
b. axis([-10 10 -35 35])
- the line “steeps” by making a limit between -10 to 10 only
c. xlabel(‘x’)
- a label beneath the graph is added entitled “X”
d. ylabel(‘y’)
- a label besides the graph is added entitled “Y”
e. title(‘Line’)
- a title appears above the graph entitled “Line”
f. clf
- the graph is cleared
Q4.4 What happens when you change the plot command to the following?
a. plot(x,y,’r’)
- a line emerges from -10 and reaches 10 and the line is color red.
b. plot(x,y,’--')
- the line changes from a straight line to a dashed one
c. plot(x,y,’—o’)
- the code results into a line with “o”s and a line is passed
through them
d. plot(x,y,’-.xg’)
- the line that results is a broken line with X’s that appears.
Q4.4 Using the plot function, obtain the graph of the functions and .
Plot the curves on the space provided below.
7
8