Skip to content

Commit

Permalink
29/6/2024 Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhduy09 committed Jun 29, 2024
1 parent 55c0407 commit e02ea11
Show file tree
Hide file tree
Showing 333 changed files with 3,257 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% Write a code for computing energy and power of x(t) = exp(-t)*unit_step(t)
% and decide whether x(t) is energy/power signal or not.

clear all; close all; clc;
% syms t T
% % x = exp(-t).*unit_step(t);
% x = exp(-t).*heaviside(t);
% E = int(abs(x).^2,-inf,inf)
% P = limit((1/T)*int(abs(x)^2,0,T),T,inf)
fprinf('');

% Write a code for computing energy and power of x(t) = 0.25^n.*unit_step(t)
% and decide whether x(t) is energy/power signal or not.

clear all; close all; clc;
syms n T
x = 0.25^n.*heaviside(n);
E =double( int(abs(x).^2,-inf,inf))
P = limit((1/T)*int(abs(x)^2,0,T),T,inf)
fprinf('');
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
% Generate and plot the pdf of uniform variables
clear all; close all; clc;

% Generate 10^6 datas with uniform distribution from 0 to 1
x = 2*rand(1,10^6)-1;
% x = (b-a)*rand(1,L) + a; % Generate L uniform distributed variables from the range [a, b];
% Use histogram to get the plot of distribution
nbins = 100;
histogram(x,nbins)
[h, x_axis_vec] = hist(x,nbins);
area = trapz(x_axis_vec,h);
% 2/nbins*sum(h)
pdf = h/area;
figure
bar(x_axis_vec,pdf)
ylim([0 0.6]); xlim([-1.2,1.2])
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
% This sub-function code is to represent the ramp function
% ramp(t) is t if t >=0 and, otherwise, is 0.
function result = ramp(t)
result = t.*(t>=0);
% result = t.*unit_step(t);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
% This sub-function code is to represent the rectangular function
% rect(t) is 1/T if -T/2 <= t <= T/2 and, otherwise, is 0.
function result = rect(t,T)
result = 1/T*((t>=-T/2)&(t <= T/2));
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
% This sub-function code is to represent the unit-step function
% u(t) is 1 if t >=0 and, otherwise, is 0.
function result = unit_step(t)
result = 1.*(t>=0);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
% This code is to compute 1+2+...+n
clc; clear all; close all;
n = 5;
x = 0;
for i = 1:n
i
x = x+i;
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
% Input the value and display the value
clc; clear all; close all;

x = input('Enter the value of x = ');
disp('The value of x is: ');
disp(x);

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% Array and Matrices
clc; clear all; close all;

% Dealing with complex vector
z = [i; 1+2i; 1-i]
z' % transpose with conjugate for complex numbers
z.' % transpose without conjugate for complex numbers
conj(z) % not transpose but conjugate for complex numbers

% Take or replace elements from matrices
x = zeros(2,3)
x = ones(2,3)

x = 1:2:20
x(1:2:length(x))
fliplr(x)
x([1, 5])



Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clc; clear all; close all;

x = -4:4;
[r, m] = func_ex_1(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
% Test the convergence
clc; clear all; close all;
% Code the RHS
N_vec = 1:15; f_N_vec = zeros(1,length(N_vec));
for ind = 1:length(N_vec)
N = N_vec(ind);
f_N = lec4_func_ex2(N);
f_N_vec(ind) = f_N;
end
% Code the LHS
y_LHS = pi*ones(1,length(N_vec));
% Plot
figure
plot(N_vec, f_N_vec)
hold on; grid on;
plot(N_vec, y_LHS,'x');



Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
% Redo Example 2 with the cumsum in sub-function
clc; clear all; close all;
% RHS
N = 10;
y_RHS = lec4_func_ex3(N);
% LHS
y_LHS = pi*ones(1,length(y_RHS));

% Plot
x_axis_vec = 0:N;
figure
plot(x_axis_vec, y_RHS)
hold on; grid on;
plot(x_axis_vec, y_LHS,'x');



Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
% Do the example 4 in Lecture 4.
clc; clear all; close all;
x_vec = 0:0.01:4;
N=10;
k= 0:N;

f_x_vec = zeros(1,length(x_vec));
for ind = 1:length(x_vec)
x = x_vec(ind);
f_x = (4./pi)*sum(sin(x.*(2.*k+1))./(2.*k+1));
f_x_vec(ind) = f_x;
end
plot(x_vec,f_x_vec)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
% Recursive Functions of Fibonacci: f(n)=f(n-1)+f(n-2)

function y = fib(n)
if n == 1
y = 0; % Create first initial value of Fib
end
if n == 2
y = 1; % Create second initial value of Fib
end
if n > 2
y = fib(n-1) + fib(n-2);
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
% Write a sub-function to calculate the
% RMS value and the mean-absolute value

function [rms_val, mean_val] = func_ex_1(x)
rms_val = sqrt(sum(abs(x).^2)/length(x));
mean_val = sum(abs(x))/length(x);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
clc; clear all; close all;

y = [];
for n = 1:10
y = [y fib(n)];
end
y
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function result = lec4_func_ex2(N)
k_vec = 0:N;
x_vec = ((-1/3).^k_vec)./(2*k_vec+1);
result = 2*sqrt(3)*sum(x_vec);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function result = lec4_func_ex3(N)
k_vec = 0:N;
x_vec = ((-1/3).^k_vec)./(2*k_vec+1);
result = 2*sqrt(3)*cumsum(x_vec);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function result = indi_func(x_vec,a,b)
result = (x_vec >= a)&(x_vec < b);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
% Practice to use indicator functions
clc; clear all; close all;

% Method 1
% x_vec = -2:0.001:2;
% unit_step_vec = []
% for ind = 1:length(x_vec)
% x = x_vec(ind);
% if x >= 0
% unit_step = 1;
% else
% unit_step = 0;
% end
% unit_step_vec = [unit_step_vec unit_step] ;
% end
% figure
% plot(x_vec, unit_step_vec,'x-b')

% Method 2
% x_vec = -2:0.001:2;
% unit_step_vec = (x_vec >= 0);
% figure
% plot(x_vec, unit_step_vec,'x-b')

% Method 3
% unit_step = @(x) (x >=0 );
% fplot(unit_step)

% For indicator functions
x_vec = -2:0.001:6;
a = 2; b =4;
indicator_vec = (x_vec >= a)&(x_vec < b);
figure
plot(x_vec, indicator_vec,'x-b')
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% Use indicator functions to represent a general function
clc; clear all; close all;

% Figure 1
x_vec = -2:0.001:4;
% fx_vec = 2*x_vec.*((x_vec >= 0)&(x_vec < 0.5)) + 1*((x_vec >= 0.5)&(x_vec < 1.5))+ (4-2*x_vec).*((x_vec >= 1.5)&(x_vec < 2));
fx_vec = 2*x_vec.*indi_func(x_vec,0,0.5)+indi_func(x_vec,0.5,1.5)+(4-2*x_vec).*indi_func(x_vec,1.5,2);

% Figure 2
fx_scaled = 2*fx_vec;

figure
subplot(1,2,1) % For Figure 1
plot(x_vec, fx_vec,'-b','linewidth',2)
grid on
xlabel('x'); ylabel('f(x)')
title('f(x) versus x')
ylim([0 2])
subplot(1,2,2) % For Figure 2
plot(x_vec, fx_scaled,'-r','linewidth',2)
grid on
xlabel('x'); ylabel('2*f(x)')
title('2*f(x) versus x')
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
clear all; close all; clc;

% Slide 7 in Lecture 6.
% k1 = [1; 0; -2];
% k2 = [0; 3; 1];
% x = [];
% for k = [k1,k2]
% x = [x, 3.0 + 0.1*k];
% end

% Slide 8 in Lecture 6
for k = [3, 7, 10]
x(k) = 3 + 0.1*k;
disp(x);
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
% Some methods to compute S = 1 + 1/2^2 + 1/3^2 +...
clear all; close all; clc;

% Cumsum function
N=1:1000; S1 = cumsum(1./(N.^2)); S1(1000)

% While-Loops
N=1000; k=1; S2=0;
while k<=N;
S2=S2+1/k^2; k=k+1;
end
disp(S2)

% For-Loops
n=1000; S3 = 0;
for k=1:n
S3=S3+1/k^2;
end
disp(S3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% Generate a random matrix and compute the squared norm of the matrix
% x = [x11 x12; x21 x22] and ||x||^2 = x11^2 + x12^2 + x21^2 + x22^2

clear all; close all; clc;

n = input('Number of row is ')
m = input('Number of column is ')
% Generate a random matrix n x m
A = randn(n,m);

% Compute the squared norm of the matrix
% Double-Loop
norm1 = 0;
for i=1:n
for j=1:m
norm1 = norm1 + abs(A(i,j))^2;
end
end
norm1

% Matrix-based Method
norm2 = sum(sum(abs(A).^2))

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
% Compute infinte sum S(n) = 1/1^2 + 1/2^2 +... + 1/n^2 when n goes to infinity
% with the relative error |S(n) - S(n-1)|/|S(n-1)| < 10^(-10)
% Notice that S(n) - S(n-1) = 1/n^2

clear all; close all; clc;
S = 0; n = 1;
while n > 0
error = (1/n^2)/S;
if error < 10^(-10)
break;
else
S = S + 1/n^2;
n = n + 1;
end
end
S
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
% S(n) = sum of (-1/3)^k/(2k+1) for k=0,1,2,..,n.
% with the relative error |S(n) - S(n-1)|/|S(n-1)| < 10^(-10)
% Notice that S(n) - S(n-1) = (-1/3)^n/(2n+1)
% Plot S(n) versus n

clear all; close all; clc;
S_vec = [];
S = 0; k = 0;
while k > -1
error = abs((-1/3)^k/(2*k+1))/abs(S);
if error < 10^(-5)
break;
else
S = S + (-1/3)^k/(2*k+1);
S_vec = [S_vec S];
k = k + 1;
end
end
n_vec = 0:length(S_vec)-1;
plot(n_vec,S_vec)
xlabel('n'); ylabel('S(n)')
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
% This code is to compute the square-root algorithm
% The requirement is to get all values of sequences
% such as the error |x(n)-x(n-1)|/|x(n-1)| < 10^(-6)
% Hint: get x_vec = [x(1),....,x(n)] and use while-loop

clear all; close all; clc;
a = 20;
n = 2; x_vec = [20];
while n > 0
x_vec = [x_vec sub_func_Ex6(n,a)];
error = abs(sub_func_Ex6(n,a) - sub_func_Ex6(n-1,a))/abs(sub_func_Ex6(n-1,a));
if error < 10^(-10)
break;
else
n = n + 1;
end
end
plot(1:length(x_vec), x_vec)
Loading

0 comments on commit e02ea11

Please sign in to comment.