master
iotcat 5 years ago
commit cd19bdc500
  1. 16
      func/drawBackGround.m
  2. 21
      func/getDisProb.m
  3. 9
      func/isInRect.m
  4. 9
      func/normrnd_circle.m
  5. 14
      func/unifrnd_circle.m
  6. 27
      t2.m
  7. 30
      t4.m
  8. 30
      t5.m
  9. 27
      t7_2.m
  10. 30
      t7_4.m
  11. 30
      t7_5.m

@ -0,0 +1,16 @@
function drawBackGround(Radius, Width, Length)
% prepare figure
hold on
% - draw circle
d_ang = 0 : pi/100 : 2*pi;
plot(Radius*cos(d_ang), Radius*sin(d_ang), '-');
% - draw rect
line([
-Length/2, -Length/2, Length/2, Length/2, -Length/2
], [
-Width/2, Width/2, Width/2, -Width/2, -Width/2
]);
axis equal
end

@ -0,0 +1,21 @@
% compute distributation
function [score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, isPlot, method)
score = 0;
totalTimes = 0;
for m = 1 : R
for n = 1 : N
[x, y] = method(Radius);
totalTimes = totalTimes + 1;
if isInRect(Length/2, Width/2, x, y)
score = score + 1;
if isPlot
d_rx = plot(x, y, 'rx');
end
else
if isPlot
d_bo = plot(x, y, 'bo');
end
end
end
prs = score / totalTimes;
end

@ -0,0 +1,9 @@
% judge if a position in a rectangle
function res = isInRect(x, y, x_, y_)
if abs(x_) < x && abs(y_) < y
res = true;
else
res = false;
end
end

@ -0,0 +1,9 @@
% get random position in circle
function [res_x, res_y] = normrnd_circle(r)
ang = 2*pi*rand(1);
t_r = normrnd(0, r);
res_x = t_r*cos(ang);
res_y = t_r*sin(ang);
end

@ -0,0 +1,14 @@
% get random position in circle
function [res_x, res_y] = unifrnd_circle(r)
while true
res_x = unifrnd(-r, r, 1, 1);
res_y = unifrnd(-r, r, 1, 1);
ans_r = sqrt(res_x^2 + res_y^2);
if ans_r < r
break
end
end
end

27
t2.m

@ -0,0 +1,27 @@
% init
clear
addpath(genpath('./func/'));
% declear const
Radius = sqrt(5);
Width = 2;
Length = 4;
% params input
N = input("Shots Times: ");
R = input("Repeating Times: ");
% draw background
drawBackGround(Radius, Width, Length);
% calculate prs
[score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, true, @unifrnd_circle);
% add legend
legend([d_rx, d_bo], {'scored shot', 'missed shot'});
t_s = sprintf('Scatter plot for N=%d and R=%d. prs=%d%%', N, R, prs*100);
title(t_s);
display(prs);

30
t4.m

@ -0,0 +1,30 @@
% init
clear
addpath(genpath('./func/'));
% declear const
Radius = sqrt(5);
Width = 2;
Length = 4;
% declear params
N = [100, 1000, 10000, 100000];
R = 5;
% declear var
array_prs = [];
% calculate prs
for i = 1 : numel(N)
[score, totalTimes, prs] = getDisProb(R, N(i), Radius, Length, Width, false, @unifrnd_circle);
array_prs = [array_prs, prs];
end
disp(array_prs);
plot(N, array_prs);
xlabel('N random shots');
ylabel('Probability');
t_s = sprintf('Scatter plot for N with R=%d.', R);
title(t_s);

30
t5.m

@ -0,0 +1,30 @@
% init
clear
addpath(genpath('./func/'));
% declear const
Radius = sqrt(5);
Width = 2;
Length = 4;
% declear params
N = 1000;
R = [5, 10, 15, 20];
% declear var
array_prs = [];
% calculate prs
for i = 1 : numel(R)
[score, totalTimes, prs] = getDisProb(R(i), N, Radius, Length, Width, false, @unifrnd_circle);
array_prs = [array_prs, prs];
end
disp(array_prs);
plot(R, array_prs);
xlabel('R random shots');
ylabel('Probability');
t_s = sprintf('Scatter plot for R with N=%d.', N);
title(t_s);

@ -0,0 +1,27 @@
% init
clear
addpath(genpath('./func/'));
% declear const
Radius = sqrt(5);
Width = 2;
Length = 4;
% params input
N = input("Shots Times: ");
R = input("Repeating Times: ");
% draw background
drawBackGround(Radius, Width, Length);
% calculate prs
[score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, true, @normrnd_circle);
% add legend
legend([d_rx, d_bo], {'scored shot', 'missed shot'});
t_s = sprintf('Scatter plot for N=%d and R=%d. prs=%d%%', N, R, prs*100);
title(t_s);
display(prs);

@ -0,0 +1,30 @@
% init
clear
addpath(genpath('./func/'));
% declear const
Radius = sqrt(5);
Width = 2;
Length = 4;
% declear params
N = [100, 1000, 10000, 100000];
R = 5;
% declear var
array_prs = [];
% calculate prs
for i = 1 : numel(N)
[score, totalTimes, prs] = getDisProb(R, N(i), Radius, Length, Width, false, @normrnd_circle);
array_prs = [array_prs, prs];
end
disp(array_prs);
plot(N, array_prs);
xlabel('N random shots');
ylabel('Probability');
t_s = sprintf('Scatter plot for N with R=%d.', R);
title(t_s);

@ -0,0 +1,30 @@
% init
clear
addpath(genpath('./func/'));
% declear const
Radius = sqrt(5);
Width = 2;
Length = 4;
% declear params
N = 1000;
R = [5, 10, 15, 20];
% declear var
array_prs = [];
% calculate prs
for i = 1 : numel(R)
[score, totalTimes, prs] = getDisProb(R(i), N, Radius, Length, Width, false, @normrnd_circle);
array_prs = [array_prs, prs];
end
disp(array_prs);
plot(R, array_prs);
xlabel('R random shots');
ylabel('Probability');
t_s = sprintf('Scatter plot for R with N=%d.', N);
title(t_s);
Loading…
Cancel
Save