0% found this document useful (0 votes)
5 views

Bai Tap Slide - matlab (2)

The document contains MATLAB exercises focused on plotting geometric shapes, matrix operations, logical operations, and numerical methods for solving equations. It includes examples for drawing lines and circles, matrix inversion, and solving equations using methods such as bisection, midpoint integration, and Runge-Kutta. The exercises also demonstrate comparisons between numerical solutions and exact solutions for differential equations.

Uploaded by

Hoàng Tú Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Bai Tap Slide - matlab (2)

The document contains MATLAB exercises focused on plotting geometric shapes, matrix operations, logical operations, and numerical methods for solving equations. It includes examples for drawing lines and circles, matrix inversion, and solving equations using methods such as bisection, midpoint integration, and Runge-Kutta. The exercises also demonstrate comparisons between numerical solutions and exact solutions for differential equations.

Uploaded by

Hoàng Tú Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

09:26 4/3/25 Bai Tap Slide - matlab

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)

////////////////////////////////////////////////////////////////////////////////////////////

Vẽ đường tròn bình thường

r=3;

>> teta=(-2*pi:0.001:2*pi);

>> x=r*cos(teta);

>> y=r*sin(teta);

>> plot(x,y,'r')

Bai 5. Ma tran nghich dao

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

Một số phép toán logic

Cho x=[1 3 5 3 6 7 5 3 9 9];

Y=[5 4 3 2 3 4 5 3 1 7];

Các phép toán:

x>y : neu x>y thi bang 1, con lai thi bang 0

x<y: neu x<y thi bang 1, con lai thi bang 0

x==y: neu x==y thi bang 1, con lai thi bang 0

x|y: neu x hoac y true(nghia la #0) thi bang 1, con lai bang 0;

(x>3)&x<8): neu ….thi bang 1, khong thi 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

Nhan cac gia tri le cua x voi 5: x+4.*x.*(mod(x,2)~=0)

DANG BAI TIM NGHIEM

Bai 21: Tim nghiem theo phuong phap bisection

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)

Bai 23: Tim gia tri gan dung


x = 2;
er = 1;
while er >= 0.0001
y = (x^2+2)/(2*x);
er = abs(y-x);
x = y;
end;
disp('Gia tri can bac 2 tinh theo pp gan dung la: ')
disp(x)

Bai 24: Tim nghiem tu phuong trinh viphan: PP euler


clc;
clear all;
a = 1;
r = 1;
h = 0.25;
y0 = 0;
f = @(t,y) r - a*y;
y_ex = @(t) (y0 - r/a)*exp(-a*t) + r/a;

k1 = 0; %Khoang thoi gian xet


k2 = 3;
t0 = k1;
n = (k2-k1)/h;
t(1) = t0;
y(1) = y0;
for i =1:n
t(i+1) = t(i) + h;
y(i+1) = y(i)+h*f(t(i),y(i));
end

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

EULER- BIEN DOI


clc;
clear all;
a = 1;
r = 1;
h = 0.25;
y0 = 0;
f = @(t,y) r - a*y;
y_ex = @(t) (y0 - r/a)*exp(-a*t) + r/a;

about:blank 4/13
09:26 4/3/25 Bai Tap Slide - matlab

k1 = 0; %Khoang thoi gian xet


k2 = 3;
t0 = k1;
n = (k2-k1)/h;
t(1) = t0;
y(1) = y0;
for i =1:n
t(i+1) = t(i) + h;
y1(i+1) = y(i)+h*f(t(i),y(i));
y(i+1) = y(i) + h/2*(f(t(i),y(i)) + f(t(i+1),y1(i+1)));
end

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

%So sanh cac nghiem voi nghiem giai tich


for i = 1:n
if t(i) == 1
yy1 = y(i);
else
if t(i) == 2
yy2 = y(i);
end
end
end

delta1 = abs(y_ex(1) - yy1);


delta2 = abs(y_ex(2) - yy2);
disp ('Sai so so voi nghiem giai tich tai t = 1')
disp (delta1);
disp ('Sai so so voi nghiem giai tich tai t = 2')
disp (delta2)

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

%So sanh cac nghiem voi nghiem giai tich


for i = 1:n
if t(i) == 1
yy1 = y(i);
else
if t(i) == 2
yy2 = y(i);
end
end
end

delta1 = abs(y_ex(1) - yy1);


delta2 = abs(y_ex(2) - yy2);
disp ('Sai so so voi nghiem giai tich tai t = 1')
disp (delta1);
disp ('Sai so so voi nghiem giai tich tai t = 2')
disp (delta2);

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

%So sanh cac nghiem voi nghiem giai tich


for i = 1:n
if t(i) == 1
yy1 = y(i);
else
if t(i) == 2
yy2 = y(i);
end
end
end

delta1 = abs(y_ex(1) - yy1);


delta2 = abs(y_ex(2) - yy2);
disp ('Sai so so voi nghiem giai tich tai t = 1')
disp (delta1);
disp ('Sai so so voi nghiem giai tich tai t = 2')
disp (delta2);

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’)

Bai 34. MA RZ:

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:

Chan bit 1=0

Le bit 1=1

about:blank 8/13
09:26 4/3/25 Bai Tap Slide - matlab

Cong modun: mod(x,2)==0

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

DANG BAI TAP CHUOI XUNG

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

Xung tam giac


function[t,y]=xungtamgiac(Tp,fs,Np)
%Tp chu ki xung
%fs tan so xung
%Np: so luong xung co trong chuoi
Tw=Tp/2;
tN=Np*Tp;
Ts=1/fs;
y=[];
for t=0:Ts:tN
y1=1-abs((mod(t,Tp)-Tw)/Tw);
y=[y y1];
end
t=0:Ts:tN;
plot(t,y)
end

xungtamgiac(1000,100,5);

Bai 37

about:blank 9/13
09:26 4/3/25 Bai Tap Slide - matlab

NOTE: DIEU CHE TUONG TU: bien do: am

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')

Bai kiem tra vt1- Ma AMI, do rong xung 50%

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

Dang bai 4 de D10,D9

SNR=Pt/h chia Pnoise=> tim dc Pnoise

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')

Cau 3 de3 D09

clc; clear all; close all;


f1=10;f2=15;fs=16*f2;
[t,y]=ham(10,15,16*fs)
SNR=8;
Ps=10*log(var(y));

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

clc; clear all; close all;


f1=10;f2=15;fs=16*f2;
[t,y]=ham(10,15,16*fs);
SNR=8;
Ps=10*log(var(y));
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-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')

Bai 3.Kiem tra D10Vt1


M=2;
x=randint(1,200,M);
%Dieu che DPSK
y=dpskmod(x,M);
scatterplot(y,1,0,'or')
figure
subplot(211)
plot(abs(y))
grid
title('Bieu dien bien do')
ylabel('Bien do')

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy