ECLR 33 (Matlab) Manual
ECLR 33 (Matlab) Manual
Lab Manual
ECLR-33
MATLAB Programming
Bachelor of Technology
in
Electronics & Communication Engineering
1
Computer Center
ECE Department, NIT, Kurukshetra
Vision
Mission
2
Computer Center
ECE Department, NIT, Kurukshetra
Program Educational Objectives (PEOs)
PEO – 2: Be able to analyze real life problems and design socially accepted and
economically viable solutions in Electronics and Communication Engineering
area.
PEO –4: Have appropriate human and technical communication skills to be a good team-
member/leaders and responsible human being
PO5: Modern tool usage: Be able to create, select, and apply appropriate techniques,
resources, and modern engineering and IT tools including prediction and modeling
to complex engineering activities with an understanding of the limitations.
3
Computer Center
ECE Department, NIT, Kurukshetra
PO6: The engineer and society: Be able to apply reasoning informed by the contextual
knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional Electronics and
Communication Engineering practice.
PO8: Ethics: Be able to apply ethical principles, commit to professional ethics in context to
Electronics and communication engineering practice.
PO11: Life-long learning: Be able to recognize the need for, and have the preparation and
ability to engage in independent and life-long learning in the broadest context of
technological change.
PO12: Project management and finance: Have knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a
member/ leader in a team, to manage projects in multidisciplinary environments.
4
Computer Center
ECE Department, NIT, Kurukshetra
ECLR-33
MATLAB Programming
L T P
0 0 3
Course This lab aims to get familiarize the students about the numerical computing in
Objectives various fields. MATLAB allows matrix manipulations, plotting the function by
using polar & contour3 commands, implementation of algorithms & creation
of user interfaces. It calculates their design, waveforms, graphs, circles, circular
helix and solves various integral and differential equations. Using MATLAB
symbolic math toolbox, determine the Laplace transform. Further in this lab
students will attain the knowledge about convolution and cross correlation
between two sequences.
Course At the end of the course, the students will be able to:
Outcomes 1. Learn various aspects of MATLAB such as interface, syntax, debugging
and execution.
2. Understand how various MATLAB scripts are created, saved and
executed.
3. Apply above knowledge in writing simple programs.
4. Learn basic concepts of simulation using SIMULINK and write complex
programs.
List of Experiments
e) First take the transpose of a vector and then multiply the vectors
5
Computer Center
ECE Department, NIT, Kurukshetra
̅∗ v̅ t
element by element u.
5. Write a Matlab program to break a function, f(x) into odd and even parts, one can
compute the two function
𝑓(𝑥) − 𝑓(−𝑥)
𝑓𝑜𝑑𝑑 (𝑥) =
2
𝑓(𝑥) + 𝑓(−𝑥)
𝑓𝑒𝑣𝑒𝑛 (𝑥) =
2
6
Computer Center
ECE Department, NIT, Kurukshetra
13. The Fibonacci sequence starts off with the numbers 0 and 1, then succeeding terms
are the sum of its two immediate predecessors. Write a Matlab program for
Fibonacci series.
14. Write a Matlab program for the calculation of the following:
1-1/2+ 1/3- 1/4 + 1/5 ………
15. Write a Matlab program to generalize for the following:
1 1 1 1
𝑠(20) = 1 + 2 + 2 + 2 + ⋯ + 2
2 3 4 20
1 1 1 1
𝑠(21) = 1 + 2 + 2 + 2 + ⋯ + 2
2 3 4 21
.
.
1 1 1 1
𝑠(100) = 1 + + + + ⋯ +
22 32 42 1002
𝑑𝑦
=𝑥+𝑡
𝑑𝑥
7
Computer Center
ECE Department, NIT, Kurukshetra
25. Compute the following transcendental equation using Matlab:
1
26. Determine the convolution of x (t) = with itself.
1+𝑡 2
27. Compute the cross correlation between the following two sequences.
28. Let x (n)=u(n)-u(n-10). Decompose x(n) into even and odd components.
30. Using MATLAB symbolic math toolbox, determine the Laplace Transform of the following
8
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 1
Apparatus Required:
Theory:
INTRODUCTION TO MATLAB:
MATLAB is a powerful language for technical computing. The name MATLAB stands for
MATRIX Laboratory, because its basic data element is a matrix (array). MATLAB can be
used for math computations, modeling and simulations, data analysis and processing,
visualization and graphics, and algorithm development.
Window Purpose
The Command Window is MATLAB’s main window and can be used for executing
commands, opening other windows, running programs written by the user, and managing
the software. An example is shown in Figure 1-1.
9
Computer Center
ECE Department, NIT, Kurukshetra
10
Computer Center
ECE Department, NIT, Kurukshetra
If a command is too long to fit in one line, it can be continued to the next line by
typing three periods … (called an ellipsis) and pressing the Enter key. The
continuation of the command is then typed in the new line. The command can
continue line after line up to a total of 4,096 characters.
Addition + 5+3
Subtraction - 5-3
Multiplication * 5*3
Right division / 5/3
Left division \ 3\5
Exponentiation 5^3
^
The simplest way to use MATLAB is as a calculator. This is done in the Command Window
by typing a mathematical expression and pressing the Enter key. MATLAB calculates the
expression and responds by displaying ans= and the numerical result of the expression in
the next line. This is demonstrated in Tutorial 1-1.
Tutorial 1-1: Using MATLAB as a calculator.
11
Computer Center
ECE Department, NIT, Kurukshetra
Procedure:
Code:
clc
clear all
close all
a=16
b=27
cube_a=a^3
sum=a+b
diff=a-b
mult=a*b
div=a/b
c=[1 4;35 49]
sqrt(c)
12
Computer Center
ECE Department, NIT, Kurukshetra
Expected Outcome:
Assignments:
Calculate following:
(14.82 +6.52 ) 55
1. +(
3.82 √2+14)
𝑒6
2. (±3.53 ) + 𝑙𝑛524 + 2061/3
16.52 (8.4±√70)
3. 4.32 ±17.3
5.23 ±6.42 +3 13.3 1.5
4. +( )
1.68 ±2 5
Conclusion:
At the end students should try to learn as much as they can from this experiment and
should try different Algebraic calculation problems in MATLAB.
13
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 2
Objective: For an electrical circuit with inductance L=0.00001 and resistance R=100
the damped natural frequency of oscillation is F=sqrt(1/L*C-R^2/4*C^2). Write a
program to calculate frequency for different value of C varying from 0.1 to 1 in steps
of 0.1
Apparatus Required:
Theory:
Procedure:
clc
L=0.00001;
R=100;
for C=0.1:0.1:1
14
Computer Center
ECE Department, NIT, Kurukshetra
f=sqrt((1/(L*C))-((R^2)/(4*C*C)))
end
Expected Outcome:
Assignments:
1. A cube has a side of 18 cm. a) Determine the radius of a sphere that has the
same surface area as the cube. b) Determine the radius of a sphere that has the
same volume as the cube.
Conclusion:
At the end students should try to learn as much as they can from this experiment and
should try different Algebraic calculation problems in MATLAB.
15
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 3
Apparatus Required:
Theory:
Code:
clc
16
Computer Center
ECE Department, NIT, Kurukshetra
u = [1,2,3,4]
v = [2,3,4,5]
a = 5*v
b = u.^5
c = v.^5
d = u.*v
e = u*transpose(v)
Expected Outcome:
Assignments:
1. Using the ones and zeros commands, create a matrix in which the first two
rows are 0s and the next two rows are 1s.
2. Create a matrix in which the middle two rows and the middle two columns
are 1s, and the rest of the entries are 0s.
3. Define the variables x= 0.85,y=12.5 and then use them to create a column
vector that has the following elements: y, yx, ln(y/x), y.x and x+y
17
Computer Center
ECE Department, NIT, Kurukshetra
Conclusion:
18
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 4
Objective: Write a MATLAB program to calculate:
Apparatus Required:
Theory:
Element wise Operation: If we apply “.” before any operation then that operation is
operated on every element of array separately.
sum(A): If A is a vector, returns the sum of the elements of the vector.
Procedure:
19
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
Conclusion:
In this experiment, the motive was to introduce the element wise operations on array
using MATLAB.
At the end, student should try to learn as much as they can from this experiment and
should try different element wise operation problems in MATLAB.
20
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 5
Objective: Write a MATLAB program to break a function into odd and even parts.
Apparatus Required:
Theory:.
Even and odd signals bear some important symmetry properties. Under reversal of
independent variable, these signals either remain the same (evensignal) or get
reflected or flipped (odd signal) about the horizontal axis. Equations or definitions (1)
and (2) mathematically express these propertiesfor both continuous and discrete time
cases.
Even Signals: x(t) = x(–t), x[n] = x[–n] (1)
Odd Signals: x(t) = –x(–t), x[n] = –x[–n] (2)
A signal x(t) can be decomposed into even and odd parts as expressed by the
equations (3) and (4)
1
𝑥𝑒 (𝑡) = 2 {𝑥(𝑡) + 𝑥(−𝑡)} (3)
1
𝑥𝑜 (𝑡) = 2 {𝑥(𝑡) − 𝑥(−𝑡)} (4)
Where x(t)= 𝑥𝑒 (𝑡) + 𝑥𝑜 (𝑡)
Procedure:
clc
clear all
close all
x=0:1:10
a= exp(x)
b= exp(-x)
odd=(a-b)/2
21
Computer Center
ECE Department, NIT, Kurukshetra
even=(a+b)/2
Expected Outcome:
Assignments:
In this experiment, the motive was to understand MATLAB tool is used to break a
signal or function in even and odd parts.
At the end students should try to learn as much as they can from this experiment and
should divide different functions or signals into even and odd parts and observe the
difference.
22
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 6
Theory:
To define any symbolic variable syms command is used in matlab. Input array,
specified as a scalar, vector, matrix, or multidimensional array. Exponential is the
basic signals with positive and negative slope. Command used for exponential signal
is exp(x). where x is any variables.
Procedure:
23
Computer Center
ECE Department, NIT, Kurukshetra
clc
symsx;
f1=exp(x);
f1=inline(f1);
f2=subs(f1,x,-x);
f2=inline(f2);
feven=(f1(x)+f2(x))/2;
fodd=(f1(x)-f2(x))/2;
disp('even part:');
disp(feven);
disp('odd part:');
disp(fodd);
Expected Outcome:
Assignments:
24
Computer Center
ECE Department, NIT, Kurukshetra
Conclusion:
In this experiment, the motive was to understand how variables are defined and
values are assigned to these variables in MATLAB.
At the end students are given the assignments to learn more about the inputs and
outputs in MATLAB
25
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 7
a) Sin wave
b) Cosine wave
c) Triangular wave
d) Square wave
e) Impulse function
f) Exponential function
g) Unit Ramp
Apparatus Required:
Theory:
Here with the help of matlab we identify the shape and behaviors of different signals.
Procedure:
Code:
Code 1.
clc
x=0:1/5:2*pi
y=sin(x)
Subplot(2,1,1);
plot(x,y,'-Or')
xlabel('x-axix')
ylabel('y axix')
title('sin wave')
gridon
26
Computer Center
ECE Department, NIT, Kurukshetra
subplot(2,1,2);
plot(x,cos(x),'green')
xlabel('x-axis')
ylabel('y axis')
title('cosine wave')
gridon
Code 2.
clc
x= -10:1/100:10;
y=square(x);
y1=sawtooth(x);
subplot(3,1,1)
plot(x,y)
xlabel('x-axix')
ylabel('y axix')
title('square wave')
gridon
subplot(3,1,2)
plot(x,y1)
xlabel('x-axix')
ylabel('y axix')
title('triangular wave')
gridon
subplot(3,1,3)
x=-1:1/100:5;
y2=exp(x);
plot(x,y2)
xlabel('x-axix')
ylabel('y axix')
title('exp wave')
gridon
27
Computer Center
ECE Department, NIT, Kurukshetra
Code 3.
clc
x=1:1/100:5;
y= [1; zeros(99,1)];
impz(y,1)
Code 4.
clc
t=1:1/100:5;
ramp_sig= t;
plot(ramp_sig)
title('unit ramp')
Expected Outcome:
28
Computer Center
ECE Department, NIT, Kurukshetra
29
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
In this experiment, the motive was to understand different commends used for basic
signals in MATLAB.
At the end students are given the assignments to learn more about shape and
behaviors of the different signals in MATLAB.
30
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 8
a) Sin(x)/x
b) x^2+1/x^2-4
c) [(10-x)^(1/3)-2]/(4-x^2)^1/2
Apparatus Required:
Theory:
Procedure:
Code1:
clc
x = -5*pi:pi/15:5*pi;
y = sin(x)./x;
plot(x,y)
Xlabel('x-axis')
Ylabel('sin(x)/x')
gridon;
Code 2:
clc
x=-5:0.001:5;
y = (x.^2 + 1)./(x.^2-4);
plot(x,y)
31
Computer Center
ECE Department, NIT, Kurukshetra
xlabel('x-axis')
ylabel('y-axis')
gridon;
Code 3:
clc
x=-5:0.001:5;
y = ((10-x).^(1/3)-2)./(4-x.^2).^(1/2);
plot(x,y,'r')
xlabel('x-axis')
ylabel('y-axis')
gridon;
Expected Outcome:
32
Computer Center
ECE Department, NIT, Kurukshetra
33
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
In this experiment, the motive was to understand the shape of the different
mathematical equations.
34
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 9
Apparatus Required:
Theory:
A circle is the locus of all points equidistant from a central point. The mathematical
equations of the circle is x2 + y2 = 52. Where x and y are the coordinates of the circle
and 5 is the radius of the circle.
Procedure:
Code:
clc
clear all
close all
t = -2*pi:0.000001:2*pi;
x = 5*cos(t);
y = 5*sin(t);
plot(x,y);
Xlabel('X-axix');
Ylabel('Y-axix');
gridon;
Expected Outcome:
35
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
At the end students are given the assignments to learn more about plotting of grids
in MATLAB
36
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 10
Apparatus Required:
Theory:
Procedure:
Code:
clc
clear all
close all
n=input('enter the no.:')
fact=1;
while n>=1
fact = fact* n;
n=n-1;
end
fact
clc
clear all
close all
p=input('enter the no.:')
37
Computer Center
ECE Department, NIT, Kurukshetra
fact=1;
for i=1:1:p
fact = fact*i;
end
fact
Expected Outcome:
Assignments:
In this experiment, the motive was to understand how to solve factorial in MATLAB
At the end students are given the assignments to learn more about how to solve the
differential equations with different conditions
38
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 11
Objective: Write a MATLAB Program to compute the sum of all powers of 2 below
50 using While statement.
Apparatus Required:
Theory:
In mathematics, the power of any no simply means the multiplication of that number
equal to the power times. To find the power of any number we can use ^ symbol in
MATLAB.
Procedure:
Code:
clc
clear all
close all
x=2;
a=0;
sum=0
while a<50
y=x^a
sum=sum + y
a=a+1
end
sum
Expected Outcome:
sum = 1.1259e+015
39
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
In this experiment, the motive was to understand how variables are defined and
values are assigned to these variables in MATLAB.
At the end students are given the assignments to learn more about iteration of loops
in MATLAB
40
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 12
-6*x-2*y+2*z=15
-3*x+4*y-3*z=13
2*x+4*y-7*z=-9
Apparatus Required:
Theory:
We can use Y = inv(X) statement and coefficients of given equations to solve them.
Y = inv(X) returns the inverse of the square matrix X. A warning message is printed
if X is badly scaled or nearly singular.
Procedure:
Code:
clc
clear all
close all
a=[15;13;-9]
b=[-6 -2 2;-3 4 -3; 2 4 -7]
b1=inv (b)
b2=b1*a;
disp(['The values of x y z are ',num2str(b2')])
2ND METHOD
a= [15;13;-9]
41
Computer Center
ECE Department, NIT, Kurukshetra
Expected Outcome:
a = 15
13
-9
b= -6 -2 2
-3 4 -3
2 4 -7
a = 15
13
-9
b =-6 -2 2
-3 4 -3
2 4 -7
x =-30/11
y =61/22
z =23/11
Assignments:
42
Computer Center
ECE Department, NIT, Kurukshetra
In this experiment, the motive was to learn and understand the different commands
that can be used to solve complex equations in MATLAB.
At the end students are given the assignments to learn more about used commands
in MATLAB.
43
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 13
Apparatus Required:
Theory:
The Fibonacci series starts with the numbers 0 and 1, the succeeding terms are sum
of its two immediate predecessors.
We will use a equation that will calculate the sum of previous two numbers and then
iterate a loop to print the required no of terms.
Procedure:
clc
clear all
close all
a=0;
b=1;
d=input('enter number of terms : ');
f=d-2;
fprintf('The fibonacci series is : \n%d\n',a)
fprintf('%d\n',b)
for x=1:f
c=a+b;
fprintf('%d\n',c)
a=b;
b=c;
44
Computer Center
ECE Department, NIT, Kurukshetra
end
Expected Outcome:
Assignments:
In this program the motive was to learn the concept of Fibonacci series and then use
for loop along with some logical equations to print the required no of terms in the
series.
45
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 14
Apparatus Required:
Theory:
We have to find the sum of series in which the sign of alternate terms are opposite
and the total no of terms are to be entered by the user.
Procedure:
Code:
clc
clear all
close all
sum =0;
n=input ('number of terms: ')
for x=1:n
sum =sum+ (-1)^(x+1)*(1/x);
end
fprintf('sum= %d', sum)
Expected Outcome:
number of terms : 3
sum =8.333333e-001
Assignments:
46
Computer Center
ECE Department, NIT, Kurukshetra
In this experiment, the motive was to understand how to implement different logic
to perform different tasks and how to use for loop in MATLAB
47
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 15
Apparatus Required:
Theory:
In mathematics, the power of any no simply means the multiplication of that number
equal to the power times. To find the sum of any series we can simply calculate terms
one by one and then add all these terms to find the sum of complete series.
Procedure:
Code:
clc
clear all
close all
sum=0;
n=input('Enter number of terms:')
for x=1:n
sum=sum+ (1/x^2);
end
fprintf('The sum is = %f', sum)
Expected Outcome:
48
Computer Center
ECE Department, NIT, Kurukshetra
1. Write a program to find the sum of same series as given above but with
alternate signs.
2. Write a program to find the sum of same series as given above but using while
loop.
Conclusion:
In this experiment, the motive was to understand how to find power of numbers and
how to find the sum of complete series in MATLAB
At the end students are given the assignments to learn more about how to calculate
different series with different patterns.
49
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 16
Apparatus Required:
Theory:
The area of the triangle having sides a, b and c can be calculated by Heron’s formula
given by the following expression:
where s=(a+b+c)/2
Procedure:
Code:
clc
clear all
close all
a = input('enter first side of triangle \n');
b = input('\n enter second side of triangle \n');
c = input('\n enter third side of triangle \n');
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
area
50
Computer Center
ECE Department, NIT, Kurukshetra
Expected Outcome:
Assignments:
In this experiment, the motive was to understand how variables are defined and
values are assigned to these variables in MATLAB.
At the end students are given the assignments to learn more about the inputs and
outputs in MATLAB
51
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 17
Objective: Write a MATLAB program for a positive number to compute and display
the even power of 2 less than or equal to positive number.
Apparatus Required:
Theory:
Any integer that can be divided exactly by 2 is an even number.The last digit is 0, 2,
4, 6 or 8.
Any integer that cannot be divided exactly by 2 is an odd number. The last digit is 1,
3, 5, 7 or 9.
Procedure:
Code:
clc
clear all
close all
x = input('Enter a positive number ')
n=0;
while 2^n<=x
2^n
n=n+2;
end
Expected Outcome:
52
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
Conclusion:
In this experiment, the motive was to understand how even and odd powers can be
calculated in MATLAB.
At the end students are given the assignments to learn more about the calculations
of power in MATLAB.
53
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 18
Apparatus Required:
Theory:
Helix is a type of smooth space curve, i.e. a curve in three-dimensional space. It has
the property that the tangent line at any point makes a constant angle with a fixed
line called the axis. Examples of helices are coil springs and the handrails of spiral
staircases. A "filled-in" helix – for example, a "spiral" (helical) ramp – is called a
helicoid. Helices are important in biology, as the DNA molecule is formed as two
intertwined helices, and many proteins have helical substructures, known as alpha
helices.
Types:
A double helix consists of two (typically congruent) helices with the same axis,
differing by a translation along the axis.
A conic helix may be defined as a spiral on a conic surface, with the distance to the
apex an exponential function of the angle indicating direction from the axis. An
example is the Corkscrew roller coaster at Cedar Point amusement park.
A circular helix, (i.e. one with constant radius) has constant band curvature and
constant torsion.
A curve is called a general helix or cylindrical helix if its tangent makes a constant
angle with a fixed line in space. A curve is a general helix if and only if the ratio of
curvature to torsion is constant.
A curve is called a slant helix if its principal normal makes a constant angle with a
fixed line in space. It can be constructed by applying a transformation to the moving
frame of a general helix.
plot3(X1, Y1, Z1,...), where X1, Y1, Z1 are vectors or matrices, plots one or more lines
in three-dimensional space through the points whose coordinates are the elements of
X1, Y1, and Z1. The values in X1, Y1, and Z1 can be numeric, date time, duration, or
categorical values.
54
Computer Center
ECE Department, NIT, Kurukshetra
Procedure:
clc
clear all
close all
t=0:0.0002:20;
x=sin(t);
y=cos(t);
plot3(x,y,t)
grid on;
Expected Outcome:
55
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
1. Define t as values between 0 and 10π. Define st and ct as vectors of sine and
cosine values. Plot a 3-D helix.
2. Draw a conical helix by using command plot3.
Conclusion:
In this experiment, the motive was to understand how 3D plots are plotted in
MATLAB
At the end students are given the assignments to learn more about 3D plots
56
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 19
Apparatus Required:
Theory:
Procedure:
Code:
clc
clear all
close all
57
Computer Center
ECE Department, NIT, Kurukshetra
[x,y] = meshgrid(2:.1:4,1:.1:3)
f = (x-3).^2 + (y-2).^2;
surf(x,y,f);
Expected Outcome:
Assignments:
In this experiment, the motive was to understand how to plot 2D and 3D grids
At the end students are given the assignments to learn more about plotting of grids
in MATLAB
58
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 20
Dy = 2x + 5sin(x) , x(0)=1
Apparatus Required:
Theory:
Differential equations are equations that relate a function with one or more of its
derivatives.
Command ‘dsolve(eqn)’in MATLAB solves the differential equation eqn, where eqn
is a symbolic equation. Use diff and == to represent differential equations. For
example, diff(y,x) == y represents the equation dy/dx=y. Solve a system of
differential equations by specifying eqn as a vector of those equations.
Procedure:
Code:
clc
clear all
close all
y = dsolve('Dy-2*t-5*sin(t)','y(0)=1')
59
Computer Center
ECE Department, NIT, Kurukshetra
Expected Outcome:
Assignments:
𝑑𝑦 𝑑2 𝑦
1. Solve = 𝑎𝑦 and 𝑑𝑡 2 = 𝑎𝑦
𝑑𝑡
𝑑2 𝑦
2. Solve = 𝑎2 𝑦 with initial conditions y(0)=b and y’(0)=1.
𝑑𝑡 2
Conclusion:
In this experiment, the motive was to understand how to solve differential equations
in MATLAB
At the end students are given the assignments to learn more about how to solve the
differential equations with different conditions
60
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 21
Apparatus Required:
Theory:
The polar function accepts polar coordinates, plots them in a Cartesian plane, and
draws the polar grid on the plane.
polar (theta,rho) creates a polar coordinate plot of the angle theta versus the radius
rho. theta is the angle from the x-axis to the radius vector specified in radians; rho is
the length of the radius vector specified in dataspace units.
polar (theta,rho,LineSpec)LineSpec specifies the line type, plot symbol, and color for
the lines drawn in the polar plot.
polar (axes_handle,...) plots into the axes with the handle axes_handle instead of into
the current axes (gca).
Code:
t=0:0.01:2*pi;
polar(t,sqrt(2*sin(5*t)))
% polar() is used to draw a curve in polar coordinates .
61
Computer Center
ECE Department, NIT, Kurukshetra
Expected Outcome:
Assignments:
In this experiment, the motive was to understand how this polar() command is used
to draw a curve in polar coordinates.
At the end students are given the assignments to learn more about the inputs and
outputs in MATLAB.
62
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 22
Objective:
5
Write a MATLAB program to compute and plot z= , for |x|≤ 3, |y|≤3 using
1+𝑥 2 +𝑦2
contour3 command in Matlab.
Apparatus Required:
Theory:
A contour plot displays isoclines of matrix Z. Label the contour lines using clabel.
contour(Z) draws a contour plot of matrix Z, where Z is interpreted as heights with respect
to the x-y plane. Z must be at least a 2-by-2 matrix that contains at least two different values.
The x values correspond to the column indices of Z and the y values correspond to the row
indices of Z. The contour levels are chosen automatically.
contour(Z,n) draws a contour plot of matrix Z with n contour levels where n is a scalar. The
contour levels are chosen automatically.
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified
in the monotonically increasing vector v. To display a single contour line at a particular
value, define v as a two-element vector with both elements equal to the desired contour
level. For example, to draw contour lines at level k, use contour(Z,[k k]). Specifying the
vector v sets the LevelListMode property to manual.
Procedure:
Code:
[x,y]=meshgrid([-3:.1:3]);
z=-5./(1+x.^2+y.^2);
contour3(x,y,z); %to find the contours of the curve
63
Computer Center
ECE Department, NIT, Kurukshetra
surf(x,y,z) %to draw the surface of the function
Expected Outcome:
Assignments:
In this experiment, the motive was to understand how z value can be calculated using x
and y in MATLAB.
At the end students are given the assignments to learn more about the calculations of
power in MATLAB.
64
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 23
2
Objective: Compute the following integral∬0 (1 − 6𝑥 2 𝑦)𝑑𝑥𝑑𝑦; using -1<y<1 in matlab.
Apparatus Required:
Theory:
Code:
65
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
Conclusion:
In this experiment, the motive was to computes the value of double integral dblquad in
MATLAB
At the end students are given the assignments to learn more about 3D plots
66
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 24
𝑑𝑦
Objective: Compute the first order differential equation 𝑑𝑥 = 𝑥 + 𝑡 with initial condition
x(0)=0.
Apparatus Required:
Theory:
S= dsolve(eqn) solves the differential equation eqn, where eqn is a symbolic equation. Use
diff and == to represent differential equations. For example, diff(y,x) == y represents the
equation dy/dx=y. Solve a system of differential equations by specifying eqn as a vector of
those equations.
S = dsolve(eqn,cond) solves eqn with the initial or boundary condition cond.
S = dsolve(eqn,cond,Name,Value) uses additional options specified by one or more
Name,Value pair arguments.
example
[y1,...,yN] = dsolve(___) assigns the solutions to the variables y1,...,yN.
Procedure:
Assignments:
67
Computer Center
ECE Department, NIT, Kurukshetra
𝑑𝑦
1. Evaluate the first order differential equation𝑑𝑥 = 𝑥 + 𝑡.
2. Evaluate the expression𝑥 2 + 𝑦 2 + 𝑧 2 using dsolve command.
Conclusion:
In this experiment, the motive was to solve the first order differential equation.
At the end students are given the assignments to learn more about plotting of grids in
MATLAB
68
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 25
Apparatus Required:
Theory:
S = solve(eqn,var) solves the equation eqn for the variable var. If you do not specify var, the
symvar function determines the variable to solve for. For example, solve(x + 1 == 2, x) solves
the equation x + 1 = 2 for x.
S = solve(eqn,var,Name,Value) uses additional options specified by one or more
Name,Value pair arguments.
Y = solve(eqns,vars) solves the system of equations eqns for the variables vars and returns
a structure that contains the solutions. If you do not specify vars, solve uses symvar to find
the variables to solve for. In this case, the number of variables that symvar finds is equal to
the number of equations eqns.
Y= solve(eqns,vars,Name,Value) uses additional options specified by one or more
Name,Value pair arguments
Procedure:
Code:
a = solve('sin(x)-exp(x)+5')
%solve() is used to solve equations.
Expected Outcome:
69
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
In this experiment, the motive was to understand how to solve transcendental equation in
MATLAB
At the end students are given the assignments to learn more about how to solve the
transcendental equation with different conditions.
70
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 26
Theory:
Convolution is the relationship between a system's input signal, output signal, and impulse
response. Convolution is a mathematical way of combining two signals to form a third
signal. It is the single most important technique in Digital Signal Processing. Using the
strategy of impulse decomposition, systems are described by a signal called the impulse
response.
Convolution is similar to cross-correlation. For discrete real valued signals, they differ only
in a time reversal in one of the signals. For continuous signals, the cross-correlation operator
is the ad joint operator of the convolution operator.
Procedure:
Code:
clc
t= 0:0.2:1;
u=1./(1+t.^2);
conv(u,u)
Expected Outcome:
Assignment:
71
Computer Center
ECE Department, NIT, Kurukshetra
1. Calculate 1/(1+t^2) with itself
Conclusion:
72
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 27
Apparatus Required:
Theory:
clc
clear all
close all
crosscorr([1 2 -1 3],[3 -2 1 4])
Expected Outcome:
73
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
In this experiment, the motive was to understand the Cross correlation in MATLAB.
At the end students are given the assignments to learn more about the Cross correlation
problems in MATLAB.
74
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 28
Objective: Write a MATLAB program to decompose x (n) into odd and even components
where x=(1 2 -1 3) = u(n)-u(n-10)
Apparatus Required:
Theory:
Even and odd signals bear some important symmetry properties. Under reversal of
independent variable, these signals either remain the same (even signal) or get reflected or
flipped (odd signal) about the horizontal axis. Equations or definitions (1) and (2)
mathematically express these properties for both continuous and discrete time cases.
Even Signals: x(t) = x(–t), x[n] = x[–n] (1)
Odd Signals: x(t) = –x(–t), x[n] = –x[–n] (2)
The even part and odd part of a signal can be determined by the given formula:
E{u(n)} = [u(n) + u(-n)]/2 and O{u(n)} = [u(n) - u(-n)]/2.
Procedure:
Code:
n=-10:15;
x=[zeros(1,10) ones(1,10) zeros(1,6)];
m=-fliplr(n);
m1=min([m,n]);
m2=max([m,n]);
m=m1:m2;
nm=n(1)-m(1);
n1=1:length(n);
x1=zeros(1,length(m));
x1(n1+nm)=x;
x=x1;
75
Computer Center
ECE Department, NIT, Kurukshetra
xe=0.5*(x+fliplr(x));
xo=0.5*(x-fliplr(x));
figure
t=-15:15;
subplot(3,1,1);
stem(t,x)
axis([-15 15 0 1.5]);
title('original signal');
xlabel('time--->');
ylabel('amplitude--->');
subplot(3,1,2);
stem(t,xe)
axis([-15 15 0 1.5]);
title('even part');
xlabel('time--->');
ylabel('amplitude--->');
subplot(3,1,3);
stem(t,xo)
axis([-15 15 -0.5 0.5]);
title('odd part');
xlabel('time--->');
ylabel('amplitude--->');
Expected Outcome:
76
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
Conclusion:
Here the MATLAB tool is used to break a signal or function in even and odd parts.
At the end, student should try to learn as much as they can from this experiment and should
divide different functions or signals into even and odd parts and observe the difference.
77
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 29
Apparatus Required:
Theory:
A sample is a value or set of values at a point in time and/or space.Sampling can be done
for functions varying in space, time, or any other dimension, and similar results are
obtained in two or more dimensions.
Sampling period : For functions that vary with time, let s(t) be a continuous function (or
"signal") to be sampled, and let sampling be performed by measuring the value of the
continuous function every T seconds, which is called the sampling interval or the sampling
period.
Sampling frequency: The sampling frequency or sampling rate, fs, is the average number
of samples obtained in one second (samples per second), thus fs = 1/T.
Procedure:
Code:
ts=0.0002;
n=-25:1:25;
nts=n*ts;
x=exp(-1000*abs(nts));
dt=0.0002;
t=-0.005:dt:0.005;
fs=5000;
xt=x*sinc(fs*(ones(length(n),1)*t-nts'*ones(1,length(t))));
figure;
stem(n,x);
78
Computer Center
ECE Department, NIT, Kurukshetra
holdon;
plot(-25:25,xt);
title('Sampled function');
ylabel('x(t)--------->');
xlabel('t------->');
Expected Outcome:
Assignments:
Conclusion:
79
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 30
a) t*exp(-at)
b) sin(at) + cos(bt)
Apparatus Required:
Theory:
Laplace transform is an integral transform named after its discoverer Pierre-Simon Laplace.
It takes a function of a real variable t (often time) to a function of a complex variable s
(frequency). The Laplace transform is very similar to the Fourier transform. In mathematics,
Laplace's equation is a second-order partial differential equation. This is often written as:
where ∆ = ∇2 is the Laplace operator and is a scalar function.
The inverse Laplace transform takes a function of a complex variable s (often frequency)
and yields a function of a real variable t (time)..
Procedure:
Code:
clc
symstreal
symsareal
symsbreal
f= t* exp(-a*t)
m = sin(a*t) + cos(b*t)
laplace(f)
laplace(m)
Expected Outcome:
80
Computer Center
ECE Department, NIT, Kurukshetra
Assignments:
a) t*exp(-at)
b) sin(at) + cos(bt)
Conclusion:
81