Beruflich Dokumente
Kultur Dokumente
Introduction to MATLAB
Objective:
The purpose of this lab is to present basics of MATLAB. The main objectives are:
1. Get familiar with GUI of the software.
2. Implementation of different commands and statements.
3. Using the help command to explore a function
4. Programming in MATLAB
5. Creating and executing m-files.
6. Plotting.
Introduction:
MATLAB (Matrix laboratory) is an interactive software system for numerical computations and
graphics.
A numerical analyst called Cleve Moler wrote the first version of Matlab in the 1970s. It has since
evolved into a successful commercial software package.
As the name suggests, Matlab is especially designed for matrix computations: solving systems of
linear equations, computing eigen values and eigenvectors, factoring matrices, and so forth.
In addition, it has a variety of graphical capabilities, and can be extended through programs written
in its own programming language. Many such programs come with the system; a number of these
extend Matlab's capabilities to nonlinear problems, such as the solution of initial value problems
for ordinary differential equations.
Characteristics of MATLAB:
1. Slow (compared with fortran or C) i.e. not pre-compiled. Avoid for loops; instead use vector form.
2. Automatic memory management, i.e., you don't have to declare arrays in advance.
3. Shorter program development time than traditional programming languages such as Fortran and C.
4. Can be converted into C code via MATLAB compiler for better efficiency.
5. Many application-specific toolboxes available.
6. Main toolboxes which are going to be used are: communication toolbox, signal processing toolbox
and other supplement tool boxes.
a = 5; % numeric variable
b = [1 2 3]; % A row vector which also can be written as b = [1,2,3]
c = [4; 5; 6]; % A column vector which also can be written as c = [4 5 6]' where ' indicates
the transpose of a matrix
d = [2 4 5;3 3 3; 1 2 7]; %matrix creation 3 by 3
f = [] ; %to create an empty matrix
g = ones(3,4); %3by4 matrix with all ones
h = zeros(6); %create 6by6 matrix of all zeros
size(g); %finds the size of g matrix
size(g,1); %finds number of rows in g
size(g,2); %finds number of columns in g
length(b); %number of elements in b
length(c); %number of elements in c
length(g); %number of elements in a single row in g
k = randint(3,4); %3by4 randomly generated 0s and 1s.
l = randint(4,4,[3,5]); %4by4 randomly generated integer numbers from 3 to 5.
m = randsrc(4,4,[3:5]); %same as l
n = randsrc(4,4,[3,5]); %4by4 randomly generated integer numbers of 3 and 5 only.
o = rand(2,2]); %2by2 randomly generated numbers between 0 and 1 uniformly distributed.
p = magic(5); %5by5 magic matrix. See the details of this matrix in the detailed help. Note
that the magic matrix can only be a square matrix
%Operations on matrices
%Element extraction
%Basic functions
sum(d)
mean(d)
max(d)
min(d)
sin(d)
cos(d)
exp(d)
log(d)
%for more built in functions, see the help on list of functions
%Basic plotting
t = [0:0.001:10];
y = sqrt(100 - t.^2);
plot(t,y) %plot y vs t
hold on %to hold the previous plot
plot(t,-y,'r') %'r' means red color
hold on
plot(-t,-y,'g')
hold on
plot(-t,y,'k')
Some basic programming. Open a new m file and name it prog2 then execute the following
commands
function res = prog2( v ) %function definition. This function can be called from
%any place after passing it the argument v
%such that v is a row vector (in this %example)
%Explore different plotting commands and other useful commands from the help menu.
Assignment:
Perform the following tasks:
1. Rewrite prog2 without the for loop
2. A system is performing an encryption operation on a text data using the figure shown below.
The letters are mapped into bits in the following manner: the letter is converted into its
corresponding sequence number in the English alphabets and then that number is converted
into binary representation. A key in binary form with the same length of bits as the text is
generated randomly. Display the encrypted message in text form. The text to encrypt is your
complete name e.g. Engr Motia Rani. Hint: discard the spaces between name and the
capitalization of the first letter.