quotes--

The aim of education is the knowledge, not of facts, but of values

Wednesday 12 September 2012

MATLAB for beginners

MATLAB ( MATrix LABoraory ) is a high - performance language for high efficiency  Engineering and scientific numerical calculations. It was original developed to provide easy acces to matrix software developed by the LINPACK and ESIPACK matrix computation software . The MATLAB environment allows us to integrate user freindly tools with superior computational  capabilities . As a result , MATLAB is one of the most useful tools for Scientific and Engineering calculation and Computing.

You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, financial modeling and analysis, and computational biology.

To the beginner it's my suggestion that the download  the Matlab and keep practice with the problem . The Matlab Help is fully compatible to the new user and  it explains every funtion with example. Here i give some Practice to generate a continuous signal . To understand the MATLAB window see the Getting Start MATLAB tutorial in HELP. I have also linked the Introduction to Matlab ppt on my post .


% To plot a continoius time signal                          /% is use before comment
%1 sinusoidal signal
clear all;                   % it removes all variables from the workspace, releasing them from system memory.
T = 0.2;        % time period in sec
t= -1:0.01:1;    % generate a time index with interval of 0.01
x1 =  sin(2*pi*t/T);        % sine function
subplot(2,2,1),plot(t,x1);      % it create a matrix form of the plot (row column position )     
xlabel('\itt'),ylabel('x_1(\itt)');    % xlabel and ylable is use for label the axis

%2 exp signal
x2 = exp(-2*t);
subplot(2,2,2),plot(t,x2);
xlabel('\itt'),ylabel('x_2(\itt)');

% 3 sawtooth signal
t1 = -20:0.01:20;
x3 =  sawtooth(t1);
subplot(2,2,3), plot(t1,x3)
xlabel('\itt'),ylabel('x_3(\itt)');

% 4 suare signal
x4 = square (t1);
subplot(2,2,4),plot(t1,x4);
xlabel('\itt'),ylabel('x_4(\itt)');
axis([-20 20 -1.5 1.5]);     % this command provide range of axis [xmin xmax ymin ymax] in matrix form


% plot countinious signals
% 1 sinc function
clear all;
t = -20:0.01:20;
x1 =  sinc(t/2);
subplot(2,2,1),plot(t,x1);
xlabel('\itt'),ylabel('x_1(\itt)');
axis([-20 20 -1.5 1.5]);

%2 rectanfular signal
x2 = rectpuls(t/10);
subplot(2,2,2),plot(t,x2);
xlabel('\itt'),ylabel('x_2(\itt)');
axis([-20 20 -1.5 1.5]);

% 3 triangular signal
x3 =  tripuls(t/10);
subplot(2,2,3), plot(t,x3)
xlabel('\itt'),ylabel('x_3(\itt)');
axis([-20 20 -1.5 1.5]);

% 4 signum signal
x4 = sign(t/3)
subplot(2,2,4),plot(t,x4);
xlabel('\itt'),ylabel('x_4(\itt)');
axis([-20 20 -1.5 1.5]);


% plot prdouct of two function
% 1
clear all;
t = -2:0.01:2; T = .2; T1=4;
x1 = sin(2*pi*t/T).*exp(-2*t);                           % for the product of two function we use
                                                                              Multiplication: Element-wise(.*) operator
subplot(2,2,1),plot(t,x1);
xlabel('\itt'),ylabel('x_1(\itt)');

%2 rectanfular signal
T2= .3; T3=2;
x2 = 2*cos(2*pi*t/T2).*sin(2*pi*t/T3);
subplot(2,2,2),plot(t,x2);
xlabel('\itt'),ylabel('x_2(\itt)');

% 3 triangular signal
x3 =  sin(2*pi*t/T).*exp(-2*t) + sin(2*pi*t/T1).*exp(-4*t);
subplot(2,2,3), plot(t,x3)
xlabel('\itt'),ylabel('x_3(\itt)');

% 4 signum signal
x4 = sinc(t).*sin(2*pi*t/T);
subplot(2,2,4),plot(t,x4);
xlabel('\itt'),ylabel('x_4(\itt)');

to create a function x= y(t) such that 

y(t) = t+5 when -5<t<=-2
      = 11 +4t when -2 <t<= 1
     =  24-9t  when 1<t<=3
     =  t-6  when 3<t<=6

Program
function x =y(t)

x1 = t+5; x2 = 11 + 4*t; x3 = 24 - 9*t; x4= t-6;
x = x1.*(-5<t&t<=-2) + x2.*(-2<t& t<=1) + x3.*(1<t&t<=3) + x4.*(3<t&t<=6);


%this  file is save with .m extension which is a function file now the y(t) work as function you can use it in  your own program.  To use it first it must be included in your working directory 
How to use this created function is explain by the following program


clear all;
tmin= -15 ; tmax= 20;
t= tmin:0.1:tmax;
y0 = y(t);
y1 = y(t+4);
y2 = 2*y(t-3);
y3 = y(2*t);
y4 = y(2*t-3);
y5 = y(t/2);
ymax = max([max(y0),max(y1),max(y2),max(y3),max(y4),max(y5)]);
ymin = min([min(y0),min(y1),min(y2),min(y3),min(y4),min(y5)]);
subplot(3,2,1), plot(t,y0);
xlabel('\itt'),ylabel('y_0(\itt))');
axis([tmin tmax ymin ymax]);
subplot(3,2,2), plot(t,y1);
xlabel('\itt'),ylabel('y_1(\itt))');
axis([tmin tmax ymin ymax]);
subplot(3,2,3), plot(t,y2);
xlabel('\itt'),ylabel('y_2(\itt))');
axis([tmin tmax ymin ymax]);
subplot(3,2,4), plot(t,y3);
xlabel('\itt'),ylabel('y_3(\itt))');
axis([tmin tmax ymin ymax]);
subplot(3,2,5), plot(t,y4);
xlabel('\itt'),ylabel('y_4(\itt))');
axis([tmin tmax ymin ymax]);
subplot(3,2,6), plot(t,y5);
xlabel('\itt'),ylabel('y_5(\itt))');
axis([tmin tmax ymin ymax]);


% program to plot the function y(t),y(-t),odd and even parts of y(t)
clear all;
clear all;
tmin= -10 ; tmax= 10;
t= tmin:0.1:tmax;
y0 = y(t);

% time revaersal of the signal
y1= y0(end:-1:1)

ymax = max([max(y0),max(y1)]);
ymin = min([min(y0),min(y1)]);
subplot(2,2,1), plot(t,y0);
xlabel('\itt'),ylabel('y_0(\itt))');
axis([tmin tmax ymin ymax]);
subplot(2,2,2), plot(t,y1);
xlabel('\itt'),ylabel('y_1(\itt))');
axis([tmin tmax ymin ymax]);

%even part of the signal
ye = (y0+y1)/2;

%odd part of the signal
yo = (y0-y1)/2;

subplot(2,2,3), plot(t,ye);
xlabel('\itt'),ylabel('y_e(\itt))');
axis([tmin tmax ymin ymax]);
subplot(2,2,4), plot(t,yo);
xlabel('\itt'),ylabel('y_o(\itt))');


The MATLAB file for the above program is link at the ELECTRODATA

No comments:

Post a Comment