MATLAB QUESTION: Write a script that produces both figures below. you can use th
ID: 3600244 • Letter: M
Question
MATLAB QUESTION:
Write a script that produces both figures below.
you can use the given script below to help.
Use 10000 for n. Use 0.4 as the standard deviation for the normal distribution.
GIVEN SCRIPT:
% Script File: Eg6_1
% Pi computation with uniform and normal distributions
% Initializations
n = 10000; rand('seed',0); randn('seed',0);
% Throw Darts Uniformly...
x = -1+2*rand(n,1);
y = -1+2*rand(n,1);
Hits = 0;
for k=1:n
% Check the k-th dart throw...
if x(k)^2 + y(k)^2 <= 1
Hits = Hits + 1;
end
end
piEstU = 4*(Hits/n);
% Throw Darts with Aiming...
sigma = .4;
x = sigma*randn(n,1);
y = sigma*randn(n,1);
Hits = 0;
nSquare = 0;
for k=1:n
% Check the k-th dart throw...
if abs(x(k))<=1 && abs(y(k))<=1
nSquare = nSquare + 1;
if x(k)^2 + y(k)^2 <= 1
Hits = Hits + 1;
end
end
end
piEstN = 4*(Hits/nSquare);
% Display the estimates...
clc
fprintf('n: %1d ',n)
fprintf('Pi Estimate via Uniform Distribution: %7.5f ',piEstU)
fprintf('Pi Estimate via N(0,%5.2f) : %7.5f ',sigma,piEstN)
FIGURES
Figure 6.3. Estimating via Uniformly Thrown Darts.Explanation / Answer
Option Infer Off
Public Class MainForm
' declare class-level variable
Private points As Integer = 10
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub goalTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles goalTextBox.KeyPress
' accept only numbers and the Backspace Key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub rollButton_Click(sender As Object, e As EventArgs) Handles rollButton.Click
' simulates the Lucky Number Game
Dim randGen As New Random
Dim random1 As Integer
Dim random2 As Integer
'remove images
firstDiePictureBox.Image = Nothing
secondDiePictureBox.Image = Nothing
'disable Roll 'Em button
rollButton.Enabled = False
'refresh form and then delay execution
Me.Refresh()
System.Threading.Thread.Sleep(1000)
'generate two random integers from 1 through 6
random1 = randGen.Next(1, 7)
random2 = randGen.Next(1, 7)
'display appropriate image in firstDiePictureBox
Select Case random1
Case 1
firstDiePictureBox.Image = dot1PictureBox.Image
Case 2
firstDiePictureBox.Image = dot2PictureBox.Image
Case 3
firstDiePictureBox.Image = dot3PictureBox.Image
Case 4
firstDiePictureBox.Image = dot4PictureBox.Image
Case 5
firstDiePictureBox.Image = dot5PictureBox.Image
Case Else
firstDiePictureBox.Image = dot6PictureBox.Image
End Select
'display appropriate image in secondDiePictureBox
Select Case random2
Case 1
secondDiePictureBox.Image = dot1PictureBox.Image
Case 2
secondDiePictureBox.Image = dot2PictureBox.Image
Case 3
secondDiePictureBox.Image = dot3PictureBox.Image
Case 4
secondDiePictureBox.Image = dot4PictureBox.Image
Case 5
secondDiePictureBox.Image = dot5PictureBox.Image
Case Else
secondDiePictureBox.Image = dot6PictureBox.Image
End Select
'check sum of random numbers
If random1 + random2 = 7 Then
Dim count As Integer = 1
Do While count <= 10
numberLabel.Visible = Not numberLabel.Visible
Me.Refresh()
System.Threading.Thread.Sleep(200)
count += 1
Loop
points += 2
Else
points -= 1
If points = 0 Then
MessageBox.Show("Sorry, you lost all of your points!" &
"Click the Start Over button to try again.",
"Lucky Number Game", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End If
' display points
pointsLabel.Text = points.ToString
' enable Roll ' Em button
rollButton.Enabled = True
End Sub
Private Sub startOverButton_Click(sender As Object, e As EventArgs) Handles startOverButton.Click
' start a new game
points = 10
pointsLabel.Text = points.ToString
firstDiePictureBox.Image = Nothing
secondDiePictureBox.Image = Nothing
End Sub
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.