% MS-E2170 Simulation % Exercise 1.1: Estimate area of a polygon using Monte Carlo simulation % d) How does the sample size of your experiment affect your estimate % and the confidence interval? Repeat the experiment using different % sample sizes. Create a plot of the length of the confidence interval % against sample size. % % Fill the '' with your own input. % % Created: 2018-02-19 Heikki Puustinen %% Initialization % Sample sizes: (construct a vector holding sample sizes for experiments) N = ; % Define the x- and y-coordinate values of the polygon. px = [0.2, 0.4, 0.8, 0.5]; py = [0, 0.25, 0, 1]; % Define the surrounding rectangle. rx = [0, 0, 1, 1]; ry = [0, 1, 1, 0]; %% Simulation % We now have to call the area estimation function multiple times (once for % each sample size). % It is good practice to initialize the variables in which we store the % results. V = zeros(length(N),1); Ci = zeros(length(N),2); % Loop trhough the sample size vector. for ii = 1:length(N) % Call the area estimation function with the appropriate sample size % and store the output in appropriate indexes in the result variables. [] = area_mc(); end %% Plotting % Create a window. f1 = figure('Name','MS-E2170, Exercise 1 d), Monte Carlo Simulation'); % Create axes ax1 = axes('Parent',f1); % Hold axes. hold(ax1,'on') % Make title and labels title(ax1,'Estimate of the area of polygon and CIs') xlabel(ax1,'Sample size') ylabel(ax1,'Area') % Plot estimated areas. % x-axis: sample size % y-axis: estimated area plot(ax1,N,V,'r') % Plot confidence intervals. % Lower plot(ax1,N,Ci(:,1),'b') % Upper plot(ax1,N,Ci(:,2),'b') % Calculate lengths of the confidence intervals. Ci_lengths = Ci(:,2) - Ci(:,1); % Create a window. f2 = figure('Name','MS-E2170, Exercise 1 d), Monte Carlo Simulation'); % Move second window a bit to the right. set(f2,'Position',get(f2,'Position') + [50 -50 0 0]) % Create axes ax2 = axes('Parent',f2); % Hold axes. hold(ax2,'on') % Make title and labels title(ax2,'Lengths of CIs') xlabel(ax2,'Sample size') ylabel(ax2,'Length of CI') % Plot the lengths of the interval against sample size. plot(ax2,N,Ci_lengths,'k','LineWidth',2)