You are on page 1of 2

INTRODUCTION TO MATLAB PROGRAM

A. Basic vector and matrix operations

Generate a line vector with 9 elements:


>>a=[1 2 3 4 6 4 3 4 5]
Note: if ';' character is placed at the end of an assignment, the result is not displayed.

Determine the vector’s length:


>> length(a)
Note: the previous use of help function_name/instruction/command is recommended.

Add a constant to each element of the vector:


>>b=a+2

Generate a bidimensional plot of the elements of this vector versus their indexes and activate the grid lines:
>>plot(b, grid on

Generate a matrix:
>>A=[1 2 0; 2 5 -1; 4 10 -1]

Determine the matrix dimensions:


>>size(A)

Determine the transpose of the matrix:


>>B=A'

Multiply two matrices:


>>C=A*B

Multiply two matrices element by element:


>>C=A*B

Determine the inverse of a matrix:


>>X=inv(A)

Verify that the product between a matrix and its inverse is the identity matrix:
>>I=inv(A)*A

Determine the solutions of the equation det (λ*I-A)=0:


>>eig(A)

Display a list of all variables stored in memory:


>>whos

Show the value of a variable by entering their names:


>>A
Note : If you do not define a variable for storing the result of an operation , the result is assigned to the
temporary variable ans .

MATLAB operates with complex values:


>>j=sqrt(-1)
>>sqrt((real(s)).^2+(imag(s)).^2)

B. Two-dimensional graphic representations

Generate a vector with increasing equally-spaced elements (the lower limit is 0, the upper limit is 5, and the step
size is 0.05):
>>x=0:0.05:5; y=sin(x);
>>plot(x,y)
>>figure,bar(x,y)
>>figure,stairs(x,y)
>>figure,stem(x,y)

Generate a statistical graphical representation (a histogram) :


>>x=0:0.05:500; y=sin(x); figure, hist(y,100)

C. Spectral Analysis: the Fourier transform and the power spectral density

Generate a sine waveform in time domain with a 50 Hz frequency, sampled with a frequency of 1 kHz:
>>t=0:0.001:0.25; x=sin(2*pi*50*t);

Calculate the discrete Fourier transform of the signal:


>>X=fft(x,256);

Calculate and plot the power spectral density of the signal, based on the Fourier transform:
>>Pxx=X.*(conj(X))/256; f=1000/256*(0:127);
>>plot(f,Pxx(1:128))
>>title('DSP'), xlabel('Frequency (Hz)')

Plot the power spectral density of the signal in the [0, 100] Hz band:
>>plot(f(1:25),Pxx(1:25))
>>title ('PSD'), xlabel('Frequency (Hz)')

D. Building a user MATLAB function

Build a function to calculate the average and the standard deviation of the elements of a vector.

Open the text editor of MATLAB program.

The first line represents the function’s header .


On the following lines it is recommended to enter comments (beginning with the '%' character) about that
function. These comment lines will be displayed after the execution of help function_name.
The following lines represent the function body, where the algorithm is implemented . All variables in the
function body are local.

function [med,devst]=stat(x)
%[med,devst]=stat(x)
%med is the mean, devst is the standard deviation
%of the elements of the x vector
n=length(x);
med=sum(x)/n;
devst=sqrt(sum((x-med).^2)/n);

The file must be saved using the same name as the function ( stat.m) .

Example:
>>cd d:\sc
>>help stat
>>x=[1 2 3 4 5 6 7];
>>[med,devst]=stat(x)

Note: see if, for, while commands which are very useful in the implementation of an algorithm.

E. The SIMULINK module

The SIMULINK module allows the modeling, the simulation and the time domain analysis of a block diagram.
The file must be saved as model_name.mdl.

You might also like