Complete E7_HW7.pdf on Matlab. use the files weights.txt and HW7_Template.m to complete the pdf assignment. Then check with auto graders HW7_ag.p for 100%.
E7 Fall 2019, UC Berkeley Homework Assignment 7 Plotting & Statistics The purpose of this lab is to introduce you to plotting and statistics functions in MATLAB and their utilization. Note: Use the templates provided in the assignment download to complete this assignment. The template has a very specific format to enable auto-grading, and if the format is altered your assignment may not be properly graded (this will be apparent to you). The due time is 11:59am (noon) October 30, 2019. NO LATE HOMEWORK WILL BE ACCEPTED. Please upload the following files through the bCourses website: • HW7 Plots.m • HW7 Plots.pdf (created using the publish command) • All function files you are asked to create or are necessary to run your code. ‘RollDice.m’, ‘MontyHall.m’, ‘Rastringin.m’ are required. • All generated plot files. ‘StackedWaves.png’, ‘PeriodicWaves.png’, ‘RastringinSurf.png’, ‘RastringinCont.png’, ‘Monty.png’, ‘Weights.png’, ‘Convergence.png’, and ‘CLT.png’. • Upload ’Vortices.avi’ movie file. Directions to upload files can be found here. 1. This question is to introduce you to some of the plotting options in MATLAB by plotting a sine, cosine and tangent curves on a angular space of 0 to 2π radians. Use linspace to divide the angular space into 1-by-1000 array and assign it to variable ‘theta’. Evaluate sine, cosine and tangent functions using this theta array and assign the arrays to ‘fsin’, ‘fcos’ and ‘ftan’ variables respectively. Use subplots to plot all three variables on three different subplots which are in a 3-by-1 stack. Subplots should have titles: ‘Sine Wave’, ‘Cosine Wave’, and ‘Tangent Wave’. On ‘Tangent Wave’ subplot, plot straight red dashed lines (‘r–’) at ‘π/2’ and ‘3π/2’ to denote asymptotes. Hint: Lookup ‘xline’ and ‘yline’ commands. The label on the x-axis should be ‘Angle’ for all the subplots. Save the plot as .png file named ‘StackedWaves.png’. Note: You can save a plot figure as .png from within MATLAB script as well using ‘print’ command. Now on a single plot, plot all 3 arrays ‘fsin’, ‘fcos’ and ‘ftan’ (without the asymptotes) versus ‘theta’ array. All three lines should have different line-widths, line colors, and 1 https://guides.instructure.com/m/4212/l/54353-how-do-i-upload-a-file-to-my-assignment-submission E7 Fall 2019, UC Berkeley line types. Add legend to the figure with the text of the legend same as the titles of the subplots described above. The title of the plot should be ‘Periodic Waves’. The x-axis range should be from −π/2 to 5π/2 and y-axis should be from −2 to 2. x and y-axis labels should be ‘Angle’ and ‘Function’ respectively. The grid should be on for this plot. Save the plot as .png file named ‘PeriodicWaves.png’. 2. In this question you will learn about vortex dynamics and making a movie in MATLAB. It presents a simple model of the motion of a group of point vortices. Consider 4 point vortices of equal strength Γ = 100. Place the vortices on a circle of radius 1 at equal angular distance between the vortices with the first one at (1,0), i.e. on a Cartesian plane, the first vortex is placed at (1,0), second vortex is at (0,1), third at (-1,0) and fourth at (0,-1). This is the initial position of the vortices at time t=0. Point vortices, in Cartesian coordinates, interact with one another by inducing velocity on the other vortices. The induced velocity on a ‘ith’ vortex is: Ui = Σ n j=1,j 6=i Γj 2π[(xi − xj)2 + (yi − yj)2] [−(yi − yj)] Vi = Σ n j=1,j 6=i Γj 2π[(xi − xj)2 + (yi − yj)2] [(xi − xj)] Here ‘n’ is the total number of vortices (4 in this case). This is the velocity induced on an ‘ith’ vortex at time ‘t’ using the positions of vortices at time ‘t’. Use following relations to get the positions of the vortices at time ‘t+ ∆t’: xi(t+ ∆t) = xi(t) + Ui(t)∆t. yi(t+ ∆t) = yi(t) + Vi(t)∆t. Here we use ∆t = 0.001. Repeat this process ‘NSteps’ times. Use NSteps=250. The final coordinates after ‘NSteps’ steps should be in a 4-by-2 array variable called ‘Coords’ where the first column represents x-coordinate of the 4 vortices and the second column the y-coordinates at time t = ∆t ·NSteps. Plotting: Plot all 4 vortices at every time step using their x and y coordinates. Denote a vortex with ‘*’. Keep both of the axis limits from -1.5 to 1.5. Use ‘clf’ to clear the graphic window at every iteration and ‘drawnow’ to plot within each iteration of the loop. Make a structure array ‘F’ to store all graphic frames and use ‘getframes’ to capture the graphics and add them to the structure array ‘F’. You can use ‘movie’ command to play the frames and play them at 10 fps. Write the movie as ‘Vortices.avi’ and save it with the default fps. You can write video files using ‘writeVideo’ MATLAB function. 3. Consider the following function: 2 E7 Fall 2019, UC Berkeley f(x, y) = 2A+ x2 − Acos(2πx) + y2 − Acos(2πy) where A = 10 and −5.12 ≤ x, y ≤ 5.12 To plot a surface of a function, we need a mesh grid. You can read about mesh grids here. You can use the ‘meshgrid’ command to generate ‘X’ and ‘Y’ grids. Now create a function ‘[Z]=Rastringin(X,Y)’ that takes in a grids of X and Y and outputs matrix of the same size as X and Y with Rastringin function evaluated at every grid point. Use ∆x = ∆y = 0.1 for your initial x and y vectors for generating the grid. Store your x-grid as ‘X’, y-grid as ‘Y’ and evaluated function at these grid points as ‘Z’. Plot a surface plot using ‘surf’ command. The x,y and z-axis labels of your plot should be ’X’, ’Y’ and ’f’ respectively. Add colorbar to the plot. Title should be ‘Rastringin Surface’ and save it as ‘RastringinSurf.png’. Now plot a contour plot using ‘contour’ function. Use the same axis labels and title your plot ‘Rastringin Contour’ and save the plot as ‘RastringinCont.png’. Make sure colorbar is included in the second plot as well. For interested readers, the function plotted in this question is the Rastringin function in two dimensions, a non-convex function used to test optimization algorithms which can be extrapolated to higher dimensions and is a good test for high dimension optimization algorithms. Further info. 4. In recent years we polled the data from E7 students after midterms since a lot of them suddenly and inexplicably seemed to lose weight afterwards. The poll was gender neutral and height blind, so we got a huge variation in the numbers. The data is given in the file ‘Weights.txt’. You can load the data using the ‘importdata(’Weights.txt’,’ ’)’ command and store the weights as an array in variable ‘W’. After loading data, calcu- late the following variables: (a) Mean: the mean of the data (b) Mode: the mode of the data. (The mode is the the number which appears most often in a set of numbers. More info here.) (c) Median: the median of the data (d) SD: standard deviation of the data (Use the builtin MATLAB function ‘std’) (e) Var: variance of the data The weight is in kilograms and integer value. Generate a histogram that displays the distribution of the results. Each bin in the histogram should represent a possible value of weights. The bins should span the 3 https://www.mathworks.com/help/matlab/ref/meshgrid.html https://en.wikipedia.org/wiki/Test_functions_for_optimization https://www.mathworks.com/help/matlab/ref/mode.html E7 Fall 2019, UC Berkeley values minimum possible to maximum possible weight. Plot title should be ‘Weights’. x and y labels should be ‘Weight’ and ‘No. Of Students’. Now plot vertical dashed lines of different colors on the histogram that represent the Mean, Median and Mode of the data on the plot. Display legend with the variable names as the legend entries. E.g. the histogram bar should have a legend entry ‘No. of Students’ and the dashed line denoting mean should show in the legend as ‘Mean’. Save the plot as ‘Weights.png’. 5. In this problem, we will demonstrate the validity of the ‘Central Limit Theorem’ (CLT) by a virtual test that involves rolling of dice. To this end, you will create a function, called ‘SumDice=RollDice(NumRolls)’, that simulates the rolling of 10 6-sided unbiased dies ‘NumRolls’ times. In the first line of your function, add this command ‘rng(1)’. This command ensures that the random generator always produces the same sequence of random numbers and this is usually used to debug codes. Here we are using it so that the autograder can grade your homework. Each die is numbered from 1 to 6. We will perform a series of experiments. In each experiment we roll 10 dies a specific number of times denoted by NumRolls and the sum of the 10 dies is noted for each throw. For example, if NumRolls is 2 the sequence of events is as follows: 10 dies are thrown once, their sum is computed. The dice are then collected and thrown a second time and their sum is again computed. RollDice returns a 2 by 1 array containing the sums from the two throws. The output of the function will be a column vector ‘SumDice’ of length ‘NumRolls’ that contains the total sum of the die values in each throw. This constitutes a single experiment. Hint: The dies are unbiased so your function can simulate that using a uniform dis- tribution. You can use ‘randi’ Make an array ‘Rolls=[10;25;50;100;200;600;1000;5000;10000]’ where each element rep- resent a NumRolls for a single experiment. Calculate the mean value of ‘SumDice’ of each experiment and store it as an element of column vector ‘DiceMean’ i.e. the size of ‘DiceMean’ will be equal to ‘Rolls’. For example, the mean of ‘SumDice’, for the first experiment Rolls(1), will be stored as DiceMean(1). Plot ‘DiceMean’ against ‘Rolls’. Plot title should be ‘Convergence CLT’. x and y labels should be ‘Number of Experiments’ and ‘Mean’ respectively. Save the plot as ‘Convergence.png’. The convergence of the mean shows the validity of central limit theorem. We will test this further. Generate a histogram that displays the distribution of the results of the last scenario. Calculate the mean and standard deviation for the last experiment (NumRolls=10000). Each bin in the histogram should represent a possible value for the sum of the dice. The bins should span the values minimum possible sum to maximum possible sum. Make a row vector ‘diesum’ which lists all possible values of the sum of dice. In this case, it will span from 10-60. Now calculate a probability distribution (pdf) of a 4 .https://en.wikipedia.org/wiki/Central_limit_theorem E7 Fall 2019, UC Berkeley normal distribution curve using the calculated mean and standard deviation (you can use ‘normpdf’ function) and multiply it with number of rolls