You are on page 1of 9

Matlab Tutorial:

1. Basics:
1.) Scalar computation:
d = 36*12 + 120;
b=2^5;
b+d
2.) Matrices:
A=[1,2,3; 4,5,6;7,8,9]
A(1,1)
A(3,4)
A(2,1)=0
A
inv(A)
3.) Arrays:
x=[1 2 3]
y=[4; 5; 6]
y(3)
y(3)=0
A*y
4.) Element-wise operations:
a=[1 2 3]
b=[2 3 4]
a.*b a./b a*b
5.) Colon notation:
a=1:10
a=1:2:10
6.) Some special matrices:
A=eye(6);
B=ones(5,3);
C=zeros(4,5);

Exercise:

Find the solution to the linear equation:


[4 9 6; 1 5 7; 8 6 3 ] x = [ 1; 5; 2]
2. Programming
1.) for loop
Compute the i^2 for i=1:10
x=[];
for i=1:10
x=[x; i^2];
end
2.) while loop
i=1;
x=[];
while i<=10
x=[x;i^2];
i=i+1;
end
3.) if... else
check whether x is even or not
if (mod(x,2)==0)
disp(x is even);
else
disp(x is odd);
end
4.) Write a .m file and define a function
Now we can write a function U(t) = 1
function u=unit_step(t)
if t>=0
u=1;
else
u=0;
end

Exercise 2:
Using Matlab to compute 1^3 + 3^3 + + (2k+1)^3 + 99^3
2. Signals and Systems
1.) How to define a signal. Signal is a function of time.
Example, x(t)=sin(t).
Define a vector for time t, and then compute the vector for x.
t=[-10:0.01:10];
x=sin(t);
y=cos(t);
figure;
hold on;
plot(t,x);
plot(t,y,r);
grid;
xlabel(t);
ylabel(x and y);
title(sin(t) and cos(t));
legend(sin(t), cos(t));
2.) Give an input and a system, find the output using Matlab
y(t)=[x(t)]^2
x(t)=U(t)
t=-1:0.01:1
x=[];
for index=1:length(t)
x=[x 2*unit_step(t(index))];
end
y=x.^2;
figure;
hold on;
plot(t, x);
plot(t,y,r);

Exercise:

Define a signal x1(t)=U(t-1)-U(t-2) and x2(t)=U(2-t)U(t-1) for -5


< t <5, and plot the signal. You may use the function we write
for u(t).
3.) Using Matlab to compute the output when the impulse function of the system is
known.
x(t)=1, 0 < t < 1
h(t)=1, 0 < t < 2
% Define a function for x(t)
function x=fun_x(t)
if (t > 0 && t<1)
x=1;
else
x=0;
end
% Define a function for h(t)
function h=fun_h(t)
if (t>0 && t<2)
h=1;
else
h=0;
end
% Main Program
clear all;
close all;
t=[-5:0.001:5];
x=[];
for index=1:length(t)
x=[x fun_x(t(index))];
end
h=[];
for index=1:length(t)
h=[h fun_h(t(index))];
end
figure;
subplot(3,1,1);
plot(t,x);
axis([-5 5 -2 2]);

grid;
xlabel('t');
ylabel('x(t)');
subplot(3,1,2);
plot(t,h);
axis([-5 5 -2 2]);
grid;
xlabel('t');
ylabel('h(t)');
y=0.001*conv(x,h);
subplot(3,1,3);
plot([-10:0.001:10], y);
axis([-5 5 -2 2]);
grid;
xlabel('t');
ylabel('y(t)');

Exercise:
Please change x(t) to x(t)=t for 0< t<1, 2-t for 1<t<2, and 0
otherwise. Plot the output.
4.) Frequency domain analysis and filtering

s1(t)=sin(100t), s2(t)=cos(200t), x(t)=s1(t)+s2(t)=sin(100t)+cos(200t).


Define a low pass filter h(t), and get y(t)=sin(100t) from x(t).
clear all;
close all;
Ts=0.001;
t=[-10:Ts:10];
s1=sin(100*t);
s2=cos(300*t);
figure;
subplot(3,1,1);
hold on;
plot([0:99]*Ts,s1(1:100));
plot([0:99]*Ts,s2(1:100),'r');
%axis([-2 2 -2 2]);
grid;
xlabel('t');
%ylabel('s1 (s2)');
legend('s1(t)', 's2(t)');
x=s1+s2;
subplot(3,1,2);
plot([0:99]*Ts,x(1:100));
grid;
xlabel('t');
ylabel('x(t)=s1(t)+s2(t)');
%axis([-2 2 -3 3]);
% Fourier Transform
X_w=Ts*fft(x);
X_w=fftshift(X_w);
w1=[-(length(X_w)-1)/2:(length(X_w)-1)/2]/length(X_w)*2*pi*1/Ts;
%figure;
subplot(3,1,3)
plot(w1,abs(X_w));
axis([-400 400 0 20]);
grid;
xlabel('\omega');
ylabel('|X(i\omega)|');
% Filter
n=[-100:1:100];
h=sinc(0.2/pi*n)*0.2/pi;
figure;
subplot(2,1,1);
plot(n*Ts, h);

xlabel('t');
ylabel('h(t)');
h_w=fft(h);
H_w=fftshift(h_w);
w2=[-(length(H_w)-1)/2:(length(H_w)-1)/2]/length(H_w)*2*pi*1/Ts;
subplot(2,1,2);
plot(w2,abs(H_w));
axis([-400 400 0 2]);
grid;
xlabel('\omega');
ylabel('|H(i\omega)|');
% After filtering
figure;
subplot(3,1,1)
hold on;
plot(w1,abs(X_w));
plot(w2,abs(H_w),'r');
legend('|X(i\omega)|', '|H(i\omega)|');
axis([-400 400 0 10]);
y=conv(x,h);
subplot(3,1,2)
plot([0:99]*Ts, y(1000:1000+99));
axis tight;
Y_w=fft(y);
Y_w=fftshift(Y_w);
xlabel('t');
ylabel('y(t)');
w3=[-(length(Y_w)-1)/2:(length(Y_w)-1)/2]/length(Y_w)*2*pi*1/Ts;
subplot(3,1,3);
plot(w3,abs(Y_w)/800);
axis([-400 400 0 10]);
xlabel('\omega');
ylabel('|Y(i\omega)|');

You might also like