MATLAB question 5 Imagine you have a simple dart board 20 x
MATLAB question
5. Imagine you have a simple dart board (20 x 20 units) with 3 different point values, as shown in Figure 3. You also have two dart guns and are curious about their accuracy. Consequently, you want to find the probability of landing a dart in each part of the board for each gun. To accomplish this, you have the dart guns shoot thousands of darts on the board at random with each dart gun and count the number of darts that land in the specified areas. If a dart lands on a border, assume the value of the lower point (e.g. if the dart lands on the 5-point/1-point border, it only counts as 1 point). f a dart lands outside the biggest circle (or anywhere else) it receives a score of 0. 27 points for three functions and 9 points for 6 figuresSolution
dart_score.m
function [ total, tally, shots ] = dart_score( n, f_dart )
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
shots = f_dart(n);
dist = sqrt(sum(shots.^2,2));
tally = zeros(1,4);
% index 1 is 10 pt area
% index 2 is 5 point area
% index 3 is 1 point area
% index 4 is 0 point area
for i = 1:length(dist)
if dist(i) < 1
tally(1) = tally(1) + 1;
elseif dist(i) < 5
tally(2) = tally(2) + 1;
elseif dist(i) < 10
tally(3) = tally(3) + 1;
else
tally(4) = tally(4) + 1;
end
end
% tally(1) = numel(find(dist<1));
% tally(2) = numel(find(dist<5 && dist>=1));
% the && here threw an error for some reason
% tally(3) = numel(find(dist<10&&dist>=5));
% tally(4) = numel(find(dist>=10));
total = sum(tally .* [10 5 1 0]);
%plotting the circles
clf
x = linspace(-10,10);
plot(x,sqrt(100-x.^2),\'--k\')
hold on
plot(x,-sqrt(100-x.^2),\'--k\')
hold on
x = linspace(-5,5,50);
plot(x,sqrt(25-x.^2),\':k\')
hold on
plot(x,-sqrt(25-x.^2),\':k\')
hold on
x = linspace(-1,1,10);
plot(x,sqrt(1-x.^2),\':k\')
hold on
plot(x,-sqrt(1-x.^2),\':k\')
hold on
scatter(shots(:,1),shots(:,2))
end
dart_gun01.m
function [ coords ] = dart_gun01( n )
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
coords = zeros(n,2);
coords(:,1) = randn(n,1)*10; %x-coords
coords(:,2) = randn(n,1)*10; %y-coords
end
dart_gun02.m
function [ coords ] = dart_gun02( n )
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
coords = zeros(n,2);
coords(:,1) = rand(n,1)*20-10; %x-coords
coords(:,2) = rand(n,1)*20-10; %y-coords
end

