You are on page 1of 10

Lab Tasks(10)

(Matlab)
Submitted to:

Respected,
Sir Tariq Mehmood
Submitted by:

Hafiz Muhammad Shafeeq


(Fa09-bce-067 )

CIIT Islamabad
Lab Tasks
Task 1:

t=0:1/100:10;
x=sin(2*t);
figure;
hold on;
plot(t,x,'--');
x1=4*sin(2*t);
plot(t,x1,'*');
x2=sin(2*t-pi/4);
plot(t,x,'*');

Task2:
clear;
clf;
tr=[-1 0 0 1 1 2 2];
yr=[0 1 0 1 0 1 0];
N=1;
c0=0.5;
t=-1:0.001:2;
y=c0*ones(size(t));
for n=-N:-1,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
for n=1:N,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
subplot(2,2,1);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
N=2;
c0=0.5;
y=c0*ones(size(t));
for n=-N:-1,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
for n=1:N,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
subplot(2,2,2);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
N=3;
c0=0.5;
y=c0*ones(size(t));
for n=-N:-1,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
for n=1:N,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
subplot(2,2,3);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
N=10;
c0=0.5;
y=c0*ones(size(t));
for n=-N:-1,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
for n=1:N,
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn*exp(j*n*2*pi*t));
end;
subplot(2,2,4);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');

Plot:
Task 3:

clc;
clear all;
t=1:1/2000:3;
x1=1;
x2=cos(2*pi*t);
x3=2*cos(2*pi*2*t);
x4=0.5*sin(2*pi*3*t);
x5=3*sin(2*pi*t);
x=x1+x2+x3+x4+x5;
subplot(2,3,1);
plot(t,x1);
subplot(2,3,2);
plot(t,x2);
subplot(2,3,3);
plot(t,x3);
subplot(2,3,4);
plot(t,x4);
subplot(2,3,5);
plot(t,x5);
subplot(2,3,6);
plot(t,x);

Plot:
Task 4:

%y(t)=t^2(u(t+1)-u(t))+(t-1)(u(t)-u(t-1))+(-1)(u(t-1)-u(t-2));
t=-2:1/100:3;
y=(t.*t).*((t>=-1)-(t>=0))+(t-1).*((t>=0)-(t>=1))+(-1)*((t>=1)-(t>=2));
plot(t,y);
grid;
xlabel('t');
ylabel('y(t)');

Plot:
Task 5:

clear;
clf;
tr=[-1 0 0 1 1 2 2];
yr=[0 1 0 1 0 1 0];
N=1;
c0=0.5;
t=-1:0.001:2;
y=c0*ones(size(t));
for n=-N:-1;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;
for n=1:N;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;

subplot(2,2,1);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
%plot truncated fourier series approximation (N=2)
N=2;
c0=0.5;
y=c0*ones(size(t));
for n=-N:-1;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;
for n=1:N;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;

subplot(2,2,2);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
%plot truncated fourier series approximation (N=3)
N=3;
c0=0.5;
y=c0*ones(size(t));
for n=-N:-1;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;
for n=1:N;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;

subplot(2,2,3);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
%plot truncated fourier series approximation (N=10)
N=10;
c0=0.5;
y=c0*ones(size(t));
for n=-N:-1;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;
for n=1:N;
cn=exp(j*pi/2)/(2*pi*n);
y=y+real(cn+exp(j*n*2*pi*t));
end;

subplot(2,2,4);
plot(t,y);
hold;
plot(tr,yr,':');
hold;
xlabel('time(seconds)');
ylabel('y(t) approximation');
Plot:

You might also like