School of Mathematics and Statistics MAST30028 Numerical Methods & Scientific Computing Week 4
Drag and drop the folder Week4 ffrom L: \MAST30028 to C:\…\MATLAB to :D\MATLAB and include it in the path. Now MATLAB knows how to find the files in Week4.
1 Confidence intervals
Exercise Set 1
This relates to material presented in Lecture 6 (Statistical errors).
2
a. b. c.
For your program in Exercise set 3a of Week 3, add code to compute a 95% confidence interval for the probability of winning the bet after performing n repetitions. Test your code.
By modifying your driver from Exercise set 3b of Week 3 or otherwise, run the simulation 100 times. How many times does the confidence interval fail to contain the exact answer?
* For the random experiment simulated in deMere1.m (4 rolls of a die), write a program to compute a 95% confidence interval for the difference between the largest value of the 4 rolls and the smallest, after n repetitions.
Monte Carlo integration
This relates to material presented in Lecture 7 ( Monte Carlo integration). For a standard normal random variable,
1 !1 −x2/2 P r(Z ≤ 1) = √ e
2π −∞
1 !1 −x2/2 dx = 0.5 + √ e dx
2π 0
Exercise Set 2
a. (using numerical quadrature)
Use the MATLAB function integral.m to find the value of P r(Z ≤ 1).
b. (Crude Monte Carlo) Now write a MATLAB program to use crude Monte Carlo, as described below, to estimate P r(Z ≤ 1).
“A second method uses the connection of the integral to the mean value of f(x), not to be confused with the expected value of a random variable. The mean value of a function f(x) over [a,b] is just
1!b
b−a f(x)dx
a
In the Crude Monte Carlo method, we generate values of x from U(a,b) and calculate the sample mean of the set of values {f(xi)}. Our estimate of the integral is then just b − a times the sample mean.”
How many function values do you need to get 2 decimal place accuracy?
c. (directly)
generate a set of samples from a standard normal random variable using randn, and count how many are less than 1. A one-liner?
1
3 Floating point numbers
These relate to material in Lecture 7( Floating point numbers)
Exercise Set 3
a. Run the M-file floatgui from the Week4 folder which illustrates a toy floating point system. Explain how the slider parameters emin, emax, t affect the set of machine numbers produced. What do you see switching to a log scale? Then back to linear scale?
b. Explain the output of the following MATLAB code
• format short e; x=1;k=0;
while x ~= 0
x=x/2;k=k+1;
end
x k
• format short e; x=1;k=0;
while isfinite(x)
x=x*2;k=k+1;
end
x k
• format short e; x=1;k=0;
while 1+x ~= 1
x=x/2;k=k+1;
end
x k
c. Predict and explain the result of the following commands (use format long e):
• x=realmax;x=x+1 x=realmax;x=2*x x=x/2
• x=realmin;x=x/2
• x=1+eps x=x-1
x=1+eps/2
x=x-1
x=8+eps
x=8+4*eps
x=8+5*eps
2