EXPERIMENT Basic Matlab
EXPERIMENT Basic Matlab
INTRODUCTION TO MATLAB
AIM- To understand MATLAB basic features and built in functions available in MATLAB.
Date:-
Name:-
EQUIPMENT AND SOFTWARE: Personal Computer loaded with MATLAB 7.1
OPERATING SYSTEM: Windows XP
THEORY: MATLAB (matrix laboratory) is a fourth-generation high-level programming
language and interactive environment for numerical computation, visualization and
programming. MATLAB is developed by MathWorks.
It allows matrix manipulations; plotting of functions and data; implementation of algorithms;
creation of user interfaces; interfacing with programs written in other languages, including C,
C++, Java, and Fortran; analyze data; develop algorithms; and create models and
applications.
It has numerous built-in commands and math functions that help you in mathematical
calculations, generating plots and performing numerical methods.
Local Environment Setup
If you are still willing to set up your environment, let me tell you a secret, setting up
MATLAB environment is a matter of few clicks. However, you need to download the
installer from here: MathWorks provides the licensed product, a trial version and a student
version as well. You need to log into the site and wait a little for their approval.
Once you get the download link, as I said, it is a matter of few clicks:
Figure 1.1 Installation window
Understanding the MATLAB Environment:
You can launch MATLAB development IDE from the icon created on your desktop. The
main working window in MATLAB is called the desktop. When you start MATLAB, the
desktop appears in its default layout:
Figure 1.2: Organisation of the Matlab window
MATLAB : Help
The most important function for learning MATLAB on your own.
MATLAB : Customization
File -> Preferences
This feature allows us to personalize our MATLAB experience.
MATLAB : Basic components of Environment
MATLAB has the following basic window components :
Launch Pad Window
To access all MATLAB services and tool boxes.
Command Window
To execute commands in MATLAB environment.
Current Directory Window
To quickly access files on the MATLAB path.
Figure Window
To display graphical output from MATLAB code.
Workspace Window
To view variable definitions and variable memory allocations
M - File Editor / Debugger Window
To write M-Files( includes color coded syntax features).
To debug M-Files interactively ( break points).
MATLAB Path Window
To add and delete folders to the MATLAB path.
Command History Window
Displays all commands issued in MATLAB since the last session ( for verification)
Commonly used Operators and Special Characters
MATLAB supports the following commonly used operators and special characters:
Operator Purpose
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
./ Array right-division operator.
:
Colon; generates regularly spaced elements and represents an entire row or
column.
( ) Parentheses; encloses function arguments and array indices; overrides precedence.
[ ] Brackets; enclosures array elements.
. Decimal point.
Ellipsis; line-continuation operator
, Comma; separates statements and elements in a row
; Semicolon; separates columns and suppresses display.
% Percent sign; designates a comment and specifies formatting.
_ Quote sign and transpose operator.
._ Nonconjugated transpose operator.
= Assignment operator.
Special Variables and Constants
MATLAB supports the following special variables and constants:
Name Meaning
ans Most recent answer.
eps Accuracy of floating-point precision.
i,j The imaginary unit -1.
Inf Infinity.
NaN
Undefined numerical result (not a
number).
Pi The number
Features of MATLAB
Following are the basic features of MATLAB:
It is a high-level language for numerical computation, visualization and application
development.
It also provides an interactive environment for iterative exploration, design and
problem solving.
It provides vast library of mathematical functions for linear algebra, statistics, Fourier
analysis, filtering, optimization, numerical integration and solving ordinary
differential equations.
It provides built-in graphics for visualizing data and tools for creating custom plots.
MATLAB's programming interface gives development tools for improving code
quality and maintainability and maximizing performance.
It provides tools for building applications with custom graphical interfaces.
It provides functions for integrating MATLAB based algorithms with external applications
Uses of MATLAB
MATLAB is widely used as a computational tool in science and engineering encompassing
the fields of physics, chemistry, math and all engineering streams. It is used in a range of
applications including:
Signal Processing and Communications
Image and Video Processing
Control Systems
Test and Measurement
Computational Finance
Computational Biology
RESULT: MATLAB basic features and built in functions were practiced thoroughly.
VIVA VICE:-
1. What is the Full Form Of MATLAB?
2.What is the working of Command Window?
3.What is NaN and eps?
EXPERIMENT-02
VARIABLES AND COMMANDS IN MATLAB
AIM- To understand Variables and commands in MATLAB
Date:-
Name:-
EQUIPMENT AND SOFTWARE: Personal Computer loaded with
MATLAB 7.1
OPERATING SYSTEM: Windows XP
THEORY: Variable names consist of a letter followed by any number of letters, digits or
underscore. MATLAB is case-sensitive. Variable names can be of any length.
Basic Commands
Save command : The save command is used for saving all the variables in the workspace, as
a file with .mat extension, in the current directory.
For example, save myfile
load command: You can reload the file anytime later using the load command.
In MATLAB environment, every variable is an array or matrix.
You can assign variables in a simple way.
For example, x = 3 % defining x and initializing it with a value
Multiple Assignments : You can have multiple assignments on the same line.
For example, a = 2; b = 7; c = a * b;
Long Assignments : Long assignments can be extended to another line by using an ellipses
(...).
For example, initial_velocity = 0;
acceleration = 9.8;
time = 20;
final_velocity = initial_velocity ...
+ acceleration * time
Commands for Managing a Session
MATLAB provides various commands for managing a session. The following table provides
all such commands:
Command Purpose
Clc Clears command window.
Clear Removes variables from memory.
Exist Checks for existence of file or variable.
Help Searches for a help topic.
Lookfor Searches help entries for a keyword.
Quit Stops MATLAB.
Who Lists current variables.
Whos Lists current variables (long display).
Commands for Working with the System
MATLAB provides various useful commands for working with the system, like saving the
current work in the workspace as a file and loading the file later.
It also provides various commands for other system-related activities like, displaying date,
listing files in the directory, displaying current directory, etc.
The following table displays some commonly used system-related commands:
Command Purpose
cd Changes current directory.
date Displays current date.
delete Deletes a file.
dir Lists all files in current directory.
load Loads workspace variables from a file.
path Displays search path.
pwd Displays current directory.
save Saves workspace variables in a file.
type Displays contents of a file.
what
Lists all MATLAB files in the current
directory.
The format function has the following forms used for numeric display:
Format Function Display up to
format short Four decimal digits (default).
format long 16 decimal digits.
format short e Five digits plus exponent.
format long e 16 digits plus exponents.
format bank Two decimal digits.
format + Positive, negative, or zero.
format rat Rational approximation.
Vector, Matrix and Array Commands
The following table shows various commands used for working with arrays, matrices and
vectors:
Command Purpose
length Computes number of elements.
linspace Creates regularly spaced vector.
logspace Creates logarithmically spaced vector.
max Returns largest element.
min Returns smallest element.
prod Product of each column.
reshape Changes size.
size Computes array size.
sort Sorts each column.
sum Sums each column.
eye Creates an identity matrix.
ones Creates an array of ones.
zeros Creates an array of zeros.
cross Computes matrix cross products.
dot Computes matrix dot products.
det Computes determinant of an array.
inv Computes inverse of a matrix.
rank Computes rank of a matrix.
Plotting Commands
MATLAB provides numerous commands for plotting graphs. The following table shows
some of the commonly used commands for plotting:
Command Purpose
axis Sets axis limits.
grid Displays gridlines.
plot Generates xy plot.
print Prints plot or saves plot to a file.
title Puts text at top of plot.
xlabel Adds text label to x-axis.
ylabel Adds text label to y-axis.
axes Creates axes objects.
close Closes the current plot.
close all Closes all plots.
figure Opens a new figure window.
subplot Creates plots in sub windows.
text Places string in figure.
bar Creates bar chart.
loglog Creates log-log plot.
polar Creates polar plot.
semilogx
Creates semilog plot. (logarithmic
abscissa).
semilogy
Creates semilog plot. (logarithmic
ordinate).
stairs Creates stairs plot.
stem Creates stem plot.
RESULT: MATLAB variables and commands were practiced thoroughly.
VIVA VICE:-
1.What is the command to stop the Matlab?
2.What is the command to productof each column in Matlab?
3.What do you mean by the command inv in Matlab?
EXPERIMENT-03
PLOTTING AND GRAPH IN MATLAB
AIM:- To understand Plotting and Graph in MATLAB
Date: ....................
Name: ...................
EQUIPMENT AND SOFTWARE: Personal Computer loaded with
MATLAB 7.1
OPERATING SYSTEM: Windows XP
THEORY:
plot the graph of a function,
you need to take the following steps:
1. Define x, by specifying the range of values for the variable x, for which the function is to
be plotted
2. Define the function, y = f(x)
3. Call the plot command, as plot(x, y)
Following example would demonstrate the concept. Let us plot the simple function y = x for
the range of values for x from 0 to 100, with an increment of 5.
Create a script file and type the following code:
x = [0:5:100];
y = x;
plot(x, y)
When you run the file, MATLAB displays the following plot
Let us take one more example to plot the function y = x2. In this example, we will draw two
graphs with the same function, but in second time, we will reduce the value of increment.
Please note that as we decrease the increment, the graph becomes smoother.
Create a script file and type the following code:
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
y = x.^2;
plot(x, y)
When you run the file, MATLAB displays the following plot
Change the code file a little, reduce the increment to 5:
x = [-100:5:100];
y = x.^2;
plot(x, y)
MATLAB draws a smoother graph:
Adding Title, Labels, Grid Lines and Scaling on the Graph
MATLAB allows you to add title, labels along the x-axis and y-axis, grid lines and also to
adjust the axes to spruce up the graph.
The xlabel and ylabel commands generate labels along x-axis and y-axis.
The title command allows you to put a title on the graph.
The grid on command allows you to put the grid lines on the graph.
The axis equal command allows generating the plot with the same scale factors and the
spaces on both axes.
The axis square command generates a square plot.
Example
Create a script file and type the following code:
x = [0:0.01:10];
y = sin(x);
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
grid on, axis equal
MATLAB generates the following graph:
Drawing Multiple Functions on the Same Graph
You can draw multiple graphs on the same plot. The following example demonstrates the
concept:
Example
Create a script file and type the following code:
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g, '.-'), legend('Sin(x)', 'Cos(x)')
MATLAB generates the following graph
Setting Colors on Graph
MATLAB provides eight basic colour options for drawing graphs. The following table shows
the colors and their codes:
Colour Code
White W
Black K
Blue B
Red R
Cyan C
Green G
Magenta M
Yellow Y
Example
Let us draw the graph of two polynomials
1. f(x) = 3x
4
+ 2x
3
+ 7x
2
+ 2x + 9 and
2. g(x) = 5x
3
+ 9x + 2
Create a script file and type the following code:
x = [-10 : 0.01: 10];
y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9;
g = 5 * x.^3 + 9 * x + 2;
plot(x, y, 'r', x, g, 'g')
When you run the file, MATLAB generates the following graph
Generating Sub-Plots
When you create an array of plots in the same figure, each of these plots is called a subplot.
The subplot command is for creating subplots. Syntax for the command is:
subplot(m, n, p)
where, m and n are the number of rows and columns of the plot array and p specifies where to
put a particular plot. Each plot created with the subplot command can have its own
characteristics. Following example demonstrates the concept:
Example
Let us generate two plots:
y 1= e
1.5x
sin(10x)
y2 = e
2x
sin(10x)
Create a script file and type the following code:
x = [0:0.01:5];
y1 = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
plot(x,y), xlabel('x'),ylabel('exp(1.5x)*sin(10x)'),axis([0 5 -1 1])
y 2= exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y),xlabel('x'),ylabel('exp(2x)*sin(10x)'),axis([0 5 -1 1])
When you run the file, MATLAB generates the following graph
Drawing Bar Charts
The bar command draws a two dimensional bar chart. Let us take up an example to
demonstrate the idea.
Example Let us have an imaginary classroom with 10 students. We know the percent of
marks obtained by these students are 75, 58, 90, 87, 50, 85, 92, 75, 60 and 95. We will draw
the bar chart for this data.
Create a script file and type the following code:
x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
bar(x,y), xlabel('Student'),ylabel('Score'),
title('First Sem:')
print -deps graph.eps
When you run the file, MATLAB displays the following bar chart
Drawing Contours
A contour line of a function of two variables is a curve along which the function has a
constant value. Contour lines are used for creating contour maps by joining points of equal
elevation above a given level, such as mean sea level.
MATLAB provides a contour function for drawing contour maps.
Example Let us generate a contour map that shows the contour lines for a given function g =
f(x, y). This function has two variables. So, we will have to generate two independent
variables, i.e., two data sets x and y. This is done by calling the meshgrid command.
The meshgrid command is used for generating a matrix of elements that give the range over
x and y along with the specification of increment in each case.
Let us plot our function g = f(x, y), where 5 x 5, 3 y 3. Let us take an increment of
0.1 for both the values. The variables are set as:
[x,y] = meshgrid(5:0.1:5, 3:0.1:3);
Lastly, we need to assign the function. Let our function be: x2 + y2
Create a script file and type the following code:
[x,y] = meshgrid(-5:0.1:5,-3:0.1:3); %independent variables
g = x.^2 + y.^2; % our function
contour(x,y,g) % call the contour function print -deps graph.eps
When you run the file, MATLAB displays the following contour map
Three Dimensional Plots
Three-dimensional plots basically display a surface defined by a function in two variables, g
= f(x,y). As before, to define g, we first create a set of (x,y) points over the domain of the
function using the meshgrid command. Next, we assign the function itself. Finally, we use
the surf command to create a surface plot.
The following example demonstrates the concept
Example
Let us create a 3D surface map for the function g = xe-(x2 + y2)
Create a script file and type the following code:
[x,y] = meshgrid(-2:.2:2);
g = x .* exp(-x.^2 - y.^2);
surf(x, y, g)
print -deps graph.eps
When you run the file, MATLAB displays the following 3-D map:
RESULT: MATLAB variables and commands were practiced thoroughly.
VIVA VICE:-
1.What do mean by the command plot(x,y)?
2.What is contour line?
3.How we can draw 3D maps in Matlab?
Experiment: 4
Continuous Time Basic Signals
AIM: To generate and plot various continuous time basic signals
Date:............
Name:.................
(a)Program to generate cosine wave.
Program:
%Title: program to generate cosine wave
%Date: ............
%Name:...........
f= input('enter the frequency in hertz of the cosine wave= '); %input for frequency
t=0:.0001:5; %time period of the function
y=cos(2*pi*f*t); %cosine equation in form of frequency
plot(t,y); %Plotting of the given cosine equation
ylabel ('Amplitude'); %label of y-axis
xlabel ('Time Index'); %label of x-axis
TITLE ('cosine wave'); %Title of the plot
Results: In Frequency 2 Hz
Conclusion: Cosine wave has been generated
(b) Program to generate sine wave.
Program:
%Title: program to generate sine wave
%Date: ..............
%Name........
f= input('enter the frequency in hertz of the sine wave= '); %input for frequency
t=0:.0001:5; %time period of the function
y=sin(2*pi*f*t); %cosine equation in form of frequency
plot(t,y); %Plotting of the given cosine equation
ylabel ('Amplitude'); %label of y-axis
xlabel ('Time Index'); %label of x-axis
TITLE ('sine wave'); %Title of the plot
Results:
In frequency 0.5 Hz
Conclusion: Sine wave has been generated.
(c) Program to generate tangent wave.
Program:
%Title: program to generate Tangent wave
%Date:
%Name...........
y= (-pi/2)+0.01:0.01:(pi/2)-0.01; %tangent function over domain (-pi/2 to pi/2)
plot(y,tan(y)); %Plotting of the given cosine equation
ylabel ('Amplitude'); %label of y-axis
xlabel ('Time Index'); %label of x-axis
title ('tangent wave'); %Title of the plot
Result:
Conclusion: Tangent wave has been generated over the domain [(-pi/2) to
(pi/2)].
(d) Program to generate exponential wave.
Program:
%Program to generate the exponential sequence
%Date: ......
%Name.........
n=input('Enter the duration of the signal N = '); %entering the input
a=input ('Enter the scaling factor a = '); %entering the scaling factor
t=0:.1:n-1; %time period
y=exp(a*t); %function of exponent
plot(t,y); %plot between function and time
ylabel ('Amplitude'); % label for y axis
xlabel ('Time Index'); % label for x axis
title ('Exponential Signal'); % title for plot
Result:
Scaling factor a = 2
Duration N = 5
Conclusion: Exponential function waveform has been generated.
(e) Program to generate sec wave.
Program:
% Program to generate Sec wave
% Date: ...............
%Name:............
% Frequency should be less to get clear graph near 1-10 hz
f= input('Enter the frequency in hertz of the sec wave');
t= (-pi/2)+0.01:0.01:(pi/2)-0.01; % Range of time
y=sec(2*pi*f*t); % Define y as a tan wave
plot(t,y); % Plotting of y w.r.t. t
ylabel ('Amplitude'); % Label to y axis
xlabel ('Time Index'); % Label to x axis
TITLE ('Sec wave'); % Give title to plot
Result: Frequency = 2Hz
Conclusion: Sec wave of frequency 2 Hz has been generated.
(f) Program to generate Cosec wave.
Program:
% Program to generate Cosec wave %
%Date:
%Name
% Frequency should be less to get clear graph near 1-10 hz%
f= input('Enter the frequency in hertz of the Cosec wave');
t= (-pi/2)+0.01:0.01:(pi/2)-0.01; % Range of time %
y=csc(2*pi*f*t); % Define y as a cosec wave %
plot(t,y); % Plotting of y w.r.t. t %
ylabel ('Amplitude'); % Label to y axis %
xlabel ('Time Index'); % Label to x axis %
TITLE ('Cosec wave'); % Give title to plot %
% %
Results: Frequency = 2Hz
Conclusion: Cosec wave of frequency 2 Hz has been generated
(g) Program to generate impulse signal.
Program:
%Program to generate Impulse Signal
%Date:
%Name
t=-2:2; %range of the plot
y=zeros(1,5); %number of zeroes in plot
y(1,3)=1; %function for impulse
stem(t,y); % plot for impulse and displays the data as lines
ylabel ('Amplitude'); % label for y axis
xlabel ('Time Index'); % label for x axis
TITLE ('Impulse Signal'); % title for plot
Result:
Conclusion: Impulse signal has been generated.
(h) Program to generate unit step sequence.
Program:
% Program to generate Unit Step sequence
%Date:
%Name :
n=input ('Enter the length of the step sequence N='); % Get the length
of the require sequence from the user
t=0:n-1; % defines the time axis
y=ones(1,n); % defines an 1 x n matrix which is filled with one
plot(t,y); % Plot the graph
ylabel ('Amplitude'); % Name the Y axis
xlabel ('Time Index'); %Name the x axis
TITLE ('Unit Step Signal'); % Give the title for the plot
Result: Length: 10
Conclusion: Unit step sequence of length=10 has been generated
(i) Program to generate a ramp signal.
Program:
% Program to generate a ramp signal
%Date:
%Name:
n=input('Enter the length of ramp sequence N = '); % Get the length
s=input(' Enter the slope of the ramp S = '); % Get the slope of the ramp
t=0:n-1; %time axis
plot(t,s*t); % plot for ramp signal
ylabel ('Amplitude'); %label for y axis
xlabel ('Time Index'); %label for x axis
TITLE ('Ramp signal'); %title for the plot
Result: Length =10 Slope = 2
Conclusion: Ramp signal of slope=2 has been generated.
(j) Program to generate triangular waveform.
Program:
%Program to generate triangular waveform
%Date:
%Name:
n=input ('Enter the length of the sequence N= '); %get the length from the user
t=0:n; %defines time axis
y=(-1).^t; %function for the waveform
plot(t,y); %plot between the function and time
ylabel ('Amplitude'); %label for y axis
xlabel ('Time Index'); %label for x axis
TITLE ('Triangular waveform'); %title for the plot
Result: Length: 5
Conclusion: Triangular waveform of sequence length = 5 has been generated.
Results : All The continous time signals are successfully generated.
VIVA VICE:
1. What is the command to generate time axis?
2.How we can draw the exponential function in Matlab?
3.How we can generate triangular wavefom in Matlab?
Experiment: 5
Discrete Signal Representation
AIM: To generate and plot various discrete signal and sequence.
Date :.................
Name.................
(a) Program to generate discrete signal.
x[n] = {-1 , 2, -3, -4, 0, 4, 3, 2, 1, 0, -1, 2, -3, -4 }
Program:
% Program to generate discrete signal
% Date:
%Name:
N = 0 : 13 ; % range of the sequences
x = [-1 2 -3 -4 0 4 3 2 1 0 -1 -2 -3 -4] ; % samples
plot (n, x) ; % plotting of samples
stem (n, x) ; % putting it discrete
ylabel ('x') ; % Label on y axis
xlabel ('n') ; % Label on x axis
title ('Discrete signal') ; % title of the plot
grid ; % providing grid to the plot
Result : Length of sequence = 14
Conclusion : Discrete signal with length N = 14 has been generated.
(b) Y[n] = { 0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0,
-1, -2, -3, -2, -1 }
Program:
% Program to generate the Discrete signal
% Date:
% Name :
n=-6:17; % Defines the time axis
y=[0,1,2,3,2,1,0,-1,-2,-3,-2,-1,0,1,2,3,2,1,0,-1,-2,-3,-2,-1];%Defines an amplitude of sequence
stem(n,y); % Plotting of y w.r.t. t
ylabel ('Amplitude'); % Label to y axis
xlabel ('Time Index'); % Label to x axis
TITLE ('Y(n)'); % Give title to plot
Results:
Conclusion: Discrete signal Y[n] has been generated.
(c)Program to generate discrete sequence of cosine wave.
Program:
% program to generate cosine wave
% Date:
%Name:
% frequency should be less to get clear graph near 1-10 Hz
f= input('enter the frequency in hertz of the cosine wave');
t=0:.0001:5; % range of time
y=cos(2*pi*f*t); % define y as a cosine wave
stem (t,y); % plotting of y w.r.t. t
ylabel ('Amplitude'); % label on y axis
xlabel ('Time Index'); % label on x axis
title ('cosine wave'); % title of the plot
Result:
Frequency = 2Hz
Conclusion: Cosine wave has been generated in discrete form.
Program:
%Program to generate discrete signal
% This is a plot of X1(n), X2(n),X3(n), X4(n)w.r.t time
% Date:
% Name:
t1=-4:4; % Defines the time axis
t2=-3:8; % Defines the time axis
t3= 0:11; % Defines the time axis
t4=-2:9; % Defines the time axis
y1=[-1,-1,3,4,6,7,-4,-5,-6]; % Defines an amplitude of sequence
y2=[2,5,3,6,5,3,2,1,7,5,4,3]; % Defines an amplitude of sequence
y3=[0,4,2,1,-2,-3,-1,2,3,4,6,3]; % Defines an amplitude of sequence
y4=[0,0,0,5,7,2,1,2,6,5,-1,-2]; % Defines an amplitude of sequence
subplot(2,2,1),
stem(t1,y1); % Plotting of y1 w.r.t. t1
ylabel ('Amplitude'); % Label to y axis
xlabel ('Time Index'); % Label to x axis
TITLE ('X1(n)'); % Give title to plot
subplot(2,2,2),
stem(t2,y2); % Plotting of y2 w.r.t. t2
ylabel ('Amplitude'); % Label to y axis
xlabel ('Time Index'); % Label to x axis
TITLE ('X2(n)'); % Give title to plot
subplot(2,2,3),
stem(t3,y3); % Plotting of y3 w.r.t. t3
ylabel ('Amplitude'); % Label to y axis
xlabel ('Time Index'); % Label to x axis
TITLE ('X3(n)'); % Give title to plot
subplot(2,2,4),
stem(t4,y4); % Plotting of y4 w.r.t. t4
ylabel ('Amplitude'); % Label to y axis
xlabel ('Time Index'); % Label to x axis
TITLE ('X4(n)'); % Give title to plot
Results:
Conclusion: Discrete signal X1[n], X2[n], X3[n] and X4[n] has been generated.
(e) Program to generate discrete sequence of sine wave.
Program:
% program to generate sine wave
%Date:2.20
Basis for column space
Upper triangle
THE LAPLACE TRANSFORMS
The Laplace transformation method is an operational method that can be used to find
the transforms of time functions, the inverse Laplace transformation using the partial-fraction
expansion of B(s)/A(s), where A(s) and B(s) are polynomials in s. In this chapter, we present the
computational methods with MATLAB to obtain the partial-fraction expansion of B(s)/A(s) and
the zeros and poles of B(s)/A(s).
MATLAB can be used to obtain the partial-fraction expansion of the ratio of two polyno-
%Name:
% frequency should be less to get clear graph near 1-10 Hz
f= input('enter the frequency in hertz of the cosine wave');
t=0:.0001:5; % range of time
y=sin(2*pi*f*t); % define y as a cosine wave
stem (t,y); % plotting of y w.r.t. t
ylabel ('Amplitude'); % label on y axis
xlabel ('Time Index'); % label on x axis
title ('sine wave'); % title for plot
Result :
Frequency = 2Hz
Conclusion: Sequence of sine wave is generated with maximum amplitude =1
(f) Program to generate exponential sequence.
Program:
%Program to generate the exponential sequence
%Date:
%Name:
n=25;('Enter the duration of the signal N = ');
a=10; ('Enter the scaling factor a = ');
t=0:.1:n-1; % range of time
y=exp(a*t); % define y as an exponential sequence
stem(t,y); % plotting of y w.r.t. t
ylabel ('Amplitude'); % label on y axis
xlabel ('Time Index'); % label on x axis
title ('exponential sequence'); % title for plot
Result:
Duration = 25
Scaling Factor = 2
Conclusion: It is a exponential sequence with duration 25 and scaling factor 2.
Results : All The Discrete time signals are successfully generated.
VIVA VICE :
1.How we can generate discrete signal in Matlab?
2.What do mean by the command subplot?
3.How we can generate Exponential function in Matlab?
Experiment: 6
Random Complex Number
AIM: To generate vector of random complex numbers and their operations.
Date :...................
Name:.........................
(a) Generate ten elements vector of random complex numbers and find the summation
of this vector.
>>x=rand(1,10);
>>y=rand(1,10);
>>z=x+i*y
or
>>z=complex(x,y)
>>sum(z)
(b)Construct complex data from real and imaginary components
>>A=complex(3,4)
A=
3.0000+4.0000i
>>B=complex(-1,-3)
B=
-1.0000-3.0000i
(c)To find absolute value and complex magnitude
>>abs(A)
ans=
5
>>abs(B)
ans=
3.1623
(d)To find phase angle
>>angle(A)
ans=
0.9273
>>angle(A)*180/pi
ans=
53.1301
>>angle(B)*180/pi
ans=
-108.4349
(e)To find the complex conjugate
>>conj(A)
ans=
3.00000-4.0000i
>>conj(B)
ans=
-1.0000+3.0000i
(f)To find the real part of complex numbers.
>>real(A)
ans=
3
>>real(B)
ans=
-1
(g)To find the imaginary part of complex numbers.
>>imag(A)
ans=
4
>>imag(B)
ans=
-3
Results :
VIVA VICE :
Experiment:
Laplace Transform
AIM: To generate and plot various
Date:............
Name:.................
(a)Program to generate cosine wave.
Program:
THE LAPLACE TRANSFORMS
The Laplace transformation method is an operational method that can be used to find
the transforms of time functions, the inverse Laplace transformation using the partial-fraction
expansion of B(s)/A(s), where A(s) and B(s) are polynomials in s. In this chapter, we present
the
computational methods with MATLAB to obtain the partial-fraction expansion of B(s)/A(s)
and
the zeros and poles of B(s)/A(s).
MATLAB can be used to obtain the partial-fraction expansion of the ratio of two polyno-
mials, B(s)/A(s) as follows:
B( s)
num
b(1)s n + b(2)s n1 + ... + b(n)
=
=
A( s)
den
a(1) sn + a(2)sn 1 + ... + a(n)
where a(1) 0 and num and den are row vectors. The coefficients of the numerator and
denomi-
nator of B(s)/A(s) are specified by the num and den vectors.
Hence
num = [b(1) b(2)
den = [a(1) a(2)
...
...
b(n)]
a(n)]
The MATLAB command
r, p, k = residue(num, den)
is used to determine the residues, poles, and direct terms of a partial-fraction expansion of the
ratio of two polynomials B(s) and A(s) is then given by
r(1)
r(2)
r (n)
B(s)
= k(s) +
+
+...+
s p(1)
s p(2)
s p( n)
A(s)
Experiment: 7
MATRIX IN MATLAB
AIM: Program for performing Matrix related operations.
Date:
Name:
Program:
>> A=[1 2 4; 5 3 6; 1 5 0] %input for matrix A
A =
1 2 4
5 3 6
1 5 0 %matrix A output
>> B =[5 2 9;4 8 6; 1 3 8] %Matrix B input
B =
5 2 9
4 8 6
1 3 8 %Matrix B output
(a)ADDITION
%To perform addition of matrices
>> add = A+B %Addition of two matrices A and B
Result:
add =
6 4 13
9 11 12
2 8 8
(b)SUBSTRACTION
%To perform substraction of matrices
>> sub = A-B %Substration of two matrices A and B
Result:
sub =
-4 0 -5
1 -5 0
0 2 -8
(c)MULTIPLICATION
%To perform multiplication of matrices
>> mul = A*B %Multiplication of two matrices A and B
Result:
mul =
17 30 53
43 52 111
25 42 39
(d) INVERSE
%To perform inverse of a matrix
>> R = [3 4 7;3 4 9;0 1 5] %input matrix R
R =
3 4 7
3 4 9
0 1 5
>> IN = inv(R) %inverse of matrix R
Result:
IN =
-1.8333 2.1667 -1.3333
2.5000 -2.5000 1.0000
-0.5000 0.5000 0
(e) DETERMINANT
%To find determinant of a matrix R
>> D = det(R) %determinant of matrix R
Result:
D =
-6
(f) ADJOINT
%To find adjoint of matrix R
>> adjoint = D*IN %adjoint is multiplication if determinant and inverse of matrix Lakshmi
Narain College of Technology and Science Department Of Electronics and Communication
Engineering
Digital Signal Processing Lab Report 29
Result:
adjoint =
11 -13 8
-15 15 -6
3 -3 0
(g) LOGICAL OPERATIONS
%To perform logical operations on matrices
>> x = [9 11;-3 1]
x =
9 11
-3 1
>> y = [3 10;1 9]
y =
3 10
1 9
>> z = [5 10;-3 0]
z =
5 10
-3 0
(i) >> T = x<y
Result:
T =
0 0
1 1
(ii) >> U = x>=y<z
Result:
U =
1 1
0 0
(iii) >> V = y>~z
Result:
V =
1 1
1 1
(iv) >> W= x&y&z
Result:
W =
1 1
1 0
(h) IDENTITY MATRIX
%To find the identity matrix of 3x3
>> I = eye(3) %eye(n) returns the n-by-n identity matrix
Result:
I =
1 0 0
0 1 0
0 0 1
Conclusion: All the matrix related operation has been performed.