You are on page 1of 16

Lab # 1

LAB # 1(b): Basic Commands.

1) a = 4, b = 8
Description: in this command, we assign values (i.e. 4 and 8) to the variables (i.e. a and b).
Example: a = 4
a=
4
2) (a:b)
Description: This command shows all values between 4 and 8 of variables a and b.
Example: (a:b)
ans =
5 6 7 8 9 10 11
3) (a:n:b)
Description: This command shows all values between 4 and 8 with 0.5 break (n=0.5) of variables
a and b.
Example: n = 0.5;
(a:b)
ans =
Columns 1 through 5
5.0000 5.5000 6.0000 6.5000 7.0000
Columns 6 through 10
7.5000 8.0000 8.5000 9.0000 9.5000
Columns 11 through 13
10.0000 10.5000 11.0000
4) logspace(a,b)
Description: This command generates a row vector of 50 logarithmically spaced points between
decades 10^a and 10^b.
Example: logspace (1,1)
ans =
Columns 1 through 20
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
Columns 21 through 40
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1
Columns 41 through 50
10 10 10 10 10 10 10 10 10 10
5) logspace(a,b,n)
Description: This command generates n points between decades 10^a and 10^b.
Example: logspace(2,2,5)
ans =
100 100 100 100 100
6) linespace(a,b)
Description: This command generates a row vector y of 100 points linearly spaced between and
including a and b.
Example: linspace(2,2)
ans =
Columns 1 through 20
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Columns 21 through 40
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Columns 41 through 60
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Columns 61 through 80
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Columns 81 through 100
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7) linspace(a,b,n)
Description: This command generates a row vector y of n points linearly spaced between and
including a and b
Example: linspace(2,2,2)
ans =
2 2
8) ;
Description: By using this command the answer will not be shown or eco on task bar windows.
Example: (a:b);
A=3
ans =
3
9) fix(a)
Description: this command, Round any fractional value towards zero.
Example: fix(3.4)
ans =
3
10) ceil(a)
Description: this command, rounds the elements of “a” to the nearest integers
towards plus infinity.
Example: ceil(3.7)
ans =
4
11) floor(a)
Description: this command, rounds the elements of “a” to the nearest integers
towards minus infinity.
Example: floor(3.7)
ans =
3
12) round(a)
Description: this command, rounds towards nearest decimal or integer.
Example: round(3.7)
ans =
4
13) rem(a,b)
Description: this command, will give reminder after division of two numbers.
Example: rem(3,7)
ans =
3
14) mod(a,b)
Description: this command, will give modulus after division of two numbers.
Example: mod(7,7)
ans =
0
15) factorial(a)
Description: this command, returns the product of all positive integers less than or equal to
n , where n is a nonnegative integer value.
Example: factorial(7)
ans =
5040
16) abs(a)
Description: this command, gives absolute value of a.
Example: abs(7.555)
ans =
7.5550
17) sqrt(a)
Description: this command, gives square root of any number.
Example: sqrt(7.555)
ans =
2.7486
18) format short
Description: this command, Scaled fixed point format with 5 digits.
Example: format short
pi
ans =
3.1415
19) format long
Description: this command Set the output format to the long fixed-decimal format and display
the value of pi.
Example: format long
pi
ans =
3.141592653589793
20) who
Description: this command, list current variables used.
Example: who
ans =
a ans b n
21) whos
Description: whos is a long form of WHO. It lists all the variables in the current
workspace, together with information about their size, bytes, class, etc.
Example: whos
Name Size Bytes Class Attributes
a 1x1 8 double
ans 1x1 8 double
b 1x1 8 double
n 1x1 8 double
22) %
Description: this command is for comment any command or program.
Example: %a = b
23) clc
Description: this command, is for clearing the current window.
Example: clc
24) clear
Description: this command, is for clearing all the variables and functions from memory.
Example: clear

25) clear (a)


Description: this command, is for clearing the specific variables and functions from memory.
Example: clear(a)
ans =
ans b n

26) rand
Description: this command, Uniformly distributed pseudorandom numbers.
Example: rand
ans =
0.7060
27) rand(2)
Description: this command, Uniformly distributed pseudorandom numbers.
Example: rand(2)
ans =
0.0318 0.0462
0.2769 0.0971
28) randn
Description: this command, Uniformly distributed pseudorandom numbers.
Example: randn
ans =
1.4384
29) help command name
Description: this command, Display help text about command in Command Window.
Example: help logspace(a,b)
30) lookfor
Description: this command, lookfor Search all M-files for keyword.
Example: lookfor(‘logspace’)
ans =

logspace - Logarithmically spaced vector.

31) exit
Description: exit terminates MATLAB after running finish.m, if finish.m exists.
Example: exit
ans =
MATLAB will shut down.
32) quit
Description: quit terminates MATLAB after running the script FINISH.M.
Example: quit
ans =
MATLAB will shut down.

Predefined Variables
33) pi
Description: this command, give value of pi in return.
Example: pi
ans =
3.1416
34) ans
Description: ans Most recent answer.
Example: ans
ans =
3.1416
35) I j
Description: these commands, show values of I and j.
Example: I j
ans =

0.0000 + 1.0000i
Lab # 2
Creation and Operations on matrices and vectors
Vectors and Matrices:
MATLAB has been designed to work with matrices. A matrix is a rectangular object (e.g. a table) consisting
of rows and columns. A vector is a special type of matrix, having only one row or one column.
Creating matrices
clc; clear; close all
m=2,n=3;
A = [ 1 2 3; 4 5 6; 7 8 9]
A = [1,2,3;4,5,6;6,7,8]
A = [1 2 3
4 5 6
7 8 9]
S = [0:2:8]
A(n,m)
A(n:m)
diag(A)
A(:,n)
A(n,:)
A(:)
A'
A( 3 , : ) = []
A( : , 2 ) = []
repmat(5,3,2)
reshape(A,2,2)
zeros(n)
zeros(m,n)
ones(n)
ones(m,n)
eye(n)
eye(m,n)
fliplr(A)
flipud(A)
length(A)
size(A)
magic(4)
Output of Creating matrices:
A=
1 2 3
4 5 6
7 8 9
A=
1 2 3
4 5 6
6 7 8
A=
1 2 3
4 5 6
7 8 9
S=
0 2 4 6 8
ans =
8
ans =
Empty matrix: 1-by-0
ans =
1
5
9
ans =
3
6
9
ans =
7 8 9
ans =
1
4
7
2
5
8
3
6
9

ans =
1 4 7
2 5 8
3 6 9
A=
1 2 3
4 5 6
A=
1 3
4 6
ans =
5 5
5 5
5 5
ans =
1 3
4 6
ans =
0 0 0
0 0 0
0 0 0
ans =
0 0 0
0 0 0
ans =
1 1 1
1 1 1
1 1 1
ans =
1 1 1
1 1 1

ans =
1 0 0
0 1 0
0 0 1
ans =
1 0 0
0 1 0
ans =
3 1
6 4
ans =
4 6
1 3
ans =
2
ans =
2 2
ans =
16 2 3 13
5 11 10 8
4 7 6 12
4 14 15 1

Operation on matrices
A = [ 1 2 3; 4 5 6; 7 8 9]
B = [1,2,3;4,5,6;6,7,8]
C=A+B
D=A-B
E=A*B
F=A.*B
G=A/B
G=A\B
H=A./B
H=A.\B
I=2*A
I=0.5*B
Output of Operation on matrices:
A =
1 2 3
4 5 6
7 8
B =
1 2 3
4 5 6
6 7 8
C =
2 4 6
8 10 12
13 15 17

D =
0 0 0
0 0 0
1 1 1
E =
27 33 39
60 75 90
93 117 141
F =
1 4 9
16 25 36
42 56 72
G =
0.2381 1.9048 -1.1429
-1.1238 3.8095 -1.6857
2.8476 -7.6190 5.7714
G =
-3.1525 -3.1525 -3.1525
6.3050 6.3050 6.3050
-3.1525 -3.1525 -3.1525
H =
1.0000 1.0000 1.0000
1.0000 1.0000 1.0000
1.1667 1.1429 1.1250
H =
1.0000 1.0000 1.0000
1.0000 1.0000 1.0000
0.8571 0.8750 0.8889
I =
2 4 6
8 10 12
14 16 18
I =
0.5000 1.0000 1.5000
2.0000 2.5000 3.0000
3.0000 3.5000 4.0000
Vectors:
i) Create a row vector ‘a’ of length 10
ii) Create a column vector ‘b’ length of 10
iii) Change row vector into column vector
iv) Create a row vector ‘c’ that has the elements: 32, 4, 81, 63, cos(pi/3) and 14.12
v) Create a column vector ‘d’that has the elements: 55, 14, log(51), 987, 0 & 5sin(2.5pi)

Code:
a = [1:10]
b = [1;2;3;4;5;6;7;8;9;10]
a'
c = [32,4,81,cos(pi/3),14.12]
d = [55;12;log(51);987;0;5*sin(2.5*pi)]
Output:
a =
1 2 3 4 5 6 7 8 9 10
b =
1
2
3
4
5
6
7
8
9
10
ans =
1
2
3
4
5
6
7
8
9
10
c =
32.0000 4.0000 81.0000 0.5000 14.1200
d =
55.0000
12.0000
3.9318
987.0000
0
5.0000
Simple Math Operations
Code:
a=5; b=2; c=9;
a^2
c^3
a*b+c^2
a^3+b*c
Output of simple math operations:
ans =
25
ans =
729
ans =
91
ans =
143
Complex Numbers
Script:
w= 2 + 3*j; x= 5 - 7*i; y= 0.8 - i*6; z= j*0.9 +5
S= w+x
P= w-y
Q= z*x
R=y/x
Output of complex numbers:
z =
5.0000 + 0.9000i
S =
7.0000 - 4.0000i
P =
1.2000 + 9.0000i
Q =
31.3000 -30.5000i
R =
0.6216 - 0.3297i
Exercise
1) Calculate:
𝟑𝟓.𝟕∗𝟔𝟒−𝟕𝟑
a.
𝟒𝟓+ 𝟓𝟐
𝟓 𝟑𝟐
b. ∗ 𝟕 ∗ 𝟔𝟐 +
𝟒 (𝟗𝟑 −𝟔𝟓𝟐)
𝟐𝟕𝟑𝟐/𝟑 𝟓𝟓𝟐
c. (𝟐 + 𝟕)𝟑 + +
𝟐 𝟑

5) Create a row vector x = [2 3 5 7 11 13 17 19 23 29] in MATLAB.


Now use the colon operator to display only the even entries of the vector, that is, the output should be
the vector 3 7 13 19 29.
Now do the same thing but display the output in reverse order.
The output should be 29 19 13 7 3

6) Create a row vector in which the first element is 1, the last element is 33, with an increment of 2
between the elements (1,3,5,…….,33)

7) Create a row vector in which the first element is 15, the elements decrease with increments
of -5 and the last elements is -25
8) Create a Matrix T =

Use the matrix T to:


a) Create a five-element row vector named ‘va’ that contains the elements of the second row of T.
b) Create a three-element row vector named ‘vb’ that contains the elements of the fourth column of T.
c) Create a ten-element row vector named ‘vc’ that contains the elements of the first and second rows
of T.
d) Create a six-element row vector named ‘vd’ that contains the elements of the second and fifth
columns of T.
e) Create a three-element column vector named ‘ua’ that contains the elements of the third column of
T.
f) Create a five-element column vector named ‘ub’ that contains the elements of the second row of T.
g) c) Create a nine-element column vector named ‘uc’ that contains the elements of the first, third and
fifth columns of T.
h) Create a ten-element column vector named ‘ud’ that contains the elements of the first and second
rows of T.

Code:
(35.7*64-(7^3))/(45+(5^2))
(5/4)*7*(6^2)+((3^2)/(9^3-652))
((2+7)^3)+((273^(2/3))/2)+((55^2)/3)

x = 13.5;
(x^3)+5*(x^2)-26.7*x-52
(sqrt(14*x^3))/(exp(3*x))
log(abs((x^2)-(x^3)))

x = 9.6; z = 8.1;
x*(z^2)-((2*z)/3*x)^(3/5)
((443*z)/(2*x^3))+((exp(-x*z))/(x+z))

a = 15.62; b = -7.08; c = 62.5; d = 0.5*(a*b)-c


a+((a*b)/c)*(((a+d)^2)/(sqrt(abs(a*b))))
d*exp(d/2)+(((a*d+c*d)/((20/a)+(30/b)))/(a+b+c+d))

x = [2 3 5 7 11 13 17 19 23 29]
a = [ 1 2 3 4 5 6 7 8 9 10 ]
evenIndices = rem(a, 2) == 0
a = a(evenIndices)
a = x(:,a)
fliplr(a)

r = [1:2:33]

r = [15:-5:-25]

t = [ 6 43 2 11 87
12 6 34 0 5
34 18 7 41 9]
va = t(2,:)
vb = t(:,4)'
vc = [t(1,:) t(2,:)]
vd = [t(:,2) ; t(:,5)]'
ua = t(:,3)
ub = t(2,:)'
uc = [t(:,1) ; t(:,3) ; t(:,5)]
ud = [t(1,:) t(2,:)]'
Output:
ans =
27.740
=
315.1169
ans =
1.7584e+03
ans =
2.9592e+03
ans =
4.7823e-16
ans =
7.7311
ans =
619.1704
ans =
2.0279
d =
-117.7948
ans =
-1.7409e+03
ans =
-66.5624
x =
2 3 5 7 11 13 17 19 23 29
a =
1 2 3 4 5 6 7 8 9 10
evenIndices =
1×10 logical array
0 1 0 1 0 1 0 1 0 1
a =
2 4 6 8 10
a =
3 7 13 19 29
ans =
29 19 13 7 3
r =
1 3 5 7 9 11 13 15 17 19 21
23 25 27 29 31 33
r =
15 10 5 0 -5 -10 -15 -20 -25
t =
6 43 2 11 87
12 6 34 0 5
34 18 7 41 9
va =
12 6 34 0 5
vb =
11 0 41
vc =
6 43 2 11 87 12 6 34 0 5
vd =
43 6 18 87 5 9
ua =
2
34
7
ub =
12
6
34
0
5
uc =
6
12
34
2
34
7
87
5
9
ud =
6
43
2
11
87
12
6
34
0
5

You might also like