Y2 Computational Physics Laboratory Solutions to Ex. 2 Numerical methods: from a skydiver to radioactive decay Problem 1: Free fall motion with air resistance 1. You should include a printout of your code, and a brief description of it. Detail should be sufficient to allow the marker to easily follow, and understand, the logic and function of the code. The code itself should be well commented. [2 marks] Again, the usual comments apply for marking. Explanations should be clear, and easy to follow; as should the code, which should be well commented. Here is an example of code to solve the first part. #include <iostream> #include <math.h> using namespace std; int main() { // Declarations int i; // Integer to loop over time float alpha, k, h, t, v, beta, va; // Drag coefficient // = alpha / mass diver // Step size for Euler // Time // Velocity // Constant for analytical prediction (term. vel.) // Analytical solution const float g=9.81, // Accn. due to gravity m=100.0; // Mass of diver // Input values for drag, and step size: cout << "Input alpha >> "; cin >> alpha; cout << "Input h >> "; cin >> h; k=alpha/m; // Calculate k beta=sqrt(g/k); // Calculate beta // Into the main loop, setting t=0, v=0 first: t=0.0; v=0.0; for (i=0; i <= 25; i++) { t += h; v += h*(g-k*(v*v)); // Increment t // Euler estimate // Analytical value: va=beta*(1.0-exp(-2.0*beta*k*t))/(1.0+exp(-2.0*beta*k*t)); // Output on the fly cout << t << " " << v << " " << va << endl; } } 2. You should include output of the estimated velocity, and the actual velocity (from Equation [5]) as a function of time, for at least one value of h. There should also be a brief discussion of the impact of the choice of h on the accuracy of Euler’s method. [2 marks] Here is some sample output from the code, with = 0.4 and h = 1.0. Give [1] mark for sensible-looking output. Input alpha >> 0.4 Input h >> 1.0 1 9.81 9.68367 2 19.2351 18.6541 3 27.5651 26.3937 4 34.3358 32.6724 5 39.43 37.5162 6 43.0211 41.1102 7 45.4278 43.7003 8 46.9831 45.5281 9 47.9634 46.7988 10 48.5715 47.6732 11 48.9447 48.2706 12 49.1724 48.6767 13 49.3107 48.9518 14 49.3945 49.1379 15 49.4452 49.2634 16 49.4759 49.3481 17 49.4944 49.4051 18 49.5056 49.4436 19 49.5124 49.4695 20 49.5165 49.4869 21 49.519 49.4986 22 49.5205 49.5065 23 49.5214 49.5118 24 49.5219 49.5154 25 49.5222 49.5178 26 49.5224 49.5194 Press Enter to return to Quincy... The discussion over the choice of h should talk about the curvature of the function. The basic Euler method only works well provided the derivative does not change significantly over each step of h; which means the step should be kept small! The more mathematically inclined students might go as far as to derive the expression for the derivative, which is: d 2v dv 2kv . 2 dt dt This approaches zero as v approaches the terminal velocity. It is very small; so Euler works quite well. (This expression will come in handy later, in Problem 3.) Award [1] mark for a reasonable, qualitative explanation. 3. You should solve Equation [2] analytically, and include a clearly laid out proof in your book. [1 mark] You will need to make use of the integral identity: v dv 1 ln 2 v 2 v . 2 This is a straightforward proof; award 0.5 marks for any attempt. What is the physical significance of [0.5 marks]? This is the terminal velocity of the diver. Problem 2: Opening the parachute 1. You should include a brief discussion of the effect of h on the results. (Hint: think about the derivative of the function.) Also describe what you found with your initial choice of h; and give the values that in the end gave sensible results, saying why they did so. [1.5 marks] The modification to the code is very straightforward. If a value of h ~ 1 is still used, the results become non-physical very quickly. In this new regime, the derivative changes much more rapidly, i.e., our diver is rapidly decelerated. So a much smaller value for h is now needed to overcome the numerical shortcomings of the Euler method. The student must reduce h until the results look sensible; something like h = 0.01 will work. Problem 3: Radioactive Decay 1. You should include a printout of your code, and a brief description of it. Detail should be sufficient to allow the marker to easily follow, and understand, the logic and function of the code. The code itself should be well commented. [1 mark] Here is some example code: // // Radioactive decay, 24/08/05 (WJC) // // #include <iostream> #include <math.h> using namespace std; int main() { // Declarations int i; // Integer to loop over time float lambda, h, t, n, na; // Decay coefficient // Step size for Euler // Time // Estimated number of nuclei // Analytical solution // Input values for lambda, and step size: cout << "Input lambda >> "; cin >> lambda; cout << "Input h >> "; cin >> h; // Into the main loop, setting t=0, n=1000 first: t=0.0; n=1000.0; for (i=0; i <= 25; i++) { t += h; n += -lambda*h*n; // Increment t // Euler estimate // Analytical value: na=1000.0*exp(-lambda*t); // Output on the fly cout << t << " " << n << " " << na << endl; } } 2. You should include output data (Euler and analytical prediction) from your code, and explain why there is a difference in accuracy compared to the diver problem. [1 mark] Here is example output for the required input parameters Input lambda >> 0.5 Input h >> 1.0 1 500 606.531 2 250 367.879 3 125 223.13 4 62.5 135.335 5 31.25 82.085 6 15.625 49.7871 7 7.8125 30.1974 8 3.90625 18.3156 9 1.95313 11.109 10 0.976563 6.73795 11 0.488281 4.08677 12 0.244141 2.47875 13 0.12207 1.50344 14 0.0610352 0.911882 15 0.0305176 0.553084 16 0.0152588 0.335463 17 0.00762939 0.203468 18 0.0038147 0.12341 19 0.00190735 0.0748518 20 0.000953674 0.0453999 21 0.000476837 0.0275364 22 0.000238419 0.0167017 23 0.000119209 0.0101301 24 5.96046e-005 0.00614421 25 2.98023e-005 0.00372665 26 1.49012e-005 0.00226033 Press Enter to return to Quincy... This does not do any where near as well as for the diver problem; disagreement between the Euler and analytical values is quite marked. Again, this is down to the derivative of the function. Here: d 2N dN 2 N . 2 dt dt This is big for small t, when N is large; so Euler does not cope too well! This is to be compared with the equation derived for the derivative of dv / dt above.