Na Lab Assignment
Na Lab Assignment
TECHNOLOGY, LAHORE
DEPARTMENT OF CIVIL ENGINEERING
1|Page
Table of Contents
CHAPTER NO.1: Starting with MATLAB.....................3
CHAPYER NO. 2 :Creating Arrays............................12
CHAPTER NO. 3 :Mathematical Operations with
Arrays.....................................................................32
CHAPTER NO 5: TWO DIMENSIONAL PLOTS............49
2|Page
CHAPTER NO.1: Starting with MATLAB
PROBLEMS:
The following problems can be solved by writing
commands in the Command Window, or by writing a
program in a script file and then executing the file
Question 1
Solution:
>> % Question 1
>> % Part (a)
>> ((14.8^2)+(6.5^2)/(3.8^2))+(55/(sqrt(2)+14))
ans =
225.5340
>> % Question 1
>> % Part (b)
>> (-3.5)^3+(exp(6)/log(524))+nthroot(206,3)
ans =
27.4611
3|Page
Question 2
Solution:
>> % Question 2
>> % Part (a)
>> ((16.5^2)*(8.4-sqrt(70)))/((4.3^2)-17.3)
ans =
7.6412
>> % Question 2
>> % Part (b)
>> (((5.2^3)-(6.4^2)+3)/((1.6^8)-2))+(13.3/5)^1.5
ans =
6.8450
Question 3
Solution:
>> % Question 3
>> % Part (a)
4|Page
>> 15*((sqrt(10)+3.7^2)/(log10(1365)+1.9))
ans =
50.2041
>> % Question 3
>> % Part (b)
>> ((2.5^3)*(16-(216/22)))/((1.7^4)+14))+nthroot(2050,4)
ans =
11.0501
Question 4 :
Solution:
>> % Question 4
>> % Part (a)
>> ((2.3^2)*1.7)/sqrt(((1-0.8^2)^2)+((2-sqrt(0.87))^2))
ans =
7.9842
Solution:
>> % Question 4
5|Page
>> % Part (b)
>> 2.34+((1/2)*(2.7)*((5.9^2)-(2.4^2)))+9.8*log(51)
ans =
80.0894
Question 5:
Solution:
>> % Question 5
>> % Part (a)
>> ((sin(7*pi/9))/((cos(5*pi/7))^2))+((1/7)*tan(5*pi/12))
ans =
2.1867
Solution:
>> % Question 5
>> % Part (b)
>> ((tand(64))/((cosd(14))^2))-
((3*sind(80))/nthroot(0.9,3))+((cosd(55))/(sind(11)))
ans =
2.1238
Question 6
6|Page
Define the variable x as x = 2.34, then evaluate:
Solution:
>> % Question 6
>> % Part (a)
>> x = 2.34 ;
>> 2*(x^4)-6*(x^3)+14.8*(x^2)+9.1
ans =
73.2258
>> % Question 6
>> % Part (b)
>> x = 2.34 ;
>> exp(2*x)/sqrt(14+(x^2)-x)
ans =
26.0345
Question 7
Solution:
7|Page
>> % Question 7
>> % Part (a)
>> t = 6.8 ;
>> log(abs((t^2)-(t^3)))
ans =
5.5917
>> % Question 7
>> % Part (b)
>> (75/2*t)*cos(0.8*t-3)
ans =
-194.7729
Question 8
Solution:
>> % Question 8
>> % Part (a)
>> x = 8.3;
>> y = 2.4;
>> (x^2)+(y^2)-((x^2)/(y^2))
ans =
62.6899
8|Page
>> % Question 8
>> % Part(b)
>> x = 8.3;
>> y = 2.4;
>> (sqrt(x*y))-(sqrt(x+y))+(((x-y)/(x-2*y))^2)-(sqrt(x/y))
ans =
2.1741
Question 9
Solution:
>> % Question 9
>> % Part (a)
>> a = 13;
>> b = 4.2;
>> c = 4*b/a ;
>> d = (a*b*c)/(a+b+c) ;
>> a*b/(c+d)+(d/c)*(a/b)-(a-b^2)*(c+d)
ans =
9|Page
43.5290
>> % Question 9
>> % Part (b)
>> (sqrt((a^2)+(b^2)))/(d-c)+log(abs(b-a+c-d))
ans =
7.8410
Question 12
Solution:
>> % Question 12
>> % Part (a)
>> x = pi/9 ;
% for LHS
>>sin(4*x)
ans=
0.9848
% for RHS
>> 4*sin(x)*cos(x)-8*sin(x)^3*cos(x)
ans=
0.9848
10 | P a g e
>> % Question 12
>> % Part (b)
>> LHS = cos(2*x)
LHS =
0.7660
>> RHS = (1-tan(x)^2)/(1+tan(x)^2)
RHS =
0.7660
Question 13
Solution:
>> % Question 13
>> % Part (a)
>> x = 12 ;
% For LHS
>> tand(4*x)
ans =
1.1106
%for RHS
>> (4*tand(x)-4*tand(x)^3)/(1-6*tand(x)^2+tand(x)^4)
11 | P a g e
ans=
1.1106
>> % Question 13
>> % Part (b)
>> x = 12;
>> LHS = sind(x)^3
LHS =
0.0090
>> RHS = (1/4)*(3*sind(x)-sind(3*x))
RHS =
0.0090
PROBLEMS:
Question 1
Solution:
>> % Question 1
>> R= [3 4*2.55 68/16 45 nthroot(110,3) cos(25) 0.05]
12 | P a g e
R=
3.0000 10.2000 4.2500 45.0000 4.7914 0.9063 0.0500
Question 2
Solution:
>> % Question 2
>> R = [54/(3+4.2^2) 32 (6.3^2)-(7.2^2) 54 exp(3.7) sin(66)+cos(3*pi/8)]
R=
2.6163 32.0000 -12.1500 54.0000 40.4473 1.2962
Question 3
Solution:
>> % Question 3
>> C = [25.5; (14*tan(58))/((2.1^2)+11); factorial(6); 2.7^4; 0.0375; pi/5]
C=
25.5000
1.4539
720.0000
53.1441
0.0375
0.6283
13 | P a g e
Question 4
Solution:
>> % Question 4
>> C2= [32/3.2^2; sind(35)^2; 6.1; log((29)^2); 0.00552; log(29)^2; 133]
C2=
3.1250
0.3290
6.1000
6.7346
0.0055
11.3387
133.0000
Question 5
Solution:
>> % Question 5
>> x = 0.85;
>> y = 12.5;
>> [y; y^x; log(y/x); y*x; x+y]
ans =
14 | P a g e
12.5000
8.5580
2.6882
10.6250
13.3500
Question 6
Solution:
>> % Question 6
>> a = 3.5; b = -6.4;
>> [a a^2 a/b a*b sqrt(a)]
ans =
3.5000 12.2500 -0.5469 -22.4000 1.8708
Question 7
Solution:
>> % Question 7
>> x = [2:5:37]
x=
2 7 12 17 22 27 32 37
Question 8
15 | P a g e
Solution:
>> % Question 8
>> x = linspace(81,12,9)
x=
81.0000 72.3750 63.7500 55.1250 46.5000 37.8750 29.2500 20.6250
12.0000
Question 9
Solution:
>> % Question 9
>> x = [22.5:-2.5:0];
>> B= transpose(A)
B=
22.5000
20.0000
17.5000
15.0000
12.5000
10.0000
7.5000
5.0000
2.5000
0
16 | P a g e
Question 10
Solution:
>> % Question 10
>> A = linspace(-21,12,15);
>> B= transpose(A)
B=
-21.0000
-18.6429
-16.2857
-13.9286
-11.5714
-9.2143
-6.8571
-4.5000
-2.1429
0.2143
2.5714
4.9286
7.2857
9.6429
12.0000
Question 11
Solution:
17 | P a g e
>> % Question 11
>> same(1:7)= -3
same =
-3 -3 -3 -3 -3 -3 -3
Question 12
Solution:
>> % Question 12
>> a= [ zeros(1,8),7.5]
a=
0 0 0 0 0 0 0 0 7.5000
Question 13
Solution:
>> % Question 13
>> b = [(1:1:10) (9:-1:1)]
b=
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
Question 16
18 | P a g e
Solution:
>> % Question 16
>> A = [(0:5:30); (600:-100:0); linspace(0,5,7)]
A=
0 5.0000 10.0000 15.0000 20.0000 25.0000 30.0000
600.0000 500.0000 400.0000 300.0000 200.0000 100.0000 0
0 0.8333 1.6667 2.5000 3.3333 4.1667 5.0000
Question 17
Solution:
>> % Question 17
>> X= [linespace(1,5,5); zeros(1,5); linspace(3,3,5)]
>> A=transpose(X)
19 | P a g e
A=
1. 0 3
2. 0 3
3. 0 3
4. 0 3
5 0 3
Question 18
Solution:
>> % Question 18
>> Anine(1:4,1:6) = 9
Anine =
9 9 9 9 9 9
9 9 9 9 9 9
9 9 9 9 9 9
9 9 9 9 9 9
Question 19
20 | P a g e
Solution:
>> % Question 19
>>A=zeros(3,5);
>> A(3,5)= 8
A=
1. 0 0 0 0
0 0 0 0 0
0 0 0 0 8
Question 20
Solution:
>> % Question 20
>> D(2:3,4:5)= 6
D=
0 0 0 0 0
21 | P a g e
0 0 0 6 6
0 0 0 6 6
Question 21
Solution:
>> % Question 21
>> E(2:4,3:5)= [1 2 3; 4 5 6; 7 8 9]
E=
0 0 0 0 0
0 0 1 2 3
0 0 4 5 6
0 0 7 8 9
Question 22
Solution:
>> % Question 22
>> F(2:4,3:5)= [1 10 20; 2 8 26; 3 6 32]
F=
22 | P a g e
0 0 0 0 0
0 0 1 10 20
0 0 2 8 26
0 0 3 6 32
Question 23
>> % Question 23
>> a = [7 2 -3 1 0]; b = [-3 10 0 7 -2]; c = [1 0 4 -6 5];
>> % Part (a)
>> X = [a; b; c]
X=
7 2 -3 1 0
-3 10 0 7 -2
1. 0 4 -6 5
23 | P a g e
Question 34
Solution:
>> % Question 34
>> M = [3:2:13; 15:-1:10; 1:1:3 1:1:3]
M=
3 5 7 9 11 13
15 14 13 12 11 10
1 2 3 1 2 3
A. =
5 7 9
14 13 12
B=
24 | P a g e
3 5 7 13
15 14 13 10
1 2 3 3
C. =
3 5 7 9 11 13
1 2 3 1 2 3
Solution:
>> % Question 35
>> N = [33 21 9 14 30; 30 18 6 18 34; 27 15 6 22 38; 24 12 10 26 42]
25 | P a g e
N=
33 21 9 14 30 30 18 6 18 34 27 15 6 22 38
24 12 10 26 42
>> % Part(a)
>> A = [N(1,1:4)',N(2,2:5)']
A=
33 18
21 6 9 18
14 34
>> % Part(b)
>> B = [N(:,3)' N(3,:)]
B=
9 6 6 10 27 15 6 22 38
>> % Part(c)
>> C(3:4,5:6) = N(2:3,4:5)
C=
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 18 34
0 0 0 0 22 38
Question 36
26 | P a g e
Solution:
>> % Question 36
>> v = 1:3:34
v=
1 4 7 10 13 16 19 22 25 28 31 34
>> M = reshape(v,3,4)
M=
1 10 19 28 4 13 22 31 7 16 25 34
>> M(2,:) = []
M=
1 10 19 28 7 16 25 34
>> M(:,3) = []
M. =
1. 10 28 7 16 34
>> N = ones(size(M))
N. =
1. 1 1
1 1 1
Question 37
27 | P a g e
Solution:
>> % Question 37
>> % Part (a)
>> a = ones(2,2);
>> b = zeros(2,2);
>> x = [a; b]
x=
1 1
1 1
0 0
0 0
>> % Question 37
>> % Part (b)
>> a = eye(3);
>> b = ones(3,3);
>> x = [a b]
x=
1 0 0 1 1 1
0 1 0 1 1 1
0 0 1 1 1 1
>> % Question 37
28 | P a g e
>> % Part (c)
>> a = ones(2,4);
>> b = zeros(1,4);
>> c = ones(1,4);
>> x = [a; b; c]
x=
1 1 1 1
1 1 1 1
1 0 0 0
2 1 1 1
Question 38
Solution:
>> % Question 38
>> % Part (a)
>> x = [eye(2); zeros(2,1);ones(2,2)]
x=
1 0 0 1 1
0 1 0 1 1
>> % Question 38
>> % Part (b)
29 | P a g e
>> a = zeros(2,2);
>> b = ones(2,2);
>> c = [a b];
>> d = zeros(1,4);
>> e = ones(1,4);
>> x = [c; d; e]
x=
0 0 1 1
0 0 1 1
1. 0 0 0
2. 1 1 1
>> % Question 38
>> % Part (c)
>> a = ones(4,2);
>> b = zeros(4,2);
>> c = ones(1,1);
>> d = zeros(3,1);
>> e = [c; d];
>> x = [a b e]
x=
1 1 0 0 1
1 1 0 0 0
1 1 0 0 0
1 1 0 0 0
30 | P a g e
Question 39
Solution:
>> % Question 39
>> A = eye(6)
>> A(1:3,4:6) = eye(3);
>> A(4:6,1:3) = eye(3);
>> A
A=
1 0 0 1 0 0
0 1 0 0 1 0
0 0 1 0 0 1
1 0 0 1 0 0
0 1 0 0 1 0
0 0 1 0 0 1
Question 40
31 | P a g e
Solution:
>> % Question 40
>> A = ones(2,2)
A=
1 1
1 1
>> A(3:4,3:4) = ones(2,2);
>> A(1:2,5:6) = ones(2,2);
>> A(3:4,7:8) = ones(2,2);
A=
1 1 0 0 1 1 0 0
1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1
0 0 1 1 0 0 1 1
32 | P a g e
CHAPTER NO. 3 :Mathematical Operations with
Arrays
PROBLEMS:
Question 1
Solution:
>> % Q=3.1
>> x= [-2:4];
>> y=x.^3-2*x.^2+x
y=
-18 -4 0 0 2 12 36
Question 2
33 | P a g e
Solution:
>> % Q=3.2
>> x=[-3:3];
>> y=(x.^2-2)/(x+4)
y=
0.4000
Question 3
Solution:
>> % Q=3.3
>> x=[1:7];
>> y=(x-3).*(x.^2+3)./x.^2
y=
-8.0000 -1.7500 0 1.1875 2.2400 3.2500 4.2449
Question 4
Solution:
34 | P a g e
>> % Q=3.4
>> t=[0:8];
>> e=2.7;
>> y=20.*t.^(2./3)./t+1-(t+1).^2./e.^(0.3.*t+5)+2./(t+1)
y=
NaN 21.9793 17.5061 15.3216 13.9463 12.9729
Question 8
Solution:
>> lenth_of_U=sqrt(((23.5).^2)+((-17).^2)+((6).^2)) lenth_of_U =
29.6184
>> % Q = 3.8(B)
>> x=23.5;y=-17;z=6;
>> v=[x.^2 y.^2 z.^2];
>> lenth_of_U=sqrt(sum(v))
35 | P a g e
lenth_of_U =
29.61
36 | P a g e
Question 9
Solution:
>> % Question 9
>> unit_vector = ([-8 -14 25])./sqrt(((-8)^2)+((-14)^2)+(25^2))
unit_vector =
-0.2689 -0.4706 0.8404
Question 10
Solution:
>> % Question 10
>> v = [3,-2,4]; u = [5,3,-1];
>> % Part (a)
>> v.*u
ans =
15 -6 -4
>> % Part (b)
>> v*u'
ans =
37 | P a g e
5
>> % Part (c)
>> v'*u
ans =
15 9 -3
-10 -6 2
20 12 -4
Question 11
>> % Question 11
>> u = [-3 8 -2]; v = [6.5 -5 -4];
>> % Part (a)
>> x = u.*v;
>> sum(x)
ans =
-51.5000
38 | P a g e
ans =
-51.5000
ans =
-51.5000
Question 12
Solution:
>> % Question 12
>> v = [2:2:10]
v=
2 4 6 8 10
>> % Part (a)
>> a = v.^-1
a. =
0.5000 0.2500 0.1667 0.1250 0.1000
39 | P a g e
>> % Part (c)
>> c = v./2
c. =
1 2 3 4 5
d. =
1 1 1 1 1
Question 13
Solution:
>> % Question 13
>> v = [5:-1:1]
v=
5 4 3 2 1
40 | P a g e
3125 256 27 4 1
d. =
4 3 2 1 0
Question 14
Solution:
>> % Question 14
>> x = [1:2:9]; y = [2:3:14];
>> % Part (a)
>> z = (x.*(y.^2))./(x+y)
z=
1.3333 9.3750 24.6154 47.0556 76.6957
41 | P a g e
Question 15
Solution:
>> % Question 15
>> p=2.3; w=5.67; t=[1:5]; x=[2.8:-0.3:1.6]; y=[4,7,10,13,17];
>> % Part (a)
>> T = ((p*(x+y).^2)*w)./y
T=
150.7540 168.1357 194.1022 222.7102 265.3920
S=
6.4149 6.9908 7.6646 8.3687 9.4152
Question 27
42 | P a g e
Solution:
>> % Question 27
>> A = [2 4 -1; 3 1 -5; 0 1 4];
>> B = [-2 5 0; -3 2 7; -1 6 9];
>> C = [0 3 5; 2 1 0; 4 6 -3];
>> % Part (a)
>> A+B
ans =
0 9 -1
0 3 2
-1 7 13
>> B+A
ans =
0 9 -1
0 3 2
-1 7 13
43 | P a g e
>> % Part (b)
>> A+(B+C)
ans =
0 12 4
1 4 2
3 13 10
>> (A+B)+C
ans =
0 12 4
2. 4 2
3. 13 10
ans =
10 35 20
25 10 -25
20 35 5
>> 5*A+5*C
ans =
10 35 20
25 10 -25
20 35 5
44 | P a g e
ans =
-11 16 32
-22 -33 -8
11 51 31
>> A*B+A*C
ans =
-11 16 32
-22 -33 -8
11 51 31
Question 28
Solution:
>> % Question 28
>> A = [2 4 -1; 3 1 -5; 0 1 4];
>> B = [-2 5 0; -3 2 7; -1 6 9];
>> C = [0 3 5; 2 1 0; 4 6 -3];
>> % Part (a)
>> A*B
ans =
-15 12 19
-4 -13 -38
45 | P a g e
-7 26 43
>> B*A
ans =
11 -3 -23
0 -3 21
16 11 7
ans =
100 81 -132
-178 -253 94
224 263 -164
>> (A*B)*C
ans =
100 81 -132
-178 -253 94
224 263 -164
ans =
-15 -4 -7
12 -13 26
19 -38 43
46 | P a g e
>> B'*A'
ans =
-15 -4 -7
12 -13 26
19 -38 43
ans =
0 0 -1
9 3 7
-1 2 13
>> A'+B'
ans =
0 0 -1
9 3 7
-1 2 13
Question 32
47 | P a g e
Solution:
>> % Question 32
>> A = [3 1.5 1 0.5 4; -2 1 4 -3.5 2; 6 -3 2 2.5 1; 1 4 -3 0.5 -2; 3 2 -1 1.5 -3]
A. =
3.0000 1.5000 1.0000 0.5000 4.0000
-2.0000 1.0000 4.0000 -3.5000 2.0000 6.0000 -3.0000 2.0000
2.5000 1.0000
1.0000 4.0000 -3.0000 0.5000 -2.0000
3.0000 2.0000 -1.0000 1.5000 -3.0000
B. =
-11.7500
19.0000
-23.0000
-1.5000
-3.5000
>> Z= A\B
Z=
-4.0000
2.5000
48 | P a g e
4.0000
1.0000
-2.0000
49 | P a g e
CHAPTER NO 5: TWO DIMENSIONAL PLOTS
Problems 1-7
Question 1
>> x=[-3:5]
x=
-3 -2 -1 0 1 2 3 4 5
>> f=((x+5).^2)./(4+3.*x.^2)
f=
0.1290 0.5625 2.2857 6.2500 5.1429 3.0625 2.0645 1.5577
1.265
>> plot(x,f,'-r*','linewidth',2,'markersize',10)
50 | P a g e
Question 2
>> x=[-5:10]
x=
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
>> e=2.71828;
>> f=(((5.*sin(x))./(x+e.^(-0.75.*x)))-(3.*x)./5)
f=
Columns 1 through 10
3.1278 2.6352 1.6912 -0.6320 -3.1667 0 2.2575 0.8451 -
1.5728 -3.3344
Columns 11 through 16
-3.9544 -3.8324 -3.7311 -4.1818 -5.1711 -6.2720
>> plot(x,f,'-b*','linewidth',2,'markersize',10)
51 | P a g e
Question 3
>> x=[0:3]
x=
0 1 2 3
>> e=2.71828;
>> f=(x+1).*(x-2).*(2.*x-0.25)-e.^x
f=
-0.5000 -6.2183 -7.3890 2.9145
>> plot(x,f,'-k*','linewidth',2,'markersize',10)
52 | P a g e
b)
>> x=[-3:6]
x=
-3 -2 -1 0 1 2 3 4 5 6
>> f=(x+1).*(x-2).*(2.*x-0.25)-e.^x
f=
-62.5498 -17.1353 -0.3679 -0.5000 -6.2183 -7.3890 2.9145 22.9020
27.0873 -74.4272
>> plot(x,f,'-k*','linewidth',2,'markersize',10)
53 | P a g e
Question 4
fplot(sqrt(abs(cos(3*x)))+(sin(4*x)).^2,(-2.2))
Question 5
54 | P a g e
fplot((exp(2.*sin(0.4*x)))*5. *cos(4*x) [-20 30])
Question 6
55 | P a g e