MATLAB programs (1)
MATLAB programs (1)
INSTITUTE OF TECHNOLOGY
SCHOOL OF ELECTRICAL AND COMPUTER
ENGINEERING
Prepared By
RAMESH HULIKAL RANGASWAMY
LECTURER
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming environment.
MATLAB has many advantages compared to conventional computer languages (e.g. C,
FORTRAN) for solving technical problems.
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. It has powerful built-in routines that enable a very wide variety of computations.
It also has easy to use graphics commands that make the visualization of results immediately
available. Specific applications are collected in packages referred to as toolbox.
There are toolboxes for signal processing, symbolic computation, control theory, simulation,
optimization, and several other fields of applied science and engineering.
>>v= [1 4 7 10 13]
>>w=[1;4;7;10;13]
1 2 3
4 5 6
7 8 9
Operations on Vectors
Note: to get the square of each element we use: X.^2 NOT X^2
Where the dot operator(.) means element by element operation
Vector Multiplication
What is the difference?
Z = X*Y -> (the inner dimension of the 2 vectors must agree: one row and the other
column)
Z = X.*Y ->vector! (X and Y dimensions must agree)
Define 2 vectors and multiply them with and without the dot operator
Plotting 2 vectors
Note: minimizing the step size will lead to very smooth curve for the relationship
and vice versa.
Simply we use the plot order to plot the vectors against each other.
Plot (indep_var , depen_var)
Exercise: plot the average temperature of the months of the year to their order
according to the following table
Solution:
>> Month=[1:1:12];
>> Temperature=[ 17 19 22 24 27 30 34 35 31 26 23 20];
>> plot(Month, Temperature)
The result is a continuous curve(Interpolation), but do we need that really in this
application?
Use stem(Month,Temperature) for discrete sample plotting.
Practical 01:
Aim: Write a MATLAB program to generate the following elementary signals:
i) Unit step sequence ii) Sine signal iii) Unit step signal
iv) Cosine signal v) Unit ramp vi) Exponential signal
(i) MATLAB program to generate Unit Step sequence.
clc;
clear all;
close all;
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 ones
stem(t,y); %displays the data as lines
ylabel ('Amplitude'); % name the Y axis
xlabel ('Time '); %Name the x axis
title ('Unit Step Signal'); % Giving the title for the plot
(ii) MATLAB program to generate Unit Step signal.
clc;
clear all;
close all;
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 '); %Name the x axis
title ('Unit Step Signal'); % Giving the title for the plot
(iii) MATLAB program to generate Unit ramp Signal.
clc;
clear all;
close all;
Practical No: 06
IMPULSE RESPONSE of a given system
a. PROGRAM:IMPLULSE RESPONSE USING DECONV FUNCTION
clc; % clear screen
close all % close all figure windows
clear all; % clear work space
y = input('Output sequence y(n) of the system = '); % enter the output sequence
x = input('Input sequence x(n) of the system = ');% enter the input sequence
[h,r] = deconv(y,x); % deconvolute output and input to get the impulse response
disp('Impulse response of the system is = ');
disp(h); % display result
N= length(h); % find the length of h
n=0:1:N-1; % define time axis
stem(n,h); % plot the impulse response
xlabel('n'); % label x axis
ylabel('h(n)'); % label y axis
title('Impulse Response of the system'); % graph title
% verification
yv = conv(x,h)+ r;% verification of result using convolution of input and impulse
to get output
disp('Verified output sequence y(n) is');
disp(yv); % display the value of output
OUTPUT:
Output sequence y(n) of the system = [1 -3/4]
Input sequence x (n) of the system = [1 -3/4 1/8]
Impulse response of the system = [1 0 –0.125 -0.0937 -0.0546]