Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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...

1 answer below »
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 a
ay and assign it to variable
‘theta’. Evaluate sine, cosine and tangent functions using this theta a
ay and assign
the a
ays 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 a
ays ‘fsin’, ‘fcos’ and ‘ftan’ (without the asymptotes)
versus ‘theta’ a
ay. 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 fo
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 othe
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
elations 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 = XXXXXXXXXXRepeat this process ‘NSteps’ times. Use NSteps=250.
The final coordinates after ‘NSteps’ steps should be in a 4-by-2 a
ay 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 a
ay ‘F’ to store all graphic frames and use ‘getframes’ to
capture the graphics and add them to the structure a
ay ‘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 you
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 colo
ar 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 colo
ar 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
e 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 gende
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 a
ay 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/matla
ef/meshgrid.html
https:
en.wikipedia.org/wiki/Test_functions_for_optimization
https:
www.mathworks.com/help/matla
ef/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)
y 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
eturns a 2 by 1 a
ay 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 a
ay ‘Rolls=[10;25;50;100;200;600;1000;5000;10000]’ where each element rep-
esent 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 XXXXXXXXXXNow 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
Answered Same Day Oct 30, 2021

Solution

Abr Writing answered on Nov 01 2021
154 Votes
RastringinCont.png
RollDice.m
function SumDice = RollDice(NumRolls)
rng(1);
SumDice = zeros(NumRolls, 1);
for i=1:NumRolls
temp = 0;
for j=1:10
temp = temp + randi([1 6], 1);
end
SumDice(i) = temp;
end
end
RastringinSurf.png
StackedWaves.png
Vortices.avi
CLT.png
hw7.p
v01.00v00.00��p�f·_±���ë��
Ï���"ph�ïTŸ�}µ�F,²ÈÉ �rvÙx�´Ç��äŒr�éG7�9ýË*ÂÆS.ê�Æ«öBü©
é��8q�ùX»�íöA�Ô0{�˜Ÿ-¦ÍSÆ\�OJ˜Æp��ThÒ(àÐÿh�‡�ÕøÉð|G9)?NÝ2²_�¶�Da¿
�ÈP3@J0¿–ÒÞ8Â¥A
�®%d�”�©"=,Ñ�ÔsSkÑ���Æ�12
]Š„RYáôÏéÉ…{fRJª��Ý3B�籕��ÁÎý9·pÁ:È,‡ê‚
Â,(êô A�+aEÕ¤“´¯/Üu.íš3Àžø4Ä�µ
aݬôx ôLåšo‡à¡Cƒã?ì�1|�Î�”7"6£�mü!Îit¶Að:”Ç�Ô�#w­Ž›$ÞTwn��]ñc–Ðr3ðaû-��„L¢E�,sï4�6K0ƒÏá‘�~�E¯î�Û©Õ`�17&õêˆÕ€�ÌT�ïj×tú ºÝ&WÇ!„}á�>N
û…Ûk•�PUâÀ4åŽM1�´;†ˆªßæ?�ʦ‘�d[λ�uÕå0šäØË�p¨ä‚d߇0!YÎÜ+ä�+pŸxOGˆ3ŽE&ðûé°‹y› ÏQMý"ZdÙž�“ÐS‰yYÅNn�?1É���²Œ4Þ�u_Iõ�¡••]…ʸ˜�0Q‹E”­F¿ÿE­ù­¨I]18ÎŒj�1€sª�ŒqÊö��� 1�]ÏÌ7Æ�œì¶€{2$¤¯�� êèЪÔ�“uõó¥äbØ��ù„C>–阦Ì%V�MV¦-ÜìùÀžÔØ�-QºÐ�Ô½Ëâ‡N@Ä—¡P[X¡�ºâêßñ¶øtIŽ�l'sm›zx��{��T}�W«o�ÎfÁµgi“�9ØHÃN娕©e¼IDg�?°Þ‹;v[à�1<¶ã½¥�¢ñ)!ž€    W=n%‚Æ��7
qy%1ÚªëH²�T„N2j½Ñ�-h¶¤[ì.<Æý™‡‡�úµ*�×óy��ÚÜó¢â�q?ILÙ=ÿ�êqˆ�#¸IAø��Èz�¬°•×��¼è3ïu��1™Ñÿ¯%T´Ê���Œ��6u·}ûÌ*Ç�ñÕ•g‘~¿­��É“sT0Î)G€‹G5O�¬¹l`B¥l"_F�¯Ïnî…�’Wy)
�uA\�"Ö�ã’�E“¶LëöVÙöõžeǧƒóÇÐé$ÜD=üÍÅ5m?@_v2ÀÌ
�¦ãt—AQûr�»�ØŒr}�j±ÍÖ@l�a�õ#Ë„iˆ��¤—q�#ë¬=R�UCø…±ÊPðžœð®ú�¦“�kãõ�Q!�¡�3‰O‡¼� 5R�b°XŠc���¯bac
K¹±Vý‡ú‚€³1m|_n‹�Öúl��K ‘ŽW„b!„�ú��$é>HŸ6�·œð¼a‡`IfhqNÛ‹‚
–°´ò�ú�V»�{=¶ì�i��S)¨÷�_…ä�ÀuñÞ•¿³ëj�È’–p
M�õ�“�‡�åQô�Ž—(@k²2¸%dðŸX�ùÎzL±Ó^‘}à§%Ú¦sÔQ< S �­ÚL:�¥ï�žð¦´Ì²7:�ûG9¼M���‘ÖB°êîÔ̬##SêÙ�5:�?ÌÌ�Û��^¯G³ÿì1»ð뚬'¡P¡Ï!`z9oDÆ®ïOžÒÊôñž4±§H9¸�à��3=�*Ú•#>’1E‘@��g�YEB˜þ.åq!Ž��    #!•�mË:­¸÷ÒÙ¥­Ÿ�‘›£5�òŒK�ŒªxÆ�g|�¬üÐ
Ï­ÔóßœÙíä¾õ�SQ`OE�ž�âþ«ñ�#ùï��øü�É-}l�Ml�ˆP�Æ2Š‘!õŠí@YØë–�SëY��íâ1YˆŽ��S÷b@�c¯e�÷Å2r{Áµbi0]wÐñ±Ó�î–xB¾!�>3“@ÿ)wAÚ�åÌ�
§-)]êŠ2�¢4��óÜž…IYA>Ìsý3šã3ú³Å8æ …'+“²'�QÈKåK)oŠä£Yè™�sN7"ßÂ�Ç꾬êŠ]~ãÌ,ï¡GkyF,�W—ÏBn�\s^}éï>æP¾¬ó™„&17þ|ÏG¸u��åªk�±ïvÄã<¡aöš�åGa¦�âikÅEo¾«bã‹¥5™s*êÁ#ècµL�ž~��|+þ�M¸’ËÎŽÒ½äј¶_Ï·ŒC©Ë[’¹òoH€ì�—šŸ��=š!ÁÛò5qYŒ�ÍjUš¥@��zØxÀû¾ÊÎæAѬ˜›À�-9 éf‡“��Ó    �-bï�Õo[M0y�÷�    %¸Bí$YX´ÕžòÆ�“    í@5�ÿ0?~_Á¨Ý¤Á�l�ŠÊ��1��qOŸ7ôûݲ6��„��äÀ©ÑWz’��´˜�æ–¿ahÔnaºL÷Ô$ÞË¥v›
? ýÌÉx�Ž€™ò­˜ß~ºuj‡˜)ÿ0€r~ç,Äb8�ÔÝ;a�äSjºný®LL…�š�ת�Õ™�]�Á7�D\Æy¸åmêü‰Õö`NrK>�    Ïl¯“Äc0Îæ.9 «@¼­è)�Õ�³5ähm"š[>”“˜±/ø¬™YdvX-‡$+‹á�rOŽg¸�¶¿SíºFºßò�ž±��7ƒ ØÎ{À}“*d/~—Ás�Ú�Üyw_¥�8‘@ýÈ"›ò�)AÙ.���Q,;´6ö2ÝÙý¹li�‰hbÇFQ�pQŽÇ4ï;y*{b€�êÁL!��–�J��Óa�V¹p�"£¤ž=¾�¹{‹#£à³íx@¹âÊ��8PzYñOë�¬à«ŽHú,dñÊ    IÜòM8S�Fˆwe“âkxÚåEàO4¼&ôuo„RQ‰(�éÅÚ‰UE˜û³”M]*Z›Í-S²õ²�‡Ç¸ž·œ�Ì'ná�Ö×I�s.©r®Ik‚N�W
�7ø'�P¿Ù§�+)@aÑW�Ð�6ýöâΛ•�ÿ5j½×)f�’7KÁ�[�ù�âÚ¡k=õX¡��Xl*?®Æ¸¢¥Ÿ‚Ÿo#^ioÌ��œ�A™Ý¾>›ùúÙô*S]1YKòK/‹›ñr:qIsÜ�
L
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here