EX1010 - Introduction to MATLAB Assignment - City University of London, UK
MATLAB Graphics Game Task -
You are required to submit two M-files that respond to the brief below. These files should be ready to be run in MATLAB to perform the tasks proposed in the brief.
You should not submit the numerical answers or the plots. Do not include any additional material apart from the two M-files because it will not be considered. However, you should include succinct comments within the M-files to explain the different parts and built-in functions that you are using, always respecting the rules for commenting in MATLAB. Only Moodle submissions will be considered. This work should be entirely your own. Any potential plagiarism (copying) will be investigated thoroughly. If plagiarism is found it will be treated a serious academic misconduct.
Problem - Consider a floor made of two parallel horizontal strips that are connected at y = 0. A straight needle of length L = 3 cm defined as a segment between the nodes A and B is dropped onto the floor and it stops at a random position given by the coordinates of its point A (xA, yA) and by its angle with respect to the horizontal line (θ), as shown in Figure 1.
You are asked to create a MATLAB M-file named "problem_XXX.m" (exactly like this, without capital letters or spaces, and substituting XXX by your full student number) that
1. Plots the needle at a random position on the floor considering that the point A of the needle can only be located in the square domain represented in Fig 1. This domain is a square of 20- cm centered at the origin of coordinates (x, y), as shown. The plot should be between -10 and 10 cm in the abscissas and in the ordinates. Use a solid black line to represent the needle and do not try to colour the background. Include a black horizontal line at y = 0. To represent the joint.
2. Uses a FOR loop to create a new plot with 50 needles distributed randomly on the floor. Use the same style and limits of the plot as in the previous section.
3. Counts how many of the 50 needles intersected (crossed) the joint of the floor in the previous plot and displays in the Command Window the proportion of the intersecting needles with respect to the total number of needles on the floor.
4. Creates a third plot that is the same as the second one but that represents the needles that intersect the joint in red colour and with a linewidth of 2 units, whilst the rest are plotted in black colour and with a linewidth of 1 unit. Use the same limits for the plot as in the previous sections.
Note: When running this file the three plots generated should appear in the screen. Hint: to avoid "losing" the first plot when the next one is generated you can use the command figure(). You are referred to the MATLAB Help for more information about how to use this command.
Note for curious minds: This is the Buffon's needle problem proposed by Georges-Louis Leclerc, Comte de Buffon, in the 18th century and it is the earliest problem in geometric probability that was solved. You can use your code to calculate the probability of a needle to lie across the line between the two strips of the floor (this is not part of the assessment). The solution of this probability is an approximation of the number π.