System Programming Lab Assignments by Gajendra
System Programming Lab Assignments by Gajendra
LAB - RECORD
V SEM
5EE4-24
Submitted To Submitted By
Dr. Virendra Sharma Gajendra Teli
Department of Electrical 20EAREE021
Engineering B.Tech Third Year
Electrical Engineering
ASSIGNMENT NO: 1
Question – 1 calculate: -
Solution: - A. x = 12;
1
Taking L.H.S >> cos(4*x)
Ans = -0.6401
Taking R.H.S. >> 8*(cos(x)) ^4 - 8*(cos(x)) ^2 +1
Ans = -0.6401
L.H.S. = R.H.S. Hence proved
B. >> x = 12;
Taking L.H.S >> tan(x/2)
Ans = -0.2910
Taking R.H.S. >> sqrt((1-cos(x))/(1+cos(x)))
Ans = -0.2910
L.H.S = R.H.S hence Proved
Question 5: - Define the two variables: alpha = 5π/8, and beta = π/8. Using these variables;
show the following trigonometric identity is correct by calculating the values of the left and
right sides of the equation.
¿ α −β
Sinα + sin β = 2sin (α + β ¿ 2 ) cos ( )
2
Solution: - >> r = [3, 4.2, 55, 68/16, 45, nthroot (110,3), cos (25), 0.5]
r=
3.0000 4.2000 55.0000 4.2500 45.0000 4.7914 0.9912 0.5000
Question 7: - Define the variable x = 0.85, y = 0.25, and then use them to create a column
vector that has the following elements: y, yx, ln (y/x), y.x, and y+x.
Solution: ->> x = 0.85;
2
>> y = 0.25;
>> c = [y; y^x; log(y/x); y*x; y+x;]
c=
0.2500
0.3078
-1.2238
0.2125
1.1000
Question 8: - use a single command to create a row vector (assign it to a variable named b)
with 19 element such that
b = 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
Do not type the vector explicitly
Solution: - >> 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
3
ASSIGNMET NO: 2
4
B. Creates a matrix G from all rows and the third through fifth columns of matrix E.
Solution: -
>>E= [0 0 0 0 2 2 2; 0.7 0.6 0.5 0.4 0.3 0.2 0.2; 2 4 6 8 10 12 14; 22 19 16 13 10 7 4]
E=
0 0 0 0 2.0000 2.0000 2.0000
0.7000 0.6000 0.5000 0.4000 0.3000 0.2000 0.2000
2.0000 4.0000 6.0000 8.0000 10.0000 12.0000 14.0000
22.0000 19.0000 16.0000 13.0000 10.0000 7.0000 4.0000
5
>>N=[33 21 9 14 30; 30 18 6 18 34; 27 15 6 22 38; 24 12 10 26 41]
N=
33 21 9 14 30
30 18 6 18 34
27 15 6 22 38
24 12 10 26 41
a. >>A=[N(1,1:4)',N(2,2:5)']
A=
33 18
21 6
9 18
14 34
b. >>B= [N (: ,3)', N (3, :)]
B=
9 6 6 10 27 15 6 22 38
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 4: - Using zeros, ones and eye commands, create the following arrays:
1 1 1 1 1 1
1 0 0 1 1
1 1 1 1 1 1
(a) (b) 0 1 0 1 1 (c)
0 0 0 0 0 0
0 0 1 1 1
0 0 1 1 1 1
Solution: - (a). >>a= [ones (2:2), zeros (2:2)]'
a=
1 1
1 1
6
0 0
0 0
(b). >>b=[eye(3,3),ones(3,3)]
b=
1 0 0 1 1 1
0 1 0 1 1 1
0 0 1 1 1 1
(c). >>c=[ones(2,4);zeros(1,4);ones(1,4)]
c=
1 1 1 1
1 1 1 1
0 0 0 0
1 1 1 1
Question 5: - Create a Matrix A in which the elements are 1. Then reassign A to itself
(several times) such that A will become.
1 1 0 0 1 1 0
1 1 0 0 1 1 0
0 0 1 1 0 0 1
0 0 1 1 0 0 1
Solution: - >>A=ones(4,8)
A=
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
>>A(1:2,3:4)=zeros(2,2)
A=
1 1 0 0 1 1 1 1
1 1 0 0 1 1 1 1
1 1 1 1 1 1 1 1
7
1 1 1 1 1 1 1 1
>>A(1:2,7:8)=zeros(2,2)
A=
1 1 0 0 1 1 0 0
1 1 0 0 1 1 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
>>A(3:4,1:2)=zeros(2,2)
A=
1 1 0 0 1 1 0 0
1 1 0 0 1 1 0 0
0 0 1 1 1 1 1 1
0 0 1 1 1 1 1 1
>>A(3:4,5:6)=zeros(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
8
ASSIGNMENT NO: 3
Question 1: - for the function y = 0.8x3 - 12x 2 + 0.75x, calculate the value of y for the
following value of x using element by element operation; -2, -1, 0, 2, 3, 4.
Solution: - >> x = [-2 -1 0 2 3 4]
x = -2 -1 0 2 3 4
>> y = 0.8.*x.^3 – 12.*x.^2 +0.75.*x
y=
-55.9000 -13.5500 0 -40.1000 -84.1500 -137.8000
2
( x −2)
Question 2: - for the function y = , calculate the value of y for the following values of
x+4
x using element by element operation; -3, -2, 0, 2, 3, 4.
Solution: - >> x = [-3 -2 0 2 3 4];
>> y = (x.^2 - 2)./ (x+4)
y=
7.0000 1.0000 -0.5000 0.3333 1.0000 1.7500
Question 3: - The following two vector are defined in MATLAB
v = [-3 5 2] u = [4 -2 6]
check your answer by executing the following commands with MATLAB.
(1) v*u (2) v*u’ (3) v’*u
Solution: - >> v = [-3 5 2];
>> u = [4 -2 6];
1. >> v.*u
Ans =
-12 -10 12
2. >> v*u'
Ans = -10
3.>> v'*u
Ans = -12 6 -18
20 -10 30
8 -4 12
9
Question 4: -Define x and y vectors x = [1 3 5 7 9] and y = [2 5 8 11 14], then use them in
the following to calculate z using elements by element calculation,
2
xy
(1). Z =
x+ y
(2) s = x (x2 – y) – (x – y)2
Solution: - >> x = [ 1 3 5 7 9];
>> y = [2 5 8 11 14];
1. >> z = x.* y ^ 2. / (x + y)
z=
1.3333 9.3750 24.6154 47.0556 76.6957
2. >> z = x. *(x.^2 - y) - (x - y). ^ 2
z=
-2 8 76 250 578
Question 5: - Solve the following system of three linear equation:
4x – 2.5y +3z = 22.2
6x – 4y – 2z = 16.6
2x + 4y -5.55z = -11.3
Solution: - >> A = [4 -2.5 3; 6 -4 -2; 2 4 -5.5]
A=
4.0000 -2.5000 3.0000
6.0000 -4.0000 -2.0000
2.0000 4.0000 -5.5000
>> B = [22.2; 16.6; -11.3]
B = 22.2000
16.6000
-11.3000
>> linsolve (A, B)
Ans =
3.1000
-0.8000
2.6000
10
Question 6: - Solve the following system of five linear equation:
-u + 0.5v + 2w – 1.75x + y = 9.5
3u + 12v – 9w + 1.5x – 6y = -4.5
3u -1.5v +w + 1.25x + 0.5y = -11.5
3u + 2v – w + 1.5x – 3y = -3.5
6u + 3v + 2w + x + 8y = -23.5
Solution: ->> A = [ -1 0.5 2 -1.75 1; 3 12 -9 1.5 -6; 3 -1.5 1 1.25 0.5; 3 2 -1 1.5 -3; 6 3 2 1 8 ]
A=
-1.0000 0.5000 2.0000 -1.7500 1.0000
3.0000 12.0000 -9.0000 1.5000 -6.0000
3.0000 -1.5000 1.0000 1.2500 0.5000
3.0000 2.0000 -1.0000 1.5000 -3.0000
6.0000 3.0000 2.0000 1.0000 8.0000
>> B = [9.5; -4.5; -11.5; -3.5; -23.5]
B=
9.5000
-4.5000
-11.5000
-3.5000
-23.5000
>> linsolve (A, B)
Ans =
-4.0000
2.5000
4.0000
1.0000
-2.0000
11
ASSIGNMENT NO: 4
2
(x +2)
Question 1: - plot the function f(t) = , for -2 <= x <= 6.
(3+2 x 2)
Solution: - >>x = -2:0.1:6;
>>y = (x+2).^2./(3+2*x.^2);
>>plot(x, y)
>>xlabel('x')
>>ylabel('f(x)')
>> grid on
3 cos ( x) 4x
Question 2: - Plot the function f(t) = −0.5 x - , for -2 <= x <= 6.
(0.5 x +e ) 7
Solution: - >> x = -2:.01:6;
>> y = 3*cos(x)./(0.5*x + exp(-0.5*x)) - 4*x/7;
12
>> plot(x,y)
13
Question 4: - Use fplot command to plot the function
15
ASSIGNMET NO: 5
16
>> y = 4*2>12/8+2>3^2
y =0 (logical)
C.>> % part c
>> y = 3*(2>10/20) + (3>5) ^2
y= 3
D.>> % part d
>> 5*5-4*7<=~2*5-2+~0
Ans = 1 (logical)
17
Ans =
1×9 logical array
1 0 0 1 0 1 0 1 1
D.>> u - (v<u)
Ans =
0 3 2 -2 0 -3 5 5 4
Question 4: - use the vector from the problem 3 use relation operators to create a vector y
that is made up of the elements of w that are larger than or equal to the elements of v:
Solution: - >> v = [6 -3 -2 7 0 3 -4 10 4];
>> u = [0 4 3 -2 0 -3 6 5 4;
>> y = v (v <= u)
y=
-3 -2 0 -4 4
Question 5: - Evaluate the following expression without using MATLAB. Check the answer
in MATLAB.
A. 0 & 21
B. ~ - 2 > -1 & 11 > = ~ 0
C. 4 – (7/2) & 6 < 5| - 3
Solution: - A. >> 0&21
Ans = 0 (logical)
B. >> ~-2>-1&11>=~0
Ans = 1 (logical)
C. >> 4-7/2&6<5|-3
Ans = 1 (logical)
ASSIGNMENT NO: 6
Question 1: - Plot the polynomial -0.19x4 + 0.61x3 + 4x2 - 12.5x - 31 in the domain -5 < x <5.
First create a vector for x, next use polyval function to calculate y, and then use the plot
function.
Solution: -
>> x = linspace ( -5, 5);
>> y = polyval ([-0.19 0.61 4 -12.5 -31], x);
>> plot (x, y)
18
Question 2: - Plot the polynomial y= -0.001x4 + 0.051x3 - 0.76x2 - 3.8x - l.4 in the domain I
x S14.First create a vector for x, next use polyval function to calculate y, and then use the
plot function.
Solution: - >> x = linspace (1, 14);
>> y = polyval ([-0.001 0.5 1 -0.76 -3.8 -1.4], x);
>> plot (x,y)
19
Question 3: - Use MATLAB to carry out the following multiplication of two polynomials:
(5x3 + 1.5x2 + 3) (2.4x3 -2x -18)
Solution: ->> p1 = [ 5 1.5 3];
>> p2 = [2.4 -2 -18];
>> p = conv (p1, p2);
>> disp(p);
12.0000 -6.4000 -85.8000 -33.0000 -54.0000
Question 4: -Use MATLAB to carry out the following multiplication of two polynomials:
(x + l .4) (x - 0.4) x (x + 0.6) (x - 1.4)
Solution: ->> p1 = [ 1 1.4 0 -0.4 0 0.6 -1.4];
>> p2 = [1 0 -1.4];
>> p = conv (p1, p2);
>> disp(p)
1.0000 1.4000 -1.4000 -2.3600 0 1.1600 -1.4000 -0.8400
1.9600
Question 5: -Use the vectors from problem 3. Use relational operators to create a vector y
that is made up of the elements of w that are larger than or equal to the elements of v:
Solution: - >> v = [ 1 3 5 6 7 9];
>> w = [ 2 4 6 8 10 12];
>> y = w (w >= v);
>> disp(y)
2 4 6 8 10 12
20
Question 6: -Determine the solution of the equation e0.6x - √2x = 5 by Numerical Analysis
methods.
21