Sie sind auf Seite 1von 13

1. Solusi no 1.

a. Uniform (0,1) Random Variable


Matlab code :

n=1000;
x=rand (1,n); %Generate Uniform Random Variable
k=0:0.1:1;
y=normpdf(k,mean(x),std(x));%Y=NORMPDF(X,MU=mean(x),SIGMA=std(x))
[N,h]=hist(x,15); %Get the information for the histogram
N=N/(h(2)-h(1))/n;%Change bar height to make it correspond to
the theoritical density
bar(h,N,1,'w') %Bar function plot
hold on
plot (k,y)
hold off
xlabel ('X')
ylabel ('f(x)-Uniform (0,1)')

Hasil Simulasi
PDF dan Histogram Uniform(0,1) Random Variabel



b. Uniform (a,b) Random Variable
Misal a=
Matlab code :

n=1000;
x=rand (1,n); %Generate Uniform Random Variable
a=2;b=5; %Set Value for Uniform (a,b)=U(2,5)
z=(b-a)*x+a; %Random Varible for U(2,5)
mean =(a+b)/2; %Set Expexted Value
s_dev=((b-a)/12)^0.5; %Set Deviation Standard
k=0:0.1:5;
y=normpdf(k,mean,s_dev); %Y = NORMPDF(X,MU=(a+b)/2,SIGMA=((b-
a)/12)^0.5)
[N,h]=hist(z,15); %Get the information for the histogram
N=N/(h(2)-h(1))/n;%Change bar height to make it correspond to
the theoritical density
bar(h,N,1,'w') %Bar function plot
hold on
plot (k,y)
hold off
xlabel ('X')
ylabel ('f(x)-Uniform (a,b)')

Hasil Simulasi
PDF dan Histogram Uniform(0,1) Random Variabel




c. Gaussian(, ) Random Variabel
Matlab code :

n=1000;
x=randn(1,n); %Generate Random Variable
m=1; %Mean = 1
sd=2; %Standard Deviation = 2
z=m+x*sd; %r.v value Gaussian with mean=1, Standard
Deviation =2
k=-8:0.1:8;
y=normpdf(k,m,sd); %Y = NORMPDF(X,MU,SIGMA)
[N,h]=hist(z,15); %Get the information for the histogram
N=N/(h(2)-h(1))/n; %Change bar height to make it correspond to
the theoritical density
bar(h,N,1,'w') %Bar function plot
hold on
plot (k,y)
hold off
xlabel ('X')
ylabel ('f(x)-Gaussian (1,2)')


Hasil Simulasi
PDF dan Histogram Gaussian(,)Random Variabel
= 1, =2





d. Rayleigh (a) Random Variabel
Matlab Code :

n=1000;
x=randn(1,n); %Generate Uniform Random Variable x
y=randn(1,n); %Generate Uniform Random Variable y
z = (x + j*y); % complex random variable

% probability density function of abs(z)
zNum = [0:0.01:7];
sigma2 = 1;
pz = (zBin/sigma2).*exp(-(zNum.^2)/(2*sigma2)); % pdf theory
[N h] = hist(abs(z),15); % simulation
N=N/(h(2)-h(1))/n;%Change bar height to make it correspond to
the theoritical density
bar(h,N,1,'w') %Bar function plot
hold on
plot(zNum,pz,'k')
hold off
xlabel('z');
ylabel('probability density, p(z)');
title('Probability density function of abs(z)' )
axis([0 7 0 0.7]);

Hasil Simulasi
PDF dan Histogram Rayleigh Random Variabel





e. Eksponensial() Random Variabel
Matlab Code :

n=1000;
lamda =2;
rv=rand(1,n); %Generate Random Variable
X=-log(rv)/lamda;
l=0:0.1:5; %axis value
y=exppdf(l,1/2); %Y = EXPPDF(X,MU=1/2), pdf exponensial
[N,h]=hist(X,20); %Get the information for the histogram
N=N/(h(2)-h(1))/n; %Change bar height to make it correspond
to the theoritical density
plot (l,y)
hold on
bar(h,N,1,'w') %Bar function plot
hold off
xlabel ('X')
ylabel ('f(x)-Exponensial ')

Hasil Simulasi
PDF dan Histogram Rayleigh Random Variabel




f. Erlang(n,) Random Variabel
Matlab Code
k=1000;
n=3;
lamda=2;
rv=rand(n,k); %Generate Random Variable
LogU=-log(rv)/lamda;
X=sum(LogU);
[N,h]=hist(X,15); %Get the information for the histogram
N=N/(h(2)-h(1))/k; %Change bar height to make it correspond
to the theoritical density
x=0:0.1:6;
y=gampdf(x,n,1/lamda); %Erlang is the same with gamma pdf
%y=((lamda^n)/factorial(n))*(x.^(n-1)).*exp(-lamda*x);
bar(h,N,1,'w')
hold on
plot (x,y,'k')
hold off
xlabel ('X')
ylabel ('f(x)-Erlang ')

Hasil Simulasi
PDF dan Histogram Rayleigh Random Variabel







Solusi no 2.
1. Random Walk
Berbagai kemungkinan arah pergerakan dari Model Random Walk


Matlab Code

r=[0 0]; % Initial condition
for t= 0:0.1:60 % Setting for t= 1 minutes, Time Step = 0.1
X1=rand(1,2); % Generate random number
X=X1>0.5; % Set Value <0.5 =0 and >0.5=1
if X==1
rnew=r+[1 0]; %X+ Direction
elseif X(1)==1
rnew=r+[0 1]; %Y+ Direction
elseif X(2)==1
rnew=r+[-1 0]; %X- Direction
else
rnew=r+[0 -1]; %Y- Direction
end

hold on;
plot([r(1) rnew(1)],[r(2) rnew(2)]); %get animation
drawnow ;
r=rnew; %Update position
end

Hasil Simulasi :
















Model pergerakan Random Walk yang lain :
Matlab Code :
K=1000; %1000 step
r=1;
u=19;
X=[0 0]; %Posisi awal
for n=1:K
s=ceil(u*rand); %Generate untuk posisi acak
Z=X(n,:)+r*[cos(2*pi*s/u) sin(2*pi*s/u)]; %Setting sudut pergerakan
X=[X;Z];
end
plot(X(:,1), X(:,2))
hold on
plot(0,0,'o')
hold off
axis equal

Hasil Simulasi :



2. Random waypoint:
Posisi awal
Random Way Point Model







Matlab Code :
clear all;
s_input = struct('V_POSITION_X_INTERVAL',[10 30],...%(m)
'V_POSITION_Y_INTERVAL',[10 30],...%(m)
'V_SPEED_INTERVAL',[0.2 2.2],...%(m/s)
'V_PAUSE_INTERVAL',[0 1],...%pause time (s)
'V_WALK_INTERVAL',[2.00 6.00],...%walk time (s)
'V_DIRECTION_INTERVAL',[-180 180],...%(degrees)
'SIMULATION_TIME',500,...%(s)
'NB_NODES',20);
s_mobility = mobility(s_input); %Use function : mobility
timeStep = 0.1;%(s)
animasi(s_mobility,s_input,timeStep); %Draw animation, use function :
animasi

Fungsi mobility.m

function s_mobility = mobility(s_input)
%The Random Waypoint mobility model.
global s_mobility_tmp;
global nodeIndex_tmp;

s_mobility.NB_NODES = s_input.NB_NODES;
s_mobility.SIMULATION_TIME = s_input.SIMULATION_TIME;
for nodeIndex_tmp = 1:s_mobility.NB_NODES
%Initialize:
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION = [];

previousX =
unifrnd(s_input.V_POSITION_X_INTERVAL(1),s_input.V_POSITION_X_INTERVAL(2));
previousY =
unifrnd(s_input.V_POSITION_Y_INTERVAL(1),s_input.V_POSITION_Y_INTERVAL(2));
previousDuration = 0;
previousTime = 0;

Out_setRestrictedWalk_random_waypoint(previousX,previousY,previousDuration,
previousTime,s_input);
%%Promenade
while (s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end) <
s_input.SIMULATION_TIME)
if (s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING(end) ==
false)
previousX =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(end);
previousY =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(end);
previousDuration =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end);
previousTime =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end);

Out_setRestrictedWalk_random_waypoint(previousX,previousY,previousDuration,
previousTime,s_input);
else
%%%Node is taking a pause:
previousDirection =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(end);
previousSpeed =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(end);
previousX =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(end);
previousY =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(end);
previousTime =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end);
previousDuration =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end);
distance = previousDuration*previousSpeed;
%%%
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end+1,1) =
previousTime + previousDuration;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(end+1,1)
= (previousX + distance*cosd(previousDirection));
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(end+1,1)
= (previousY + distance*sind(previousDirection));
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(end+1,1)
= 0;

s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(end+1,1) = 0;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING(end+1,1)
= false;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end+1,1) =
Out_adjustDuration_random_waypoint(s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_
TIME(end),unifrnd(s_input.V_PAUSE_INTERVAL(1),s_input.V_PAUSE_INTERVAL(2)),
s_input);
end
end
%To have speed vectors as well rather than
%only the scalar value:
nb_speed =
length(s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE);
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_X =
zeros(nb_speed,1);
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_Y =
zeros(nb_speed,1);
for s = 1:nb_speed
speed =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(s);
direction =
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(s);
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_X(s) =
speed*cosd(direction);
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_Y(s) =
speed*sind(direction);
end

%To remove null pauses:
v_index = s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(1:end-1)
== 0;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(v_index) =
[];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_X(v_index) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_Y(v_index) = [];

%%%%%%%%%%%%%%%%%%To remove the too small difference at the end, if
%%%%%%%%%%%%%%%%%%there is one:
if ((s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end) -
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end-1)) < 1e-14)
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(end) =
[];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_X(end) = [];
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_Y(end) = [];
end
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end) =
s_input.SIMULATION_TIME;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end) = 0;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(end) = 0;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_X(end) = 0;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_Y(end) = 0;

s_mobility.VS_NODE(nodeIndex_tmp) =
struct('V_TIME',s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME,...

'V_POSITION_X',s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X,...

'V_POSITION_Y',s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y,...

'V_SPEED_X',s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_X,...

'V_SPEED_Y',s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_Y);

end

clear s_mobility_tmp;
clear nodeIndex_tmp;
end

function
Out_setRestrictedWalk_random_waypoint(previousX,previousY,previousDuration,
previousTime,s_input)

global s_mobility_tmp;
global nodeIndex_tmp;

x_tmp = previousX;
y_tmp = previousY;
time_tmp = previousTime + previousDuration;
duration_tmp =
Out_adjustDuration_random_waypoint(time_tmp,unifrnd(s_input.V_WALK_INTERVAL
(1),s_input.V_WALK_INTERVAL(2)),s_input);
direction_tmp =
unifrnd(s_input.V_DIRECTION_INTERVAL(1),s_input.V_DIRECTION_INTERVAL(2));
speed =
unifrnd(s_input.V_SPEED_INTERVAL(1),s_input.V_SPEED_INTERVAL(2));
distance_tmp = speed*duration_tmp;
if (distance_tmp == 0)
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end+1,1) = time_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(end+1,1) =
x_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(end+1,1) =
y_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(end+1,1) =
direction_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(end+1,1) =
speed;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING(end+1,1) = true;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end+1,1) =
duration_tmp;
else
%The loop begins:
flag_mobility_finished = false;
while (~flag_mobility_finished)
x_dest = x_tmp + distance_tmp*cosd(direction_tmp);
y_dest = y_tmp + distance_tmp*sind(direction_tmp);
flag_mobility_was_outside = false;
if (x_dest > s_input.V_POSITION_X_INTERVAL(2))
flag_mobility_was_outside = true;
new_direction = 180 - direction_tmp;
x_dest = s_input.V_POSITION_X_INTERVAL(2);
y_dest = y_tmp + diff([x_tmp x_dest])*tand(direction_tmp);
end
if (x_dest < s_input.V_POSITION_X_INTERVAL(1))
flag_mobility_was_outside = true;
new_direction = 180 - direction_tmp;
x_dest = s_input.V_POSITION_X_INTERVAL(1);
y_dest = y_tmp + diff([x_tmp x_dest])*tand(direction_tmp);
end
if (y_dest > s_input.V_POSITION_Y_INTERVAL(2))
flag_mobility_was_outside = true;
new_direction = -direction_tmp;
y_dest = s_input.V_POSITION_Y_INTERVAL(2);
x_dest = x_tmp + diff([y_tmp y_dest])/tand(direction_tmp);
end
if (y_dest < s_input.V_POSITION_Y_INTERVAL(1))
flag_mobility_was_outside = true;
new_direction = -direction_tmp;
y_dest = s_input.V_POSITION_Y_INTERVAL(1);
x_dest = x_tmp + diff([y_tmp y_dest])/tand(direction_tmp);
end
current_distance = abs(diff([x_tmp x_dest]) + 1i*diff([y_tmp
y_dest]));
current_duration =
Out_adjustDuration_random_waypoint(time_tmp,current_distance/speed,s_input)
;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_TIME(end+1,1) =
time_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_X(end+1,1) =
x_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_POSITION_Y(end+1,1) =
y_tmp;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DIRECTION(end+1,1) =
direction_tmp;

s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_SPEED_MAGNITUDE(end+1,1) = speed;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_IS_MOVING(end+1,1) =
true;
s_mobility_tmp.VS_NODE(nodeIndex_tmp).V_DURATION(end+1,1) =
current_duration;
if(flag_mobility_was_outside)
time_tmp = time_tmp + current_duration;
duration_tmp = duration_tmp - current_duration;
distance_tmp = distance_tmp - current_distance;
x_tmp = x_dest;
y_tmp = y_dest;
direction_tmp = new_direction;
else
flag_mobility_finished = true;
end
end
%the loop ended
end
end

function duration =
Out_adjustDuration_random_waypoint(time,duration,s_input)

if ((time+duration) >= s_input.SIMULATION_TIME)
duration = s_input.SIMULATION_TIME - time;
end
end


Fungsi animasi.m
function animasi(s_mobility,s_input,time_step)

v_t = 0:time_step:s_input.SIMULATION_TIME; %setting time

for nodeIndex = 1:s_mobility.NB_NODES
%Get position use linear interpolation
vs_node(nodeIndex).v_x =
interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).
V_POSITION_X,v_t);
vs_node(nodeIndex).v_y =
interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).
V_POSITION_Y,v_t);
end

figure;
hold on;
for nodeIndex = 1:s_mobility.NB_NODES
vh_node_pos(nodeIndex) =
plot(vs_node(nodeIndex).v_x(1),vs_node(nodeIndex).v_y(1),'*','color',[0.3
0.3 1]);
end
title(cat(2,'Simulation time (sec):
',num2str(s_mobility.SIMULATION_TIME)));
xlabel('X (meters)');
ylabel('Y (meters)');
title('Radom Waypoint mobility');
ht = text(min(vs_node(1).v_x),max(vs_node(1).v_y),cat(2,'Time (sec) =
0'));
axis([min(vs_node(1).v_x) max(vs_node(1).v_x) min(vs_node(1).v_y)
max(vs_node(1).v_y)]);
hold off;
for timeIndex = 1:length(v_t);
t = v_t(timeIndex);
set(ht,'String',cat(2,'Time (sec) = ',num2str(t,4)));
for nodeIndex = 1:s_mobility.NB_NODES

set(vh_node_pos(nodeIndex),'XData',vs_node(nodeIndex).v_x(timeIndex),'YData
',vs_node(nodeIndex).v_y(timeIndex));
end
drawnow;
end
end

Hasil Simulasi :










Jawaban pertanyaan :
Beberapa pemodelan gerakan di atas diambil karena pengguna dapat berada dimana saja dan dengan
gerakan yang tidak dapat diprediksi sehingga dilakukan pemodelan. Disamping itu, pemodelan pada
keadaan tertentu dimana terdapat banyak interferensi seperti di dalam kendaraan (di jalan
raya,pegunungan, hutan, lapangan), di dalam bangunan, di kerumunan orang atau di tempat yang
padat penduduknya dengan distribusi dan penyebaran yang beragam sehingga dilakukan pemodelan.

Das könnte Ihnen auch gefallen