Top Banner
MATLAB Winter Training Report Submitted To : Submitted By : Dr. Bhavnesh Kumar Navtej Singh Narula
59

Matlab (1)

Jul 07, 2016

Download

Documents

Mayur Ramavat

matlab
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Matlab (1)

MATLAB Winter Training

Report

Submitted To: Submitted By:

Dr. Bhavnesh Kumar Navtej Singh Narula

ICE - II

Roll No. 492/IC/13

CERTIFICATE

Page 2: Matlab (1)

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 EngineeringNetaji Subhas Institute of Technology

Azad Hind Fauj Marg, Sector-3Dwarka, New Delhi-110 078

Page 3: Matlab (1)

INDEX1. 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. Differentiationb. 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.

Page 4: Matlab (1)

16. The model of RC circuit can be found from Kirchhoff’s 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: 5¨x + 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 different 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 the following function using syms: f ( x )=x2 ex−5 x3

Compute the integral, and the first and second derivatives of the above function symbolically. Define the following function using syms:

f ( x , y , z )=x2 e y−5 z2

Compute the integral with respect to x.

Page 5: Matlab (1)

MODELS1. 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.

Page 6: Matlab (1)

Problem 1

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

Solution:

Page 7: Matlab (1)

Problem 2

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

Solution

Page 8: Matlab (1)

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

Page 9: Matlab (1)
Page 10: Matlab (1)

Problem 4

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

Solution :

Page 11: Matlab (1)

Problem 5

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

Solution

Page 12: Matlab (1)

Problem 6

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

Solution :

Page 13: Matlab (1)

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:1 close all 2 clear all 3 clc 4

5 A=rand(4,4); 6 disp('Matrix A:') 7 disp(num2str(A)) 8

9 sum cols = sum(A,1); 10 disp('Sum of columns:') 11 disp(num2str(sum cols)) 12

13 sum rows = sum(A,2); 14 disp('Sum of columns:') 15 disp(num2str(sum rows)) 16

17 A transpose = A'; 18 disp('Transpose of A:') 19 disp(num2str(A transpose)) 20

21 Product = A*A transpose; 22 disp('Product of A and its transpose:') 23 disp(num2str(Product)) 24

25 Sum = A+A transpose; 26 disp('Sum of A and its transpose:') 27 disp(num2str(Sum)) 28

29 B=rand(4,4); 30 disp('Matrix B:') 31 disp(num2str(B)) 32

33 sum = A+B; 34 disp('Sum of A and B:') 35 disp(num2str(sum)) 36

37 product = A*B; 38 disp('Product of A and B:') 39 disp(num2str(product)) 40

41 determinant = det(A+B); 42 disp('Determinant of A + B:') 43 disp(num2str(determinant)) 44

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

Page 14: Matlab (1)

Output:1 Matrix A:2 0.27692 0.69483 0.43874 0.186873 0.046171 0.3171 0.38156 0.489764 0.097132 0.95022 0.76552 0.445595 0.82346 0.034446 0.7952 0.646316 Sum of columns:7 1.2437 1.9966 2.381 1.7685

8 Sum of columns: 9 1.5974

10 1.2346 11 2.2585 12 2.2994 13 Transpose of A:

14 0.27692 0.046171 0.097132 0.8234615 0.69483 0.3171 0.95022 0.03444616 0.43874 0.38156 0.76552 0.795217 0.18687 0.48976 0.44559 0.6463118 Product of A and its transpose:19 0.78689 0.49205 1.1063 0.7216420 0.49205 0.48814 0.81612 0.668921 1.1063 0.81612 1.6969 1.009422 0.72164 0.6689 1.0094 1.729323 Sum of A and its transpose:24 0.55385 0.741 0.53588 1.010325 0.741 0.6342 1.3318 0.5242126 0.53588 1.3318 1.531 1.240827 1.0103 0.52421 1.2408 1.292628 Matrix B:29 0.70936 0.6551 0.95974 0.7512730 0.75469 0.16261 0.34039 0.255131 0.27603 0.119 0.58527 0.5059632 0.6797 0.49836 0.22381 0.6990833 Sum of A and B:34 0.98629 1.3499 1.3985 0.9381435 0.80086 0.47971 0.72194 0.7448636 0.37316 1.0692 1.3508 0.9515437 1.5032 0.53281 1.019 1.345438 Product of A and B:39 0.96894 0.43974 0.80089 0.7379140 0.71028 0.3713 0.48518 0.6510141 1.3002 0.53131 0.96442 1.014242 1.2689 0.96177 1.4121 1.4816

43 Determinant of A + B: 44 −0.020018 45 Sum of diagonal elements of A + B: 46 4.1622

Page 15: Matlab (1)

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

Code:1 clear all 2 close all 3 clc 4

5 X = 0:pi/10:2*pi; 6 Y1 = sin(X); 7 Y2 = sin(X−0.25); 8

9 plot(X, Y1, X, Y2); 10 xlabel('x'); 11 ylabel('y1, y2'); 12 legend('y1', 'y2');

Output:

1y1

0.8 y2

0.6

0.4

0.2

y 2

0y 1 ,

−0.2

−0.4

−0.6

−0.8

−11 2 3 4 5 6 70

x

Plot of X,Y 1 and Y 2

Page 16: Matlab (1)

Problem 9

Create the following transfer functions:

50Y1

=

s2 + 5s + 302s2 + 3s

Y2 =

4s2 + 3s

And add these two transfer functions.

Code:1 clear all 2 close all 3 clc 4

5 disp('Transfer Function Y1:') 6 Y1 = tf([0 0 50], [1 5 30]) 7

8 disp('Transfer Function Y2:') 9 Y2 = tf([2 3 0],[4 3 0])

10

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

Output:1 Transfer Function Y1:2

3 Transfer function: 4 50 5 −−−−−−−−−−−−−− 6 sˆ2 + 5 s + 30 7

8 Transfer Function Y2:9

10 Transfer function: 11 2 sˆ2 + 3 s 12 −−−−−−−−−−− 13 4 sˆ2 + 3 s 14

15 Transfer Function Y = Y1 + Y2:16

17 Transfer function: 18 2 sˆ4 + 13 sˆ3 + 275 sˆ2 + 240 s 19 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 20 4 sˆ4 + 23 sˆ3 + 135 sˆ2 + 90 s

Page 17: Matlab (1)

Problem 10

Develop a MATLAB program to convert temperature reading from C to F and vice-versa.

Funtion 1 : Celsius to Fahrenheit1 function [ f ] = celsius2farh( c ) 2 % celcius2farh coverts from celsius scale to farheneit

3 % Input:4 % c : Temperature in celsius5 % Output:6 % f: Temperatutr in farheneit

7

8 f = 1.8*c + 32; 9 end

Function 2 : Fahrenheit to Celsius1 function [ c ] = farh2celsius( f ) 2 % farh2celsius coverts from farheneit scale to celsius

3 % Input:4 % f : Temperature in farheneit5 % Output:6 % c: Temperatutr in celsius

7

8 c = 5/9*(f − 32); 9 end

Code:1 clear all 2 close all 3 clc 4

5 disp(['45 degrees celsius in farheneit: ' num2str(celsius2farh(45))]) 6 disp(['113 degrees farheneit in celsius: ' num2str(farh2celsius(113))])

Output:1 45 degrees celsius in farheneit: 113 2 113 degrees farheneit in celsius: 45

Page 18: Matlab (1)

Problem 11Design your own problems on (Validate the results you obtained)

1. Differentiation2. Integration

Code:1 close all 2 clear all 3 clc 4

5 syms x 6 f = exp(4*x)*sin(3*x); 7 disp('Function f: ') 8 disp(f) 9 derivative = diff(f);

10 disp('Derivative of f: ') 11 disp(derivative) 12 integral = int(f); 13 disp('Integral of f: ') 14 disp(integral)

Output:1 Function f: 2 sin(3*x)*exp(4*x) 3

4 Derivative of f: 5 3*cos(3*x)*exp(4*x) + 4*sin(3*x)*exp(4*x) 6

7 Integral of f: 8 −(exp(4*x)*(3*cos(3*x) − 4*sin(3*x)))/25

Page 19: Matlab (1)

Problem 12

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

Code:1 close all 2 clear all 3 clc 4

5 t = −10:0.01:10;6

7 y1 = heaviside(t); 8 h=figure; 9 plot(t, y1);

10 xlabel('t'); ylabel('y1'); 11 title('Step Response'); 12 axis([−10 10 −1 2]); 13 saveas(h, 'Q7 fig1.eps', 'eps'); 14

15 y2 = sin(t); 16 h=figure; 17 plot(t, y2); 18 xlabel('t'); ylabel('y2'); 19 title('Sine Curve'); 20 axis([−10 10 −1.5 1.5]); 21 saveas(h, 'Q7 fig2.eps', 'eps'); 22

23 y3 = t; 24 h=figure; 25 plot(t, t); 26 xlabel('t'); ylabel('y3'); 27 title('Ramp Function'); 28 axis([−10 10 −15 15]); 29 saveas(h, 'Q7 fig3.eps', 'eps'); 30

31 t=[−2:1:2]; 32 y4 = zeros(size(t)); 33 y4(3)=1; 34 h=figure; 35 stem(t,y4); 36 xlabel('t'); ylabel('y4'); 37 title('Impulse Function'); 38 axis([−2.5 2.5 −1 1.5]); 39 saveas(h, 'Q7 fig4.eps', 'eps');

Page 20: Matlab (1)

Output:y1

Step Response2

1.5

1

0.5

0

−0.5

−1−8 −6 −4 −2 0 2 4 6 8 10−10t

Step Signal

Sine Curve1.5

1

0.5

y2 0

−0.5

−1

−1.5−8 −6 −4 −2 0 2 4 6 8 10−10

t

Sinusoidal Signal

Page 21: Matlab (1)

y3y4

15

10

5

0

−5

−10

−15−10

1.5

1

0.5

0

−0.5

−1−2.5

Ramp Function

−8 −6 −4 −2 0 2 4 6 8 10t

Ramp Signal

Impulse Function

−2 −1.5 −1 −0.5 0 0.5 1 1.5 2 2.5t

Impulse Signal

Page 22: Matlab (1)

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:1 close all 2 clear all 3 clc 4

5 A = [1:2:21]; 6 B = [50:−2:2]; 7

8 disp('A =') 9 disp(num2str(A))

10

11 disp('B =') 12 disp(num2str(B))

Output:1 A =2 1 3 5 7 9 11 13 15 17 19 213 B =4 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 145 12 10 8 6 4 2

Page 23: Matlab (1)

Problem 15

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

Code:1 clear all 2 close all 3 clc 4

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

Output:1 Sum of numbers from 1 to 10: 2 55

Page 24: Matlab (1)

Problem 16

Develop a MATLAB program to add all the odd numbers from 1 to 101, both numbers are inclusive.

Code:1 close all 2 clear all 3 clc 4

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

Output:1 Sum of odd numbers from 1 to 101: 2 2601

Page 25: Matlab (1)

Problem 17

The model of RC circuit shown in figure 1 can be found from Kirchhoff’s 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:1 close all 2 clear all 3 clc 4

5 sol = dsolve('0.1*Dy+y = 0', 'y(0)=2', 't');6 disp('Solution is:')7 disp(sol)8 h=figure;9 ezplot(sol);

10 saveas(h, 'Q11 fig1.eps', 'eps');

Output:1 Solution is:

2 2/exp(10*t)

23 2/exp(10 t)x 10

10

9

8

7

6

5

4

3

2

1

0

−6 −5 −4 −3 −2 −1 0t

Figure 7: Solution Plot

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

Page 26: Matlab (1)

Problem 18Consider 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:1 close all 2 clear all 3 clc 4

5 sol = dsolve('D2y + 3*Dy + 2*y = 0', 'y(0)=0.1', 'Dy(0) = 0.05', 't'); 6 disp('Solution is:') 7 disp(sol) 8 h=figure; 9 ezplot(sol,[0 10]);

10 saveas(h, 'Q14 fig1.eps', 'eps');

Output:1 Solution is:

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 1 2 3 4 5 6 7 8 9 10t

Figure 8: Solution Plot

Page 27: Matlab (1)

Problem 19

Use MATLAB to compute the solution of following equation:5¨x + 7 x˙ + 4x = sin(t)

Code:1 close all 2 clear all 3 clc 4

5 y=dsolve('d*D2x + 7*Dx +4*x = sin(t)','t'); 6 disp('Solution is:') 7 disp(y)

Output:1 Solution is: 2 C2*exp((t*((49 − 16*d)ˆ(1/2) − 7))/(2*d)) + 3 C3*exp(−(t*((49 − 16*d)ˆ(1/2)+ 7))/(2*d)) + 4 (exp((7*t + t*(49 − 16*d)ˆ(1/2))/(2*d))*exp(−(t*((49 − 16*d)ˆ(1/2) + 7))/5 (2*d))*(cos(t) − (sin(t)*((49 − 16*d)ˆ(1/2) + 7))/(2*d)))/ 6 ((49 − 16*d)ˆ(1/2)*(((49 − 16*d)ˆ(1/2) + 7)ˆ2/(4*dˆ2) + 1)) − 7 (exp((7*t − t*(49 − 16*d)ˆ(1/2))/(2*d))*exp((t*((49 − 16*d)ˆ(1/2) − 7))/ 8 (2*d))*(cos(t) + (sin(t)*((49 − 16*d)ˆ(1/2) − 7))/ 9 (2*d)))/((49 − 16*d)ˆ(1/2)*(((49 − 16*d)ˆ(1/2) − 7)ˆ2/(4*dˆ2) + 1))

Page 28: Matlab (1)

Problem 20Using 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:1 close all 2 clear all 3 clc 4

5 sol = dsolve('0.1*Dy+y = 0', 'y(0)=2', 't');6 disp('Solution is:')7 disp(sol)8 h=figure;9 ezplot(sol);

10 saveas(h, 'Q11 fig1.eps', 'eps');

Output:1 Solution is:

2 2/exp(10*t)

23 2/exp(10 t)x 10

10

9

8

7

6

5

4

3

2

1

0

−6 −5 −4 −3 −2 −1 0t

Figure 9: Solution Plot

Page 29: Matlab (1)

Problem 21

Use MATLAB to compute and plot the solution of following equation

10dy/dt + y = 20 + 7sin2t , y(0) = 15

Code:1 clear all 2 close all 3 clc 4

5 sol = dsolve('10*Dy + y = 20 + 7*sin(2*t)','y(0) = 15','t'); 6 disp('Solution is:') 7 disp(sol) 8 h=figure; 9 ezplot(sol);

10 saveas(h, 'Q17 fig1.eps', 'eps');

Output:1 Solution is:

2 (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)) −...+ 2018

17

16

15

14

13

12

11

−6 −4 −2 0 2 4 6t

Figure 10: Solution Plot

Page 30: Matlab (1)

Problem 22a) 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)

Page 31: Matlab (1)

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

1 0 1

Solution: a)

b)

Page 32: Matlab (1)

Problem 24a) Create two different 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)

Page 33: Matlab (1)

c)

Page 34: Matlab (1)

d)

e)

Page 35: Matlab (1)

f)

Page 36: Matlab (1)

Problem 25Generate 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:10sum=sum+a(i);endfor i=1:10;s=(a(i)-mean)^2;endmean=sum/11sd=sqrt(s/10)variance=sd^2

Output :

Page 37: Matlab (1)

Problem 26

a) Define the following function using syms:f ( x )=x2ex−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 )=x2 e y−5 z2

Compute the integral with respect to x .

Solution:

Page 38: Matlab (1)

Model – 1

To convert AC into DC using Half Wave Rectifier.

Page 39: Matlab (1)

Model – 2

To convert AC into DC using Full Wave Rectifier.

Page 40: Matlab (1)

Model – 3

To estimate current and voltages in a DC circuit.

Page 41: Matlab (1)

Model – 4

To understand the working of AND & OR logical operators when provided with two pulse wave forms.