Bai Tap Slide - matlab (2)
Bai Tap Slide - matlab (2)
Bai (them): cho toa do cac diem, ve doan thang noi cac diem do
x1 = [1 2];
x2 = [5 6];
x3 = [0 1];
x4 = [3 2];
y = [ x1 x2 x3 x4];
plot(y)
Bai (them 2): Vẽ đường tròn có bán kính bằng 2, biết rằng phương trình của
đường tròn là [x(t); y(t)] = [r cos(t); r sin(t)] với t = [0; 2π].
t=(0:0.01:2*pi);
r=2;
x=r.*cos(t);
y= r.*sin(t);
plot(x,y);
CACH 2:
t=(0:0.01:2*pi);
y='2.*sin(t)';
x='2.*cos(t)';
ezplot(x,y)
////////////////////////////////////////////////////////////////////////////////////////////
r=3;
>> teta=(-2*pi:0.001:2*pi);
>> x=r*cos(teta);
>> y=r*sin(teta);
>> plot(x,y,'r')
Cach 1:
C=[1 1 1; 1 0 1; -1 0 0]
D=inv(C)
Cach 2:
C=[1 1 1; 1 0 1; -1 0 0]
about:blank 1/13
09:26 4/3/25 Bai Tap Slide - matlab
I=eye(3,3)
D = I/C
Bai 8.
C=[1 2 3 4 10;-22 1 11 -12 4; 8 1 6 -11 5;18 1 11 6 4]
D=[(C<=10)&(C>=-10)]
>> E=D.*C
Y=[5 4 3 2 3 4 5 3 1 7];
x|y: neu x hoac y true(nghia la #0) thi bang 1, con lai bang 0;
x(x>5): so sanh(x>5) duoc vi tri cac so >5 roi lay gia tri tuong ung cua vecto x
y(x<=4): so sanh(x<=4) duoc vi tri cac so <=4 roi lay gia tri tuong ung tu vecto y
Gan cac thanh phan gia tri duong cua chuoi x = 0: ( nghia la x>0)
- Y=x.*(x<=0) % chuyen nguoc lai x<=0 vi true thi= 1, false=0, roi nhan lai voi x
Nhan 2 cac gia tri la boi so cua 3: x+x.*(rem(x,3)==0) :cac so la boi cua 3 =1, con lai
=0, roi nhan no voi x xong roi +x
Bai21(a,b)
function x=bai21(a,b)
[[[[a= input('a= ');
b= input ('b= ');
]]]
about:blank 2/13
09:26 4/3/25 Bai Tap Slide - matlab
f=@(x)x.^2-2;
er=1;
while abs(er)> 1e-4
x0=(a+b)/2;
if f(x0)*f(a)>0;
a=x0;
else b= x0;
end
er=(b-a)/b;
end;
disp(' Nghiem phuong trinh la: ');
disp(x0);
end
Cach 2
clear all;
close all;
a=-1;
b=3;
f=@(x)(x^3)/3+4*(x^2)+x-6;
x0=(a+b)/2;
x1=0;
x2=3;
if abs(f(x1))<=1e-4
no=x1;
break;
else
if abs(f(x2))<=1e-4
no=x2;
break;
else
while (b-a)/b > 1e-4
if abs(f(x0)) < 1e-4
break;
else
if f(x0)*f(a) > 0
a=x0;
else b=x0;
end
x0=(a+b)/2;
end
end
end
end
no=x0
Bai 22: Tim nghiem theo phuong phap tich phan Midpoid
clc; clear all;
close all;
a=-1.5;
b=1.5;
f=@(x)4*x.^3*2*exp(x)*cos(x);
N=100;
h=(b-a)/N;
I=0;
for k=1:N
xj=a+(k-1/2)*h;
I=I+f(xj);
end
about:blank 3/13
09:26 4/3/25 Bai Tap Slide - matlab
I=I*h;
disp('Gia tri tich phan gan dung tinh theo pp midpoit la: ')
disp(I)
plot(t,y,'ro-','linewidth',2);
hold on
plot(t,y_ex(t),'b*-','linewidth',2);
title('Euler bien doi','fontsize',15,'color','b');
legend('Nghiem theo Euler bien doi','Nghiem giai tich',2);
xlabel('t','fontsize',15,'color','b');
ylabel('Nghiem','fontsize',15,'color','b');
grid on
about:blank 4/13
09:26 4/3/25 Bai Tap Slide - matlab
plot(t,y,'ro-','linewidth',2);
hold on
plot(t,y_ex(t),'b*-','linewidth',2);
title('Euler bien doi','fontsize',15,'color','b');
legend('Nghiem theo Euler bien doi','Nghiem giai tich',2);
xlabel('t','fontsize',15,'color','b');
ylabel('Nghiem','fontsize',15,'color','b');
grid on
RK-3:
% % Giai phuong trinh vi phan su dung Runge-Kutta bac 3
clc; clear all; close all;
a = 1;
r = 1;
y0 = 0;
f = @(t,y) r - a*y;
y_ex = @(t) (y0 - r/a)*exp(-a*t) + r/a;
k1 = 0;
k2 = 3;
h = 0.25;
t0 = k1;
n = (k2-k1)/h;
t(1) = t0;
y(1) = y0;
about:blank 5/13
09:26 4/3/25 Bai Tap Slide - matlab
for k = 1:n
k1 = h*feval(f,t(k),y(k));
k2 = h*feval(f,t(k)+2/3*h,y(k)+2/3*k1);
k3 = h*feval(f,t(k)+2/3*h,y(k)+2/3*k2);
y(k + 1) = y(k) + (2*k1 + 3*k2 + 3*k3)/8; % RK-4
t(k+1) = t(k) + h;
end
plot(t,y,'ro--','linewidth', 4)
hold on
plot(t, y_ex(t),'b*-','linewidth', 2)
legend('3th Runge-kutta', 'Exact',2)
title('Bieu dien nghiem phuong trinh vi phan bang Runge-Kutta bac
3','Fontname','VnTime','color','b','Fontsize',11);
grid on
RK4:
% % Giai phuong trinh vi phan su dung Runge-Kutta bac 4
%
clc; clear all; close all;
a = 1;
r = 1;
y0 = 0;
f = @(t,y) r - a*y;
y_ex = @(t) (y0 - r/a)*exp(-a*t) + r/a;
k1 = 0;
k2 = 3;
h = 0.25;
t0 = k1;
n = (k2-k1)/h;
t(1) = t0;
y(1) = y0;
for k = 1:n
k1 = h*feval(f,t(k),y(k));
k2 = h*feval(f,t(k)+h/2,y(k)+k1/2);
k3 = h*feval(f,t(k)+h/2,y(k)+k2/2);
k4 = h*feval(f,t(k)+h,y(k)+k3);
y(k + 1) = y(k) + (k1 + 2*k2 + 2*k3 + k4)/6; % RK-4
about:blank 6/13
09:26 4/3/25 Bai Tap Slide - matlab
t(k+1) = t(k) + h;
end
plot(t,y,'ro--','linewidth', 4)
hold on
plot(t, y_ex(t),'b*-','linewidth', 2)
legend('4th Runge-kutta', 'Exact',2)
title('Bieu dien nghiem phuong trinh vi phan bang Runge-Kutta bac
4','Fontname','VnTime','color','b','Fontsize',11);
grid on
RK-4
% Bai 11
% % Giai phuong trinh vi phan su dung Runge-Kutta bac 4
clc; clear all; close all;
f = @(t,y) 2*exp(t)-2*y;
y_exact = @(t) 2/3*exp(t) + 1/3*exp(-2*t);
a = 0; b = 3; h = 0.25;
y0 = 1; t0 = a;
n = (b-a)/h;
t(1) = t0;
y(1) = y0;
for k = 1:n
k1 = h*feval(f,t(k),y(k));
k2 = h*feval(f,t(k)+h/2,y(k)+k1/2);
k3 = h*feval(f,t(k)+h/2,y(k)+k2/2);
k4 = h*feval(f,t(k)+h,y(k)+k3);
y(k + 1) = y(k) + (k1 + 2*k2 + 2*k3 + k4)/6; % RK-4
t(k+1) = t(k) + h;
end
plot(t,y,'ro--','linewidth', 3)
hold on
plot(t, y_exact(t),'bs-','linewidth', 2)
legend('4th Runge-kutta', 'Exact',2)
title('Bieu dien nghiem phuong trinh vi phan bang Runge-Kutta bac
4','Fontname','VnTime','color','b','Fontsize',12);
grid
for i=1:n
if t(i)==2
about:blank 7/13
09:26 4/3/25 Bai Tap Slide - matlab
yy = y(i);
end
end
delta = abs(y_exact(2)-yy);
disp('Sai so tuyet doi tai x = 2 la:');
disp(delta)
Chuong 4
Bai 31.
CHU Y:
Chuyen tu thap phan sang nhi phan: vi du cho 1 vecto hang a, muon chuyen thanh cac
cot so nhi phan: b=de2bi(a’,’left-msb’)
Chuyen nguoc lai tu nhi phan sang cac cot so thap phan: a=bi2de(b,’left-msb’)
x = randint(1,128);
y=reshape(x,length(x)/4,4)
z=bi2de(y’)
data=[0 1 1 0 1 0 1 0];
fs=1000;
for t=0:length(data)*fs-1
k = fix(t/fs)+1;
m(t+1)=data(k);
end
plot(m)
xlabel('time-ns')
Bai 35:
Le bit 1=1
about:blank 8/13
09:26 4/3/25 Bai Tap Slide - matlab
a=(1:15);
i=de2bi(a','left-msb')
g=[1 0 0 0 1 1 1;
0 1 0 0 1 1 0;
0 0 1 0 1 0 1;
0 0 0 1 0 0 1]
c=mod((i*g),2)
end
VE DANG XUNG e mũ
clc
clear all
R = 1e3;
Tp = 1/R;
fs = 16*R;
ts = 1/fs;
t = 0:ts:5*Tp;
for i = 1:length(t)
tm = mod(t(i),Tp);
y(i) = exp(-4*tm/Tp);
end
plot (t,y)
grid
xungtamgiac(1000,100,5);
Bai 37
about:blank 9/13
09:26 4/3/25 Bai Tap Slide - matlab
Tan so: fm
Pha: pm
clear all;
clc;
fc=300;
fs=16*fc;
A=2;
t=[0:1/fs:1];
s=2.*cos(20*pi*t+pi/4)+cos(30*pi*t);
subplot(311)
plot(t,s)
title('Tin hieu dau vao');
y=ammod(s,fc,fs,A);
subplot(312)
plot(t,y)
title('Tin hieu sau khi dieu che');
k=amdemod(y,fc,fs,A);
subplot(313)
plot(t,k)
title('Tin hieu sau giai dieu che');
Bai 38: MA NRZ
data=[0 1 1 0 1 0 1 0];
fs=1000;
nrz=2*data-1;
for t=0:length(data)*fs-1
k = fix(t/fs)+1;
m(t+1)=nrz(k);
end
plot(m)
xlabel('time-ns')
clc
clear all
d=[1 0 0 1 1 0 1 0 0 1];
h=find(d==1);
for i=1:length(h)
if mod(i,2)==0
d(h(i))=-1;
else d(h(i))=1;
end
end
R=10e6;
Tb=1/R;%Chu kì bit
Timewindow=Tb*length(d);%Khoang thoi gian mo phong
fs=1000;%Taan so lay mau
t=0:Tb/fs:Timewindow-Tb/fs;%vector thoi gian
%y=zeros(length(t));
for i=1:length(t)
if (mod(t(i),Tb)>=0.5*Tb)
y(i)=0;
else
about:blank 10/13
09:26 4/3/25 Bai Tap Slide - matlab
k=floor(t(i)/Tb)+1;
y(i)=d(k);
end
end
y_AWGN=awgn(y,3);
plot(t,y_AWGN,'r')
hold on
plot(t,y,'linewidth',2);
axis([min(t) max(t) -1.5 1.5]);
xlabel('t')
ylabel('A')
title('Xung AMI-50%')
grid on
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Bai tap
Thay vao cong thu trong Chuong 5. Voi Pnoise=sqrt(Var(y)/2) => tim ra noise. Sau
lay noise+y de ra tin hieu co ca noise
Bai4_De1_D10
M=4;
x=randint(1,1000,M);
h=modem.pskmod(M);
h.symbolorder =('gray');
y=modulate(h,x);
SNRdB=10;
Ps=10*log(var(y)); %Chuyen cong suat tin hieu sang don vi dBW;
ynoise=awgn(y,SNR,Ps);
h=scatterplot(y,1,0,'or');
hold on
scatterplot(ynoise,1,0,'xb',h);
title('Bieu do chom sao cua tin hieu')
legend('Khong qua AWGN','Sau khi qua AWGN')
about:blank 11/13
09:26 4/3/25 Bai Tap Slide - matlab
y_noise=awgn(y,SNR,Ps);
subplot(211)
plot(t,y)
title('Tin hieu duoc tao ra')
subplot(212)
plot(t,y_noise)
title('tin hieu sau khi qua kenh AWGN')
Cau4 de 3 D09
Cau4-de2-D10
clc; clear all; close all;
M=4;
x=randint(1,1000,M);
%Dieu che ban tin
h=modem.qammod(M);
h.symbolorder=('gray');
y=modulate(h,x);
%Them AWGN
SNR=10;
Ps=10*log(var(y)); %bien doi cong suat tin hieu sang dBW
y_noise=awgn(y,SNR,Ps);
h=scatterplot(y,1,0,'or')
hold on
scatterplot(y_noise,1,0,'xb',h)
title('Bieu do chom sao cua tin hieu')
about:blank 12/13
09:26 4/3/25 Bai Tap Slide - matlab
subplot(212)
stairs(angle(y))
grid
title('Bieu dien pha')
ylabel('Pha')
about:blank 13/13