1
PROGRAM NO. - 1
AIM:-INTRODUCTION TO MATLAB.
INTRODUCTION:- MATLAB is a high-performance language for technical computing. It
integrates computation, visualization, and programming in an easy-to-use environment where
problems and solutions are expressed in familiar mathematical notation. Typical uses include
Math and computation
Algorithm development
Data acquisition Modeling, simulation, and prototyping
Data analysis, exploration, and visualization
Scientific and engineering graphics
Application development, including graphical user interface building
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. This allows you to solve many technical computing problems, especially those
with matrix and vector formulations, in a fraction of the time it would take to write a program in
a scalar non interactive language such as C or FORTRAN.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide
easy access to matrix software developed by the LINPACK and EISPACK projects
MATLAB has evolved over a period of years with input from many users. In university
environments, it is the standard instructional tool for introductory and advanced courses in
mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-
productivity research, development, and analysis.
MATLAB features a family of add-on application-specific solutions called toolboxes. Very
important to most users of MATLAB, toolboxes allow you to learn and apply specialized
technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that
extend the MATLAB environment to solve particular classes of problems. Areas in which
toolboxes are available include signal processing, control systems, neural networks, fuzzy logic,
wavelets, simulation, and many others.
THE MATLAB SYSTEM:-
The MATLAB system consists of five main parts:
Development Environment:- This is the set of tools and facilities that help you use MATLAB
functions and files. Many of these tools are graphical user interfaces. It includes the MATLAB
2
desktop and Command Window, a command history, an editor and debugger, and browsers for
viewing help, the workspace, files, and the search path.
The MATLAB Mathematical Function Library:- This is a vast collection of computational
algorithms ranging from elementary functions, like sum, sine, cosine, and complex arithmetic, to
more sophisticated functions like matrix inverse, matrix eigen values, Bessel functions, and fast
Fourier transforms.
The MATLAB Language:- This is a high-level matrix/array language with control flow
statements, functions, data structures, input/output, and object-oriented programming features. It
allows both "programming in the small" to rapidly create quick and dirty throw-away programs,
and "programming in the large" to create large and complex application programs.
Graphics:- MATLAB has extensive facilities for displaying vectors and matrices as graphs, as
well as annotating and printing these graphs. It includes high-level functions for two-dimensional
and three-dimensional data visualization, image processing, animation, and presentation
graphics. It also includes low-level functions that allow you to fully customize the appearance of
graphics as well as to build complete graphical user interfaces on your MATLAB applications.
The MATLAB External Interfaces (API):- This is a library that allows you to write C and
FORTRAN programs that interact with MATLAB. It includes facilities for calling routines from
MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and
writing MAT-files.
BASICS OF MATLAB:-
MATLAB Windows:-
MATLAB works through three basic windows:
MATLAB Desktop:- This is where MATLAB puts you when you launch it. MATLAB desktop
by default consists of following sub windows:
COMMAND WINDOWS: This is the main window. It is characterized by MATLAB
command prompt (>>).
WORKSPACE: This sub window lists all variables you have generated so far and shows
their type and size.
CURRENT DIRECTORY: This is where all your files currently in use are listed. You
can run M – Files, rename them and delete them etc.
3
COMMAND HISTORY: All commands types on MATLAB prompt in the command
window gat recorded in this window.
FIGURE WINDOW:- The output of all the graphics command types in the command window
get recorded in this window.
EDITOR WINDOW:- This is where user writes, edit creates and save his program in files
called M – Files. User can use any text editor out of these tasks.
FILE TYPES:-
MATLAB can read, write several types of files. There are mainly five types of files for storing
data and programs.
M – Files:- These are the standard ASCII TEXT files with ‘.m’ extension to the file
name.
MAT – Files:- These are binary data files with ‘.mat’ extension to the file name. MAT
files are created by MATLAB when you save data with save command.
FIG – Files:- These are binary figure files with ‘.fig’ extension that can be opened again
in MATLAB as figures.
P – Files:- These are complied M – Files with ‘.p’ extension that can be executed in
MATLAB directly. These files are created with pcode commands.
MATLAB COMMANDS:-
1. clc: Clear Command Window
Syntax: clc
Description: clc clears all input and output from the Command Window display, giving
you a "clean screen".
2. input: Request user input
Syntax: user_entry = input('prompt')
Description: The response to the input prompt can be any MATLAB expression, which
is evaluated using the variables in the current workspace.
3. subplot: Create axes object in tiled positions
Syntax: subplot(m,n,p)
Description: subplot divides the current figure into rectangular panes that are numbered
row wise. Each pane contains an axes object. Subsequent plots are output to the current
pane.
4. plot: Linear 2-D plot
Syntax: plot(Y)
4
Description: plot(Y) plots the columns of Y versus their index if Y is a real number. If Y
is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the
imaginary component is ignored.
5. title: Add title to current axes
Syntax: title('string')
Description: Each axes graphics object can have one title. The title is located at the top
and in the center of the axes.
6. xlabel, ylabel, zlabel: Label the x-, y-, and z-axis
Syntax: xlabel('string')
Description:- Each axes graphics object can have one label for the x-, y-, and z-axis. The
label appears beneath its respective axis in a two-dimensional plot and to the side or
beneath the axis in a three-dimensional plot.
7. stem: Plot discrete sequence data
Syntax: stem(Y)
Description:- A two-dimensional stem plot displays data as lines extending from a
baseline along the x-axis. A circle (the default) or other marker whose y-position
represents the data value terminates each stem.
8. disp:- Display text or array
Syntax:- disp(X)
Description:- disp(X) displays an array, without printing the array name. If X contains a
text string, the string is displayed.
9. ones: Create an array of all ones
Syntax: Y = ones(n)
Description:- Y = ones(n) returns an n-by-n matrix of 1s. An error message appears if n
is not a scalar.
10. zeros:- Create an array of all zeros
Syntax:- B = zeros(n)
Description:- B = zeros(n) returns an n-by-n matrix of zeros. An error message appears
if n is not a scalar.
5
PROGRAM NO.-2
AIM:-WAP to plot the sine, cosine and tangent wave.
APPRATUS REQUIRED:- MATLAB 7.8 version
PROGRAM:-
% to genrate sinewave
x=-pi:pi/3:pi
y=sin(x)
subplot(2,2,1)
plot(x,y)
title('sinewave')
xlabel('time')
ylabel('amplitude')
%to genrate cosinewave
x=-pi:pi/3:pi
y=cos(x)
subplot(2,2,2)
plot(x,y)
title('cosinewave')
xlabel('time')
ylabel('amplitude')
%to genrate tangentwave
x=-pi:pi/3:pi
8
PROGRAM NO.- 3
AIM:-WAP to plot impulse, ramp and unit step function.
APPRATUS REQUIRED:- MATLAB 7.9 version.
PROGRAM:-
% impluse function
x=-5:1:5
n=5
i=[zeros(1,n),ones(1,1),zeros(1,n)]
subplot(2,2,1)
stem(x,i)
title('impluse function')
xlabel('time')
ylabel('amplitude')
% unit step
x=-5:1:5
n=5
r=[zeros(1,n),ones(1,n+1)]
subplot(2,2,2)
stem(x,r)
title('unit step function')
xlabel('time')
ylabel('amplitude')
% ramp function
x=-5:1:5
n=5
z=0:n
y=[zeros(1,n),z]
subplot(2,2,3)
stem(x,y)
title('ramp function')
xlabel('time')
ylabel('amplitude')
9
OUTPUTS:-
x = -5 -4 -3 -2 -1 0 1 2 3 4 5
n = 5
i = 0 0 0 0 0 1 0 0 0 0 0
x = -5 -4 -3 -2 -1 0 1 2 3 4 5
n = 5
r = 0 0 0 0 0 1 1 1 1 1 1
x = -5 -4 -3 -2 -1 0 1 2 3 4 5
n = 5
z =0 1 2 3 4 5
y = 0 0 0 0 0 0 1 2 3 4 5
WAVEFORMS:-
10
PROGRAM NO.-4
AIM:- WAP to plot convolution & multiplication of two signal.
APPRATUS REQUIRED:- MATLAB 7.9 version.
PROGRAM:-
x=0:1:10
% input first signal
y=(.6).^x
subplot(2,2,1)
stem(y)
title ('first signal')
% second input signal
z=(.7).^y
subplot(2,2,2)
stem(z)
title ('second signal')
% convolution
c=conv(y,z)
subplot (2,2,3)
stem(c)
title ('convolution')