% MS-E2170 Simulation % Exercise 1.1: Estimate area of polygon using Monte Carlo simulation % a) Verify the sampling experiment by constructing a plot of the result. % % Fill the '' with your own input. % % Created: 2018-02-19 Heikki Puustinen %% Initialization % Sample size: N = ; % Define the x- and y-coordinate values of the polygon. px = []; py = []; % Define the surrounding rectangle. This is the space where the random % points are generated. We use unit rectangle as it's area is easy to % calculate. rx = [0, 0, 1, 1]; ry = [0, 1, 1, 0]; %% Simulation % Generate 2N random numbers from the uniform distribution U(0,1). % N for x-coordinate values and N for y-coordinate values. sx = ; sy = ; % Check which points (sx,sy) are inside polygon (px,py). % Hint: 'inpolygon' (1: inside, 0: outside). in = ; %% Plotting % Let's create a window so we can plot the results. "fig" is just a name % for the handle to the window so we can access it later. fig = figure('Name','MS-E2170, Exercise 1 a), Monte Carlo Simulation'); % Create an axes (optional but good practice). ax = axes('Parent',fig); % Set title title(ax,'Verification plot') % Set hold on so that consecutive plots do not clear the axes. hold(ax,'on'); % Plot the polygon. Note that the plot-command does not automatically % connect the last and the first coordinates so we add the first % coordinate to the end. 'r' is for 'red'. plot(ax,[px, px(1)],[py, py(1)],'Color','r') % Plot the surrounding rectangle (blue). plot(ax,[rx, rx(1)],[ry, ry(1)],'LineWidth',2,'Color','b') % Plot points that are inside the polygon (red dots). plot(ax,sx(in),sy(in),'r.') % Plot points that are outside the polygon (blue dots). plot(ax,sx(~in),sy(~in),'b.')