Introduction to MATLAB Assignment -
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.
Problem 1 -
The integral of cos(x2) is called the Fresnel C integral. It is interesting to note that it does not have a closed-form solution. When it is evaluated from 0 to 1 it can be estimated using the Maclaurin infinite series as:
I = 0∫1cos(x2)dx = n=0∑∞(-1)n/((2n)!(4n+1))
where (2n)! is the factorial of 2n.
You are asked to create a MATLAB M-file named "problem1_XXX.m" (exactly like this, without capital letters or spaces, and substituting XXX by your full student number) that
1. Calculates the series of I using as many terms as necessary to converge to the solution that will be considered as exact:
Iexact ∼ 0.904524237900272
Hint: the factorial of ?? (or ??!) in MATLAB is obtained by typing factorial(X). Type format long in the Command Window to display more than 4 decimals.
2. Creates a plot of cos(x2) in which the value of x appears in the abscissa and the value of the function appears in the ordinate. The plot should range from -10 to 10, with data points spaced 0.01 units. The curve should be black and solid.
3. Obtains the numerical integral of cos(x2) from 0 to 1 by adding the area of 30 rectangles equally spaced over the range from 0 to 1. Evaluate the function cos(x2) at the middle of each rectangle to calculate its height.
4. Obtains the error (in %) of the numerical integration with respect to the value of Iexact obtained from the Maclaurin infinite series. The value of this error should be displayed in the Command Window when running the script.
Problem 2 -
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 "problem2_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 π.
For your reference, the third figure in Problem 2 should look similar to Figure 2 (attached). Note that the position of the needles is random and because of this it is highly unlikely that you will obtain exactly the same plot as in Figure 2, it is just a reference.
IMPORTANT NOTE: in this problem all the plots should have the same aspect ratio between the horizontal and the vertical axes (as the one shown in Figure 2). This can be achieved by using the command: axis square.
Attachment:- Assignment File.rar