You are on page 1of 48

MATLAB

Winter Training
Report

Submitted To:
Submitted By:
Dr. Bhavnesh Kumar
Singh Narula

Navtej
ICE - II
Roll

No. 492/IC/13

CERTIFICATE
This is to certify that the work presented in this report on winter training in
MATLAB, submitted by Navtej Singh Narula (Roll No. 492/IC/13) to the
Division of Instrumentation and Control Engineering, Netaji Subhas Institute
of Technology, New Delhi is a record of my own work carried out under the
supervision and guidance of Dr. S.K. Jha, ICE Dep.

(Navtej Singh Narula)


Roll No. 492/IC/13
Division of Instrumentation and Control Engineering
Netaji Subhas Institute of Technology
Azad Hind Fauj Marg, Sector-3
Dwarka, New Delhi-110 078

INDEX
1. Develop a LV program to add all the integer number from 1 to
10.
2. Plot a sine wave having exactly 4 cycles of data points (1V,
50Hz, =45o.)
3. Demonstrate through the LV code the properties of the
Amplitude Scaling, Time Scaling and Time Shifting for some
common signals. Also include the combined operations of
shifting and scaling.
4. Plot the function y = Mod( -1) on linear, semi-log and log-log
plot.
5. Use 3D plot to plot your own generated data. Hint: use 2D sine wave.
6. Plot two sine waves having different amplitudes and phases on a single chart.
7. Construct any 4 4 matrix, calculate sum of its rows and sum
of its columns. Con-struct transpose of its matrix. If 4 4
matrix was A and its transpose was A , then calculate A + A
and A A. Construct B matrix of 4 4 and calculate A + B
and A B. Calculate the determinant of the resulting matrix A
+ B. Now calculate the sum of its A + B diagonal elements.
8. Construct a plot between X, Y 1 and Y 2 on single plot
where X = (0, 2 ) with increment of /10
9. Create the given transfer functions.
10.
Develop a MATLAB program to convert temperature
reading from C to F and vice-versa.
11.
Design your own problems on (Validate the results you
obtained)
a. Dierentiation
b. Integration

12.
Plot all the standard signals like step, ramp, impulse and
sinusoids.
13.

Construct a series of numbers

14.
Develop a MATLAB program to add all the integer
number from 1 to 10.
15.
Develop a MATLAB program to add all the odd numbers
from 1 to 101, both numbers are inclusive.
16.
The model of RC circuit can be found from Kirchhos
voltage law and conservation of change. RC dy/dt + y = v(t).
Suppose the value of RC is 0.1 second. Use a numerical
method to find the free response for the case where the
applied voltage v is 0 and the initial capacitor voltage is y(0) =
2 volts.
17.
Consider a mechanical system with equation mx + bx
+ kx = 0 where m = 1kg, b = 3N sec/m and k = 2N/m.
Assume that at t = 0 , x(0) = 0.1m and x(0) = 0.05m/sec.
Obtain motion of mass m subjected to initial condition.
18.
Use MATLAB to compute the solution of following
equation:
5x + 7 x + 4x = sin(t)
19.
Using numerical method (ODE), obtain free response of
an RC circuit (RCy + y = v(t))where applied voltage v is zero
and initial capacitor voltage is y(0) = 2V, RC = 0.1s.
20.
Use MATLAB to compute and plot the solution of
following equation
10dy/dt + y = 20 + 7sin2t , y(0) = 15
21.

Create a matrix of zeros with 2 rows and 4 columns.


Create the row vector of odd numbers through 21,

22.
Find the sum S of vector L's elements. And form the
matrix.
23.
Create two dierent vectors of the same length and add
them.
subtract
them.
Perform
element-by-element
multiplication on them. Perform element-by-element division
on them. Raise one of the vectors to the second power. Create
a 3X3 matrix and display the first row of and the second
column.

24.
Generate a large chunk of data and compute Mean,
Standard Deviation and Variance, Mode, Moment around the
mean, RMS etc.
25.
Study Laplace transform and inverse Laplace transform
for some standard signals using MATLAB.
26.

Define
2

the

following

function

using

syms:

f ( x )=x e 5 x

Compute the integral, and the first and second derivatives of


the above function symbolically.
Define the following function using syms:
f ( x , y , z )=x 2 e y 5 z 2

Compute the integral with respect to x.

MODELS
1.To convert AC into DC using Half Wave Rectifier.
2.To convert AC into DC using Full Wave Rectifier.
3.To estimate current and voltages in a DC circuit.
4.To understand the working of AND & OR logical
operators when provided with two pulse wave
forms.

Problem 1
Develop a LV program to add all the integer number from 1 to 10.

Solution:

Problem 2
Plot a sine wave having exactly 4 cycles of data points (1V, 50Hz,
=45o.)

Solution

Problem 3
Demonstrate through the LV code the properties of the Amplitude
Scaling, Time Scaling and Time Shifting for some common signals.
Also include the combined operations of shifting and scaling.

Solution

Problem 4
Plot the function y = Mod( -1) on linear, semi-log and log-log plot.

Solution :

Problem 5
Use 3D plot to plot your own generated data. Hint: use 2D sine
wave.

Solution

Problem 6
Plot two sine waves having different amplitudes and phases on a single chart.

Solution :

Problem 7
Construct any 4 4 matrix, calculate sum of its rows and sum of its
columns. Con-struct transpose of its matrix. If 4 4 matrix was A

and its transpose was A , then calculate A + A and A A. Construct


B matrix of 4 4 and calculate A + B and A B. Calculate the
determinant of the resulting matrix A + B. Now calculate the sum of
its A + B diagonal elements.
Code:
close all
clear all
clc

1
2
3
4

A=rand(4,4);
disp('Matrix A:')
disp(num2str(A))

5
6
7
8
9
10
11

sum cols = sum(A,1);


disp('Sum of columns:')
disp(num2str(sum cols))

12
13
14
15

sum rows = sum(A,2);


disp('Sum of columns:')
disp(num2str(sum rows))

16
17
18
19

A transpose = A';
disp('Transpose of A:')
disp(num2str(A transpose))

20
21

22
23

Product = A*A transpose;

disp('Product of A and its transpose:')


disp(num2str(Product))

24
25
26
27

Sum = A+A transpose;


disp('Sum of A and its transpose:')
disp(num2str(Sum))

28
29
30
31

B=rand(4,4);
disp('Matrix B:')
disp(num2str(B))

32
33
34
35

sum = A+B;
disp('Sum of A and B:')
disp(num2str(sum))

36
37

38
39

product = A*B;

disp('Product of A and B:')


disp(num2str(product))

40
41
42
43

determinant = det(A+B);
disp('Determinant of A + B:')
disp(num2str(determinant))

44
45
46
47

tr = trace(A+B);
disp('Sum of diagonal elements of A + B:')
disp(num2str(tr))

Output:
1
2
3
4
5
6
7

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

Matrix A:
0.27692
0.69483
0.43874
0.18687
0.046171
0.3171
0.38156
0.48976
0.097132
0.95022
0.76552
0.44559
0.82346
0.034446
0.7952
0.64631
Sum of columns:
1.2437
1.9966
2.381
1.7685
Sum of columns:
1.5974
1.2346
2.2585
2.2994
Transpose of A:
0.27692
0.046171
0.097132
0.82346
0.69483
0.3171
0.95022
0.034446
0.43874
0.38156
0.76552
0.7952
0.18687
0.48976
0.44559
0.64631
Product of A and its transpose:
0.78689
0.49205
1.1063
0.72164
0.49205
0.48814
0.81612
0.6689
1.1063
0.81612
1.6969
1.0094
0.72164
0.6689
1.0094
1.7293
Sum of A and its transpose:
0.55385
0.741
0.53588
1.0103
0.741
0.6342
1.3318
0.52421
0.53588
1.3318
1.531
1.2408
1.0103
0.52421
1.2408
1.2926
Matrix B:
0.70936
0.6551
0.95974
0.75127
0.75469
0.16261
0.34039
0.2551
0.27603
0.119
0.58527
0.50596
0.6797
0.49836
0.22381
0.69908
Sum of A and B:
0.98629
1.3499
1.3985
0.93814
0.80086
0.47971
0.72194
0.74486
0.37316
1.0692
1.3508
0.95154
1.5032
0.53281
1.019
1.3454
Product of A and B:
0.96894
0.43974
0.80089
0.73791
0.71028
0.3713
0.48518
0.65101
1.3002
0.53131
0.96442
1.0142
1.2689
0.96177
1.4121
1.4816
Determinant of A + B:
0.020018
Sum of diagonal elements of A + B:
4.1622

Problem 8
Construct a plot between X, Y 1 and Y 2 on single
plot where X = (0, 2 ) with increment of /10
Y 1 = sin (x)
Y 2 = sin (x - 0.25)
Code:
clear all
close all
clc

1
2
3
4

X = 0:pi/10:2*pi;

Y1 = sin(X);
Y2 = sin(X0.25);

6
7
8
9
10
11
12

plot(X, Y1, X, Y2);


xlabel('x');
ylabel('y1, y2');
legend('y1', 'y2');

Output:
1

y1
y2

0.8
0.6
0.4

y1, y2

0.2
0
0.2
0.4
0.6
0.8
1

Plot of X,Y 1 and Y 2

Proble
m9
Create the following transfer functions:
50

Y1 =

s2 + 5s + 30

2s2 +

3s

Y2 =

4s2

+ 3s

And add these two transfer functions.


Code:
clear all
close all
clc

1
2
3
4

disp('Transfer Function Y1:')


Y1 = tf([0 0 50], [1 5 30])

5
6
7

disp('Transfer Function Y2:')


Y2 = tf([2 3 0],[4 3 0])

8
9
10
11
12

disp('Transfer Function Y = Y1 + Y2:')


Y = Y1+Y2

Output:
1

Transfer Function Y1:

Transfer function:
4
50

s2 + 5 s + 30

5
6
7
8

Transfer Function Y2:

9
10
11
12
13

Transfer function:
2 s2 + 3 s

4 s2 + 3 s

14
15

Transfer Function Y = Y1 + Y2:

16
17
18
19
20

Transfer function:
2 s4 + 13 s3 + 275 s2 + 240 s

4 s4 + 23 s3 + 135 s2 + 90 s

Problem 10
Develop a MATLAB program to convert temperature reading from C
to F and vice-versa.
Funtion 1 :
1
2
3
4
5
6

Celsius to Fahrenheit

function [ f ] = celsius2farh( c )
% celcius2farh coverts from celsius scale to farheneit
% Input:
%
c : Temperature in celsius
% Output:
%
f: Temperatutr in farheneit

f = 1.8*c + 32;

end

Function 2 :
1
2
3
4
5
6

Fahrenheit to Celsius

function [ c ] = farh2celsius( f )
% farh2celsius coverts from farheneit scale to celsius
% Input:
%
f : Temperature in farheneit
% Output:
%
c: Temperatutr in celsius

c = 5/9*(f 32);

end

Code:
clear all
close all
clc

1
2
3
4
5
6

disp(['45 degrees celsius in farheneit: ' num2str(celsius2farh(45))])


disp(['113 degrees farheneit in celsius: ' num2str(farh2celsius(113))])

Output:
1
2

45 degrees celsius in farheneit: 113


113 degrees farheneit in celsius: 45

Problem 11
Design your own problems on (Validate the results you
obtained)
1. Dierentiation
2. Integration
Code:
close all
clear all
clc

1
2
3
4
5

syms x

f = exp(4*x)*sin(3*x);

7
8
9
10
11
12
13
14

disp('Function f: ')
disp(f)
derivative = diff(f);
disp('Derivative of f: ')
disp(derivative)
integral = int(f);
disp('Integral of f: ')
disp(integral)

Output:
Function f:

1
2

sin(3*x)*exp(4*x)

Derivative of f:

4
5

3*cos(3*x)*exp(4*x) + 4*sin(3*x)*exp(4*x)

Integral of f:

(exp(4*x)*(3*cos(3*x) 4*sin(3*x)))/25

Problem 12
Plot all the standard signals like step, ramp, impulse
and sinusoids.
Code:
close all
clear all
clc

1
2
3
4
5

t = 10:0.01:10;

6
7
8
9
10
11
12
13

y1 = heaviside(t);
h=figure;
plot(t, y1);
xlabel('t'); ylabel('y1');
title('Step Response');
axis([10 10 1 2]);
saveas(h, 'Q7 fig1.eps', 'eps');

14
15
16
17
18
19
20
21

y2 = sin(t);
h=figure;
plot(t, y2);
xlabel('t'); ylabel('y2');
title('Sine Curve');
axis([10 10 1.5 1.5]);
saveas(h, 'Q7 fig2.eps', 'eps');

22
23
24
25
26
27
28
29

y3 = t;
h=figure;
plot(t, t);
xlabel('t'); ylabel('y3');
title('Ramp Function');
axis([10 10 15 15]);
saveas(h, 'Q7 fig3.eps', 'eps');

30
31
32
33
34
35
36
37
38
39

t=[2:1:2];
y4 = zeros(size(t));
y4(3)=1;
h=figure;
stem(t,y4);
xlabel('t'); ylabel('y4');
title('Impulse Function');
axis([2.5 2.5 1 1.5]);
saveas(h, 'Q7 fig4.eps', 'eps');

Output:

Step Response

1.5

y1

0.5

0.5

1
10

8
t

10

Step Signal
Sine Curve
1.5

y2

0.5

0.5

1.5
10

Sinusoidal Signal

0
t

10

1
5

Ramp Function

1
0

y3

1
8

0
t

10

0
t

0.5

1.5

2.5

Ramp Signal
Impulse Function

1
.

y4

0
.

1.5

0.5

Impulse Signal

Problem 14
Construct a series of numbers:
A = 1 3 5 7 9 11 13 15 17 19 21
B = 50 48 46 44 42..........2
Code:
close all
clear all
clc

1
2
3
4

A = [1:2:21];
B = [50:2:2];

5
6
7

disp('A =')
disp(num2str(A))

8
9
10

disp('B =')
disp(num2str(B))

11
12

Output:
1
2
3
4
5

A =
1
3
B =
50 48
12 10

46
8

44
6

42
4

11
40
2

13
38

15
36

17
34

19
32

21
30

28

26

24

22

20

18

16

14

Problem 15
Develop a MATLAB program to add all the integer number
from 1 to 10.
Code:
clear all
close all
clc

1
2
3
4
5
6
7

s = sum(1:10);
disp('Sum of numbers from 1 to 10:')
disp(num2str(s))

Output:
1
2

Sum of numbers from 1 to 10:


55

Problem 16
Develop a MATLAB program to add all the odd numbers from 1 to
101, both numbers are inclusive.
Code:
close all
clear all
clc

1
2
3
4
5
6
7

s = sum(1:2:101);
disp('Sum of odd numbers from 1 to 101:')
disp(num2str(s))

Output:
1
2

Sum of odd numbers from 1 to 101:


2601

Problem 17
The model of RC circuit shown in figure 1 can be found from
Kirchhos voltage law and conservation of change. RC dy/dt + y
= v(t). Suppose the value of RC is 0.1 second. Use a numerical
method to find the free response for the case where the applied
voltage v is 0 and the initial capacitor voltage is y(0) = 2 volts.
Code:
close all
clear all
clc

1
2
3
4
5
6

7
8
9
10

sol = dsolve('0.1*Dy+y = 0', 'y(0)=2', 't');

disp('Solution is:')

disp(sol)
h=figure;
ezplot(sol);
saveas(h, 'Q11 fig1.eps', 'eps');

Output:
1

Solution is:

2/exp(10*t)
2/exp(10 t)

23

10

x 10

9
8
7
6
5
4
3
2
1
0
6

Figure 7: Solution Plot


Conclusion: The free response of the RC circuit has been obtained.

Problem 18
Consider a mechanical system with equation mx + bx + kx = 0
where m = 1kg, b = 3N sec/m and k = 2N/m. Assume that at t = 0
, x(0) = 0.1m and x(0) = 0.05m/sec. Obtain motion of mass m
subjected to initial condition.
Code:
close all
clear all
clc

1
2
3
4

sol = dsolve('D2y + 3*Dy + 2*y = 0', 'y(0)=0.1', 'Dy(0) = 0.05', 't');

disp('Solution is:')
disp(sol)
h=figure;
ezplot(sol,[0 10]);
saveas(h, 'Q14 fig1.eps', 'eps');

6
7
8
9
10

Output:
Solution is:

1
2

1/(4*exp(t)) 3/(20*exp(2*t))
1/(4 exp(t)) 3/(20 exp(2 t))

0.1

0.08

0.06

0.04

0.02

0
0

5
t

Figure 8: Solution Plot

10

Problem 19
Use MATLAB to compute the solution of following equation:
5x + 7 x + 4x = sin(t)
Code:
close all
clear all
clc

1
2
3
4

y=dsolve('d*D2x + 7*Dx +4*x = sin(t)','t');

disp('Solution is:')
disp(y)

6
7

Output:
1

Solution is:

C2*exp((t*((49 16*d)(1/2) 7))/(2*d)) +


C3*exp((t*((49 16*d)(1/2)+ 7))/(2*d)) +

3
4
5
6
7
8
9

(exp((7*t + t*(49 16*d)(1/2))/(2*d))*exp((t*((49 16*d)(1/2) + 7))/


(2*d))*(cos(t) (sin(t)*((49 16*d)(1/2) + 7))/(2*d)))/
((49 16*d)(1/2)*(((49 16*d)(1/2) + 7)2/(4*d2) + 1))
(exp((7*t t*(49 16*d)(1/2))/(2*d))*exp((t*((49 16*d)(1/2) 7))/
(2*d))*(cos(t) + (sin(t)*((49 16*d)(1/2) 7))/
(2*d)))/((49 16*d)(1/2)*(((49 16*d)(1/2) 7)2/(4*d2) + 1))

Problem 20
Using numerical method (ODE), obtain free response of an RC circuit
(RCy + y = v(t))where applied voltage v is zero and initial capacitor
voltage is y(0) = 2V ,
RC = 0.1s.
Code:
close all
clear all
clc

1
2
3
4
5
6

7
8
9
10

sol = dsolve('0.1*Dy+y = 0', 'y(0)=2', 't');

disp('Solution is:')

disp(sol)
h=figure;
ezplot(sol);
saveas(h, 'Q11 fig1.eps', 'eps');

Output:
1

Solution is:

2/exp(10*t)
2/exp(10 t)

23

10

x 10

9
8
7
6
5
4
3
2
1
0
6

Figure 9: Solution Plot

Problem 21
Use MATLAB to compute and plot the solution of following
equation
10dy/dt + y = 20 + 7sin2t , y(0) = 15
Code:
clear all
close all
clc

1
2
3
4

sol = dsolve('10*Dy + y = 20 + 7*sin(2*t)','y(0) = 15','t');

disp('Solution is:')
disp(sol)
h=figure;
ezplot(sol);
saveas(h, 'Q17 fig1.eps', 'eps');

6
7
8
9
10

Output:
1

Solution is:

(7*sin(2*t))/401 1865/(401*exp(t/10)) (140*cos(2*t))/401 + 20


(7 sin(2 t))/401 1865/(401 exp(t/10)) ...+ 20

18

17

16

15

14

13

12

11
6

0
t

Figure 10: Solution Plot

Problem 22
a) Create a matrix of zeros with 2 rows and 4
columns.
b) Create the row vector of odd numbers
through 21,
L= 1
3
5
7
9
11 13 15
17 19
21
(Use the colon operator)

Solution:
a)

b)

Problem 23
a) Find the sum S of vector L's elements.
b) Form the matrix
A= 2
3
2
1
0
1

Solution:
a)

b)

Problem 24
a) Create two dierent vectors of the same
length and add them.
b) Now subtract them.
c) Perform element-by-element multiplication
on them.
d) Perform element-by-element division on
them.
e) Raise one of the vectors to the second
power.
f) Create a 3X3 matrix and display the first
row of and the second column on the screen.

Solution:
a)

b)

c)

d)

e)

f)

Problem 25

Generate a large chunk of data and compute


Mean, Standard Deviation and Variance,
Mode, Moment around the mean, RMS etc.

Solution:
Code:
a=0:10;
sum=0;
for i=1:10
sum=sum+a(i);
end
for i=1:10;
s=(a(i)-mean)^2;
end
mean=sum/11
sd=sqrt(s/10)
variance=sd^2
Output :

Problem 26
a) Define the following function using syms:
f ( x )=x 2 e x 5 x3

b) Compute the integral, and the first and


second derivatives of the above function
symbolically.
c) Define the following function using syms:
f ( x , y , z )=x 2 e y 5 z 2

Compute the integral with respect to x .

Solution:

Model 1

To convert AC into DC using Half


Wave Rectifier.

Model 2

To convert AC into DC using Full


Wave Rectifier.

Model 3

To estimate current and voltages in


a DC circuit.

Model 4

To understand the working of AND


& OR logical operators when
provided with two pulse wave
forms.

You might also like