0% found this document useful (0 votes)
75 views81 pages

ECLR 33 (Matlab) Manual

This document outlines the objectives, outcomes and list of experiments for the MATLAB Programming lab course ECLR-33. The course aims to familiarize students with numerical computing in various fields using MATLAB. Students will learn the MATLAB interface and syntax, create scripts and programs, apply concepts of simulation using SIMULINK. The list of 10 experiments covers basic MATLAB commands, writing programs to calculate damped natural frequency, define and manipulate vectors, calculate summations and break functions into odd and even parts.

Uploaded by

KRITIKA 176
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views81 pages

ECLR 33 (Matlab) Manual

This document outlines the objectives, outcomes and list of experiments for the MATLAB Programming lab course ECLR-33. The course aims to familiarize students with numerical computing in various fields using MATLAB. Students will learn the MATLAB interface and syntax, create scripts and programs, apply concepts of simulation using SIMULINK. The list of 10 experiments covers basic MATLAB commands, writing programs to calculate damped natural frequency, define and manipulate vectors, calculate summations and break functions into odd and even parts.

Uploaded by

KRITIKA 176
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Computer Center

ECE Department, NIT, Kurukshetra

Lab Manual
ECLR-33
MATLAB Programming

Bachelor of Technology
in
Electronics & Communication Engineering

Department of Electronics & Communication Engineering


National Institute of Technology
Kurukshetra-136119
Web page: http://www.nitkkr.ac.in/sub_courses.php?id=143&id3=62

1
Computer Center
ECE Department, NIT, Kurukshetra
Vision

To impart state-of-the-art Electronics and Communication Engineering


Education and Research responsive to global challenges.

Mission

 M1: To prepare students with strong theoretical and practical knowledge by


imparting quality education.
 M2: To produce comprehensively trained and innovative graduates in
Electronics and Communication Engineering through hands on practice and
research to encourage them for entrepreneurship.
 M3: To inculcate team work spirit and professional ethics in students.

2
Computer Center
ECE Department, NIT, Kurukshetra
Program Educational Objectives (PEOs)

PEO – 1: Have a lead and successful role in their professional career.

PEO – 2: Be able to analyze real life problems and design socially accepted and
economically viable solutions in Electronics and Communication Engineering
area.

PEO –3: Be capable of lifelong learning and professional development by pursuing


higher education and participation in research and development activities.

PEO –4: Have appropriate human and technical communication skills to be a good team-
member/leaders and responsible human being

Program Outcomes (POs)


A graduate of the Electronics and Communication Engineering Program will:

PO1: Engineering knowledge: Possess knowledge of mathematics, science, engineering


fundamentals, and Electronics and Communication Engineering specialization to
solve the problems in Electronics and Telecommunication Systems.

PO2: Problem analysis: Be able to analyze complex problems in Communication systems,


Analog &Digital Electronic Systems, & DSP based systems using first principles of
mathematics, science, and engineering sciences to reach substantiated conclusions.

PO3: Design/development of solutions: Be able to design solutions for complex


Electronics and communication engineering problems and design system
components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and
environmental considerations.

PO4: Conduct investigations of complex problems: Be able to use research-based


knowledge and research methods including design of experiments, analysis and
interpretation of data, and synthesis of the information to provide valid conclusions.

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.

PO7: Environment and sustainability: Be able to understand the impact of Electronics


and communication engineering solutions in societal and environmental contexts,
and demonstrate the knowledge of, and need for sustainable development.

PO8: Ethics: Be able to apply ethical principles, commit to professional ethics in context to
Electronics and communication engineering practice.

PO9: Individual and team work: Be able to function effectively as an individual, as a


member or leader in diverse teams, and in multidisciplinary settings.

PO10: Communication: Be able to communicate effectively on complex Electronics and


communication engineering activities with the engineering community and with
society at large.

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.

Program Specific Outcomes (PSOs)


At the end of the program, the student will:
PSO1: Clearly understand the fundamental concepts of Electronics and Communication
Engineering.
PSO2: Formulate the real life problems and develop solutions in the area of semiconductor
technology, signal processing and communication systems.
PSO3: Posses the skills to communicate effectively in both oral and written forms,
demonstrating the practice of professional ethics, and responsive to societal and
environmental needs.

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

1 To study Basic MATLAB arithmetic Commands.


2 For an electrical circuit with an inductance L=0.01 mH and resistance R=100Ω,the
1 𝑅2
damped natural frequency of oscillation f=√𝐿𝐶 − 4𝐶 2 write a progoram to calculate
the frequency for different values of c varying from 0.1 to 1 in step of 0.01.
3 Write a Matlab program to define the vectors:
𝑢̅ =< 1,2,3,4 > 𝑎𝑛𝑑 𝑣̅ =< 2,3,4,5 >

Now determine the following using Matlab

a) Multiply the vector v with a scalar quantity i. e. 5v̅

b) Take the power of 5 of a vector i. e. u̅5

c) Take the power of 5 of a vector i. e. v̅ 5

d) Multiply the vectors element by element i. e. ̅̅̅̅̅̅


u.∗ v̅

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.

4 Write a Matlab program to calculate:


(i) Summation of a number k from 0 to 100 i.e. ∑𝟏𝟎𝟎
𝒌=𝟎 𝒌

(ii) Summation of a number 𝐤 from 0 to 100 i.e. ∑𝟏𝟎𝟎


𝟐
𝒌=𝟎 𝒌
𝟐

(iii) Summation of a number 1/k from 0 to 100 i.e. ∑𝟏𝟎𝟎 𝟏


𝒌=𝟏 ⁄𝒌

(iv) Summation of a number 1/𝐤 𝟐 from 0 to 100 i.e. ∑𝟏𝟎𝟎 𝟏


𝒌=𝟏 ⁄𝒌𝟐

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. Calculate using Matlab


(i) Define the symbolic variable x.
(ii) Define the symbolic function 𝑦 = 𝑒 𝑥
(iii) Define the symbolic function 𝑦 = 𝑒 −𝑥
(iv) Using the result of previous two sections, calculate 𝑓𝑜𝑑𝑑 (𝑥) and 𝑓𝑒𝑣𝑒𝑛 (𝑥) for the
function 𝑦 = 𝑒 𝑥
7. Write a Matlab program to generate different waveforms –
(i) Square wave (v) unit step function
(ii) Sine wave (vi) unit impulse function
(iii) Triangle wave (vii) A rectangular pulse of width 2
(iv) Exponential wave (viii) Unit ramp function

8. Draw graphs of the functions using Matlab


1
sin 𝑥 1 𝑥 2 +1 (10−𝑥)3 −2
(𝑖) (𝑖𝑖) + 𝑥 (𝑖𝑖𝑖) (𝑖𝑣) 1
𝑥 (𝑥−1)2 𝑥 2 −4
(4−𝑥 2 )2
9. Draw the circle of radius r=5 using Matlab.
10. Write a function in Matlab for factorial to compute the factorial n! . for n= 50.
11. Write a Matlab program to compute sum of all powers of 2 below 50 using while
command.
12. Write a Matlab program to solve the equations:
-6x - 2y + 2z = 15
-3x + 4y - 3z = 13
2x + 4y - 7z = -9

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

16. The area, A, of a triangle with sides of length a, b and c is given by 𝐴 =


√(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) where s=(a+b+c)/2. Write a Matlab function that will
accept the values a, b and c as inputs and return the value of A as output.
17. Write a Matlab program for a positive number to compute and display the even
powers of 2 less than or equal to positive number.
18. Use the command plot3(x,y,z) to plot the circular helix 𝑥(𝑡) = sin 𝑡 , 𝑦(𝑡) =
cos 𝑡 , 𝑧(𝑡) = 𝑡, 0 ≤ 𝑡 ≤ 20
19. Plot the surface defined by the function using Matlab:
𝑓(𝑥, 𝑦) = (𝑥 − 3)2 + (𝑦 − 2)2 for 2 ≤ 𝑥 ≤ 4,1 ≤ 𝑦 ≤ 3
20. Find the solution of first order ODE:
𝑥̇ = 2𝑥 + 5 sin 𝑡 , 𝑥(0) = 1
21. Plot 𝑟 2 = 2 sin 5𝑡, 𝑓𝑜𝑟 0 ≤ 𝑥 ≤ 2𝜋 using polar command in Matlab.
5
22. Plot 𝑧 = − 1+𝑥 2 +𝑦2 , 𝑓𝑜𝑟 |𝑥| ≤ 3, |𝑦| ≤ 3 using contour3 command in Matlab.
23. Compute the following integral:
1 2
𝐼 = ∫ ∫ (1 − 6𝑥 2 𝑦) 𝑑𝑥𝑑𝑦
−1 0

24. Compute the first order differential equation

𝑑𝑦
=𝑥+𝑡
𝑑𝑥

With initial condition x(0)=0

7
Computer Center
ECE Department, NIT, Kurukshetra
25. Compute the following transcendental equation using Matlab:

sin 𝑥 = 𝑒 𝑥 − 5 here initial value x0=1.

1
26. Determine the convolution of x (t) = with itself.
1+𝑡 2

27. Compute the cross correlation between the following two sequences.

X(n)= {1,2, -1,3}, h(n)= {3, -2,1,4}

28. Let x (n)=u(n)-u(n-10). Decompose x(n) into even and odd components.

29. Let x (t)=𝑒 −1000 |𝑡|

a) Sample x(t) at fs=5000 samples/sec to obtain x(n)

b) From the samples x(n), reconstruct x(t).

30. Using MATLAB symbolic math toolbox, determine the Laplace Transform of the following

a) x(t)=t𝑒 −𝑎𝑡 𝑢(𝑡)

b) x(t) = [sin(𝑎𝑡) + cos(𝑏𝑡)(𝑢(𝑡))

8
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 1

Objective: To study the Basic MATLAB arithmetic commands

Apparatus Required:

A PC installed with MATLAB software.

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.

Table 1.1 MATLAB Windows

Window Purpose

Command Window Main window, enters variables, runs programs.


Figure Window Contains output from graphic Commands.
Editor Window Creates and debugs script and function files.
Help Window Provides help information.
Command History window Logs commands entered in the Command
Window.
Workspace Window Provides information about the variables that are
used.
Current Folder Window Shows the files in the current folder.

WORKING IN THE COMMAND WINDOW:

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

Fig.1.1: Command Window

NOTES FOR WORKING IN THE COMMAND WINDOW:


 To type a command the cursor must be placed next to the command prompt ( >> ).
 Once a command is typed and the Enter key is pressed, the command is executed.
However, only the last command is executed. Everything executed previously (that
might be still displayed) is unchanged.
 Several commands can be typed in the same line. This is done by typing a comma
between the commands. When the Enter key is pressed the commands are executed
In order from left to right.
 It is not possible to go back to a previous line that is displayed in the Command
Window, make a correction, and then re-execute the command.
 A previously typed command can be recalled to the command prompt with the up
arrow key. When the command is displayed at the command prompt, it can be
modified if needed and then executed. The down-arrow key can be used to move
down the list of previously typed commands.

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.

ARITHMETIC OPERATIONS WITH SCALARS:


The symbols of arithmetic operations are:

Operation Symbol Example

Addition + 5+3
Subtraction - 5-3
Multiplication * 5*3
Right division / 5/3
Left division \ 3\5
Exponentiation 5^3
^

USING MATLAB AS A CALCULATOR:

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

In this experiment, the motive was to introduce MATLAB as calculator.

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:

A PC installed with MATLAB software.

Theory:

MATLAB is used as a calculator

Description of command used:

‘+’: This is used for Addition.

‘-’ : This is used for Subtraction.

‘*’ : This is used for Multiplication.

‘/’ : This is used for Division.

‘.*’ : This is used for element by element Multiplication.

‘./’ : This is used for element by element Division.

SQRT :It is used to do square roots of the value given to it.

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

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:

In this experiment, the motive was to introduce MATLAB as calculator.

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

Objective: Write a MATLAB program to define the vectors.

U=<1,2,3,4> and V=<2,3,4,5>

Now determine the following using MATLAB

a) Multiply the vector v with a scalar quantity.


b) Take the power of 5 of a vector u.
c) Take the power of 5 of a vector v.
d) Multiply the vectors element by element
e) First take the transpose of a vector and then multiply the vectors element by
element

Apparatus Required:

A PC installed with MATLAB software.

Theory:

Row vector-Vector that only contains a single row eg.,vector A.


Column vector - Vector that contains only a single column, eg. Vector B.
Square vector- Vector that contains equal number of row and column, eg. Vector C
and Vector D.
Vector operations like addition and subtraction can only be done on vectors that
contain equal number of rows and column.
Vector operation like multiplication can only be done if:
number of column of first vector=number of rows of second vector.
Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

In this experiment, the motive was to create arrays using MATLAB.


At the end, student should try to learn as much as they can from this experiment and
should try different Matrix calculation problems in MATLAB.

18
Computer Center
ECE Department, NIT, Kurukshetra

EXPERIMENT 4
Objective: Write a MATLAB program to calculate:

a) Summation of a number k from 0 to 100.


b) Summation of a number k^2 from 0 to 100.
c) Summation of a number 1/ k from 1 to 100.
d) Summation of a number 1/k^2 from 1 to 100.

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:
clc
clear all
close all
N = 0:100;
sum(N)
sum(N.^2)
N= 1:100;
sum(1./N)
sum(1./N.^2)
Expected Outcome:

19
Computer Center
ECE Department, NIT, Kurukshetra

Assignments:

1. For the function 𝑦 = 𝑥 3 ± 2𝑥 2 + 𝑥, calculate the value of y for the following


values of x using element by element operations: ±2, ±1,0,1,2,3,4.
𝑥 2 ±2
2. For the function𝑦 = calculate the value of y for the following values of x
𝑥+4
using element by element operations: ±3, ±2, ±1,0,1,2,3.

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:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

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:

1. Break a signal Sin(x) into even and odd parts.


Conclusion:

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

Objective: Calculate following using Matlab

a) Define the symbolic variable x


b) Define the symbolic function y = exp(x)
c) Define the symbolic function y = exp(-x)
d) Using the results of previous two sections, calculate fodd(x)and feven(x) for
the function y = exp(x)
Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:
clc
clear all
close all
x=-1:1/100:5;
y2=exp(x);
plot(x,y2)
xlabel('x-axix')
ylabel('y axix')
title('exp wave')
gridon

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:

1. Find the mathematical and physical significance of exponential signal.


2. Find the shape of the even and odd signals when we use exponential signal.

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

Objective: Write a MATLAB program to plot following waveforms

a) Sin wave
b) Cosine wave
c) Triangular wave
d) Square wave
e) Impulse function
f) Exponential function
g) Unit Ramp

Apparatus Required:

A PC installed with MATLAB software.

Theory:

Here with the help of matlab we identify the shape and behaviors of different signals.

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

2. Write a program to plot combined wave of sine and cosine signals.


3. Write a program to plot above signals in discrete form.
Conclusion:

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

Objective: Write a MATLAB program to plot following functions.

a) Sin(x)/x
b) x^2+1/x^2-4
c) [(10-x)^(1/3)-2]/(4-x^2)^1/2

Apparatus Required:

A PC installed with MATLAB software.

Theory:

To see the shape of different mathematical equations we use plot command in


matlab.here we have three mathematical function to analysis their plot.

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

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:

4. Plot mathematical function like equation of circle, parabola, hyperbola etc.


5. Marge all graph in the single graph with different color.
Conclusion:

In this experiment, the motive was to understand the shape of the different
mathematical equations.

34
Computer Center
ECE Department, NIT, Kurukshetra

EXPERIMENT 9

Objective: Write a MATLAB program to draw the circle of radius r=5.

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

1. Evaluate the expression 𝑥 2 + 𝑦 2 over the 2-D grid.


2. Create circle with concentric ring.
Conclusion:

In this experiment, the motive was to understand how to plot 2D shape of


mathematical function.

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

Objective: Write a MATLAB program to find factorial of number

Apparatus Required:

A PC installed with MATLAB software.

Theory:

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the


product of all positive integers less than or equal to n. For example, The value of 0! is
1, according to the convention for an empty product.

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

2. Solve factorial program using direct command.


3. Solve any mathematical sequence which has not fixed pattern.
Conclusion:

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:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

1. Write a program to find sum of all even powers of 2 below 50.


2. Write a program to find sum of all odd powers of 2 below 50.
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 iteration of loops
in MATLAB

40
Computer Center
ECE Department, NIT, Kurukshetra

EXPERIMENT 12

Objective: Write a MATLAB Program to solve the equations:-

-6*x-2*y+2*z=15
-3*x+4*y-3*z=13
2*x+4*y-7*z=-9
Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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

b= [-6 -2 2;-3 4 -3; 2 4 -7]


[x y z]= solve ('-6*x-2*y+2*z=15','-3*x+4*y-3*z=13','2*x+4*y-7*z=-9')

Expected Outcome:

a = 15

13

-9

b= -6 -2 2

-3 4 -3

2 4 -7

b1 =-0.1455 -0.0545 -0.0182

-0.2455 0.3455 -0.2182

-0.1818 0.1818 -0.2727

The values of x y z are -2.7273 2.7727 2.0909

Output of 2nd method

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

1. Write a program to define and solve 4 variable equations.


2. Write a program to solve other equations by both the methods and
compare the results.
Conclusion:

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

Objective: Write a MATLAB program to display the Fibonacci series up to a


particular number.

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

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:

enter number of terms : 10

The fibonacci series is : 0 1 1 2 3 5 8 13 21 34

Assignments:

1. Write a program to print Fibonacci series using while loop.


2. Write a program to print Fibonacci series for some other no of terms .
Conclusion:

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

Objective: Write a MATLAB program for the calculation of following:-

1 – 1/2 +1/3 -1/4 +1/5…….n terms

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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

1. Evaluate the expression 1-1/1! +2/2! -3/3!..........upto n terms.


2. Write a Matlab program to calculate the above two series using while loop.
Conclusion:

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

Objective: Write a MATLAB program to generalize the following:-

Sum(n)= 1+ 1/2^2 + 1/3^2 +1/4^2+…… n terms

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

Enter number of terms: 5


n=5
The sum is = 1.463611
Assignments:

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

Objective: The area A of a triangle with sides of length a, b and c is given by 𝐴 =


√s(s − a)(s − b)(s − c) , where s=(a+b+c)/2. Write a MATLAB function that will
accept the values a, b and c as inputs and return the values of A as outputs.

Apparatus Required:

A PC installed with MATLAB software.

Theory:

The area of the triangle having sides a, b and c can be calculated by Heron’s formula
given by the following expression:

𝐴 = √s(s − a)(s − b)(s − c),

where s=(a+b+c)/2

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

1. Find the area of rectangle having sides a and b.


2. Find the area of a circle having radius a.
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

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:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

4. Write a program to compute and display the even powers of 3 and 5.


5. Write a program to compute and display the odd powers of 3 and 5.

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

Objective: Use command plot3(x,y,z) to plot a circular helix.

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

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

Objective:Plot the surface defined by the function using MATLAB

𝑓(𝑥, 𝑦) = (𝑥 − 3)2 + (𝑦 − 2)2 𝑓𝑜𝑟 2 < 𝑥 < 4 𝑎𝑛𝑑 1 < 𝑦 < 3

Apparatus Required:

A PC installed with MATLAB software.

Theory:

[X,Y] = meshgrid(x,y) returns 2-D grid coordinates based on the coordinates


contained in vectors x and y. X is a matrix where each row is a copy of x, and Y is a
matrix where each column is a copy of y. The grid represented by the coordinates X
and Y has length(y) rows and length(x) columns.

[X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x), returning square grid


coordinates with grid size length(x)-by-length(x).

[X,Y,Z] = meshgrid(x,y,z) returns 3-D grid coordinates defined by the vectors x, y,


and z. The grid represented by X, Y, and Z has size length(y)-by-length(x)-by-
length(z).

[X,Y,Z] = meshgrid(x) is the same as [X,Y,Z] = meshgrid(x,x,x), returning 3-D grid


coordinates with grid size length(x)-by-length(x)-by-length(x).

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

1. Evaluate the expression 𝑥 2 + 𝑦 2 over the 2-D grid.


2. Create 3-D grid coordinates from x-, y-, and z-coordinates defined in the
interval [0,6], and evaluate the expression𝑥 2 + 𝑦 2 + 𝑧 2
Conclusion:

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

Objective:Find the solution of first order ODE.

Dy = 2x + 5sin(x) , x(0)=1

Apparatus Required:

A PC installed with MATLAB software.

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.

‘dsolve(eqn,cond)’ solves eqn with the initial or boundary condition cond.

‘dsolve(eqn,cond,Name,Value)’ uses additional options specified by one or more


Name,Value pair arguments.

Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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

Objective: To Plot r2 = 2sin5t, for 0 ≤ x ≤ 2π using polar command in matlab.

Apparatus Required:

A PC installed with MATLAB software.

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

h = polar (...) returns the line object in h.


Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

1. Find r using polar command.


2. Find lobes at various angles.
Conclusion:

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:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

1. Write a program to compute z for |x|≤ 3 using contour3.


2. Write a program to compute z for |y|≤3 and display it using contour3.
Conclusion:

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:

A PC installed with MATLAB software.

Theory:

q = dblquad (fun,xmin,xmax,ymin,ymax) calls the quad function to evaluate the double


integral fun(x,y) over the rectangle xmin<= x <= xmax, ymin<= y <= ymax. The input
argument, fun, is a function handle that accepts a vector x, a scalar y, and returns a vector
of integrand values.

q = dblquad(fun,xmin,xmax,ymin,ymax,tol) uses a tolerance tol instead of the default,


which is 1.0e-6.

q = dblquad(fun,xmin,xmax,ymin,ymax,tol,method) uses the quadrature function


specified as method, instead of the default quad. Valid values for method are @quadl or the
function handle of a user-defined quadrature method that has the same calling sequence as
quad and quadl.
Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

Code:

dblquad(@(x,y)1-6*x.*x.*y, 2, 0, -1, 1) %dblquad computes the value of double integral


functions
Expected Outcome:

65
Computer Center
ECE Department, NIT, Kurukshetra

Assignments:

1. dblquad computes the value of double integral functions

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:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

dsolve ('Dy = x + t', 'y(0)=0' )


Expected Outcome:

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

Objective: Compute the following transcendental equation using Matlab:

sinx = ex – 5 here initial value x(0) = 1.

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

Code:

a = solve('sin(x)-exp(x)+5')
%solve() is used to solve equations.
Expected Outcome:

69
Computer Center
ECE Department, NIT, Kurukshetra

Assignments:

1. Solve sinx = ex – 5 here initial value x(0) = 1


Conclusion:

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

Objective: To find the convolution of 1/ (1+t^2) with itself.


Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

In this experiment, the motive was to introduce Convolution in MATLAB.


At the end, student should try to learn as much as they can from this experiment and
should try different Convolution problems in MATLAB.

72
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 27

Objective: To compute cross correlation between two sequences (1 2 -1 3) (3 -2 1 4).

Apparatus Required:

A PC installed with MATLAB software.

Theory:

Cross-correlation is a measure of similarity of two series as a function of the displacement


of one relative to the other. This is also known as a sliding dot product or sliding inner-
product. It is commonly used for searching a long signal for a shorter, known feature.
It has applications in pattern recognition, single particle analysis, electron tomography,
averaging, cryptanalysis, and neurophysiology.
Procedure:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.
Code:

clc
clear all
close all
crosscorr([1 2 -1 3],[3 -2 1 4])
Expected Outcome:

73
Computer Center
ECE Department, NIT, Kurukshetra

Assignments:

1 Find the cross correlation between two sequences (1 2 -1 3) (3 -2 1 4).


2 Find the cross correlation between two sequences (3 5 -8) (4 -7 5)
Conclusion:

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:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

Break a signal x(n) into even and odd parts

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

Objective: To form samples of x(t) at fs=500 samples/sec to obtain x(n)

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

Form samples of x(t) at fs=500 samples/sec to obtain x(n)

Conclusion:

In this experiment, the motive was to form samples using MATLAB.


At the end, student should try to learn as much as they can from this experiment.

79
Computer Center
ECE Department, NIT, Kurukshetra
EXPERIMENT 30

Objective: Determine the laplace transformation of following function

a) t*exp(-at)
b) sin(at) + cos(bt)

Apparatus Required:

A PC installed with MATLAB software.

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:

Open MATLAB in your PC.


Open a new script.
Write the code as mentioned in the next heading in the script.
Save the script.
Run the script.
Desired result will be displayed on the command window.

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:

The Laplace transformation of following function

a) t*exp(-at)
b) sin(at) + cos(bt)

Conclusion:

In this experiment, the motive was to introduce Laplace Transformation in MATLAB.


At the end, student should try to learn as much as they can from this experiment and should
try different Laplace transformation problems in MATLAB.

81

You might also like