Physics 202 Laboratory 3 Root-Finding 1 Laboratory 3 Physics 202 Laboratory The fundamental question answered by this week s lab work will be: Given a function F (x), find some/all of the values {x i } n i=1 for which F (x i) = 0. It s a modest goal, and we will use a simple method to solve the problem. But, as we shall see, there are a wide range of physical problems that have, at their heart, just such a question. We ll start in the simplest, polynomial setting, and work our way up to the shooting method. 3.1 Physical Problems We ll set up some direct applications of root-finding with familiar physical examples, and then shift gears and define a numerical root-finding routine that can be used to solve a very different set of problems. 3.1.1 Orbital Motion In two-dimensions, with a spherically symmetric potential energy (meaning here that V (x, y, z) = V (r), a function of a single variable, r x 2 + y 2 + z 2, the distance to the origin) we can use circular coordinates to write the total energy of a test particle moving under the influence of this potential as E = 1 2 m (ṙ 2 + r 2 φ2 ) + V (r). (3.1) Conservation of momentum tells us that the z-component of angular momentum is conserved, with L z = (r p) z = m r 2 φ, so we can rewrite the 1 Some of this material appears in Computational Methods for Physics by Joel Franklin, Cambridge University Press, 2013 and is used with the author s permission. 1
3.1. PHYSICAL PROBLEMS Laboratory 3 energy as: E = 1 2 m ṙ2 + 1 L 2 z 2 m r 2 + V (r) }{{} U(r) (3.2) where U(r) defines an effective potential energy we have turned a twodimensional problem into a one-dimensional problem for the coordinate r, and an effective potential that governs the motion in this setting. Since we know the energy of the system in terms of r, we can invert (3.2) to get: ṙ 2 = 2 E U(r) F (r). (3.3) m Now we can ask for the turning points of orbital motion (if/when they exist), those points at which ṙ = 0 the answer is provided by radial locations r i for which: F (r i ) = 0, (3.4) precisely the sort of root-finding problem of interest. In cases like Newtonian gravity, where V (r) 1/r, the resulting F (r) is just a polynomial (in fact, quadratic), so we don t need any fancy numerical solutions. But for more complicated potentials, root-finding can be used efficiently to isolate, at least numerically, the zeroes of the function F (r). 3.1.2 Area Minimization Many minimization problems end in functions that require numerical rootfinding. As a simple example of this type of problem, consider a surface connecting two rings of equal radii, R, separated a distance L as shown in Figure 3.1. We want to find the surface with minimal area soap films find these minimal surfaces automatically, there the soap film is taking advantage of a minimal energy configuration, leading to a stable equilibrium. The immediate goal is a function s(z) that gives the radius of the surface as a function of height. We ll take z = 0 at the bottom ring, then z = L is the height at the top ring. Our area expression follows from the azimuthal symmetry for a platelet extending from z to z + dz and going around an infinitesimal angle dφ, the area is: da = s dφ dz 2 + s (z) 2 dz 2, (3.5) 2
3.1. PHYSICAL PROBLEMS Laboratory 3 R s(z) L Figure 3.1: We want to find the function s(z), the radius as a function of height, associated with a surface connecting two rings of radius R, separated a distance L, that has minimal surface area. as can be seen in Figure 3.2. If we integrate this expression for the area in both φ and z, we get the total area of the surface: A = 2 π L 0 s(z) 1 + s (z) 2 dz. (3.6) This formula is nice, but it proceeds from a given function of s(z). There is a general method for taking such a functional (here A is a number that depends on the function s(z), so A is itself a function of the function s(z) we call those functionals) and minimizing it the result is an ODE for s(z) that can be used to find s(z) 2. When we carry out the minimization procedure in this problem, we get the following ODE, with appropriate boundary conditions: The general solution is: 1 + s 2 s s = 0 s(0) = R s(l) = R. (3.7) ( ) z β s(z) = cosh, (3.8) for independent real constants and β (with what dimensions?). Now for the z = 0 boundary, we have: ( ) β s(0) = cosh = R (3.9) 2 This procedure will become familiar to you in classical mechanics, it is an application of variational calculus. 3
3.1. PHYSICAL PROBLEMS Laboratory 3 s(z + dz) s(z + dz) s(z) s (z) dz s(z) dz dz2 + s (z) 2 dz 2 Figure 3.2: Calculating the infinitesimal hypotenuse for the platelet extending from z z + dz and going around an arc of s(z) dφ. and we must simultaneously solve: ( ) L β s(l) = cosh = R. (3.10) From the first equation, we can write β = cosh 1 ( ) R, and then the second equation becomes: ( ( )) L R cosh cosh 1 = R. (3.11) Define the function: ( ( )) L R F (x) x cosh x cosh 1 R (3.12) x it is clear that we are interested in the roots, those define the final constant of integration for our solution: F () = 0. Note the importance of actually plotting F (x) not all functions F (x) have roots. 3.1.3 E&M For the time-dependent fields in electricity and magnetism, we know that information (in that setting, the magnitude and direction of E and B at a particular point r = x ˆx + y ŷ + z ẑ at time t) travels at a finite and 4
3.1. PHYSICAL PROBLEMS Laboratory 3 specific speed: c. The immediate implication is that if we would like to know the electric field at our observation point (and time) due to a charge that is moving along some prescribed trajectory: w(t), we need to evaluate the location of the particle not at time t, but at some earlier time t r (the retarded time). The setup is shown in Figure 3.3 the particle of charge q is at w(t) at time t, but it is the earlier location w(t r ) that has information traveling at c to the observation point r at time t. ẑ q w(t) w(t r ) ˆx r at t ŷ Figure 3.3: A particle with charge q moves along the prescribed curve w(t). For the point r at time t, field information comes from the earlier time t r, when the charge was at w(t r ). The time it takes for the information from w(t r ) to reach r is t t r, and during that interval, the field information travels at c, so the distance travelled is c (t t r ). At the same time, the distance between r and w(t r ) is, geometrically, r w(t r ) putting these together, we have the defining equation for the retarded time: c (t t r ) = r w(t r ). (3.13) Notice that the retarded time t r is a function of t and r (the observation time and location), and is defined implicitly in terms of w(t r ). We may or 5
3.2. SHOOTING Laboratory 3 may not be able to solve this equation for t r given w (cannot be done for all but the simplest trajectories), but we can define a root-finding problem: F (T ) = c (t T ) r w(t ). (3.14) The roots of F (T ) will provide the retarded time. 3.2 Shooting We have a cannon that can fire projectiles with speed v at an angle θ. Question: What angle θ should we use to force our cannon to hit a target a distance R away? Here, we know the answer automatically the trajectory of the slug is given by: x(t) = v cos θ t y(t) = v sin θ t 1 2 g (3.15) t2, and we can solve for y as a function of x, since t = v cos θ, then y(x) = tan θ x 1 ( 2 g x ) 2. (3.16) v cos θ Now the range R is the location of x when y = 0, a root-finding issue, of course, but in this case, we can solve directly: R = v2 g sin(2 θ) θ = 1 ( ) R g 2 sin 1 v 2. (3.17) Notice that one important element of this calculation was our ability to make the height, y, a function of x. We can do this pretty generically starting from Newton s second law if y(t) y(x(t)), then dy dt = dy(x) dx dx dt = y (x) v x, so that we take d dt v x d dx, then F x = m v x dv x dx F y = m v x [ dvx dx dy dx + v x x d 2 ] y dy dx 2 = F x dx + m d 2 (3.18) y v2 x dx 2, and in this form, we can start with almost any force, and develop the ODE version of the range formula with height parametrized by x. 6
3.2. SHOOTING Laboratory 3 As a check, take F x = 0, and F y = m g, then the above reads: dv x dx = 0 y (x) = g vx 2, (3.19) and v x is a constant, equal to v cos θ for us, so y g (x) = v 2 cos 2 θ y(x) = tan θ x 1 2 g x 2 v 2 cos 2 θ, (3.20) as before. This time, the tan θ term comes up naturally, since the x derivative is related to the time derivative at zero: ẏ(0) = y (0)v x (0), and v x (0) = ẋ(0) = v cos θ, so we have ẏ(0) = y (0) ẋ(0), and then y (0) = ẏ(0)/ẋ(0) = tan θ. Suppose, for fun, we try introducing wind-resistance, a drag term of the form: F g = γ v v. This gives us an additional force in both directions, and Newton s second law tells us that: m ẍ = γ ẋ 2 + ẏ 2 ẋ m ÿ = m g γ ẋ 2 + ẏ 2 ẏ, (3.21) from which we learn, using (3.18), that: v x(x) = γ 1 + y m (x) 2 v x (x) y (x) = g v x (x) 2. (3.22) There is no longer any simple answer, but we can still imagine solving this equation numerically; it is, after all, a second order ODE, and we have a method for solving those. Suppose we ask the same question, in this context: Given v, find θ so that a projectile hits a target a distance R away from the starting point. Our initial conditions are v x (0) = v cos θ, and y (0) = tan θ. Define the function: Verlet(θ) to be the numerical solution for y(r) given an angle θ, then we have a function whose zero is precisely the correct θ, i.e. we want to find the roots of: F (θ) = Verlet(θ), (3.23) those roots will be the values of θ for which y = 0 at x = R. So we can define a numerical function of a single variable, and perform our (numerical) root-finding on that. 7
3.3. BISECTION Laboratory 3 3.3 Bisection Finally, we come to the relevant numerical method we will use bisection to find a zero of a function F (x) lying in between two initial points, x l and x r. How do we know that there is a root between those two points? What if there is more than one root in there? The easiest thing to do is to plot F (x), always an option, and see roughly where the zero crossings are. Then bookend a particular zero of interest, and hone in on it using the bisection routine. Bisection itself is almost entirely defined by its name. We start with two locations, x l and x r, we evaluate F (x l ) and F (x r ) if a root lies between these two, then one will be positive, and the other negative. Now, we evaluate the function F at the midpoint x m 1 2 (x l + x r ). If F (x m ) has the same sign as F (x l ), then the root lies between x m and x r if F (x m ) has the same sign as F (x r ), then the root lies between x l and x m. In either case, we update the labels x l and x r appropriately, so that the root now lies in an interval half as large as the original one. We continue this process until F (x m ) is as small as we want (set by some external tolerance ɛ 10 12 or so). A schematic of the first few steps of the process is shown in Figure 3.4. F (x) 2 4 x x 1 l x 1 r x 2 l x 2 r x 3 l x 3 r Figure 3.4: The successive bisections for the function F (x). Here, x n l and x n r refer to the left and right endpoints of the interval for the n th iteration of the bisection. 8
3.3. BISECTION Laboratory 3 Lab In this lab, you will implement the bisection routine sketched in the notes. You can use either a While loop, or a recursive approach. The first problem should ensure that your routine is working properly. Don t forget, in all cases, to plot the function whose roots you are trying to find that will allow you to successively bracket them for bisection honing. Problem 3.1 Write your bisection function it should take, as arguments, a function F (the function whose roots we are interested in), an initial bracketing, a pair xl and xr, and a tolerance eps that specifies how close to zero we should be before exiting (when F (x m ) < ɛ, i.e. when the midpoint value gives an F that is close to zero). Try your bisection routine on F (x) = x 3 π x 2 2 x + 5. (3.24) Find all three roots, using eps= 10 8 (use this value of ɛ for the remaining problems as well), and record your results below (show five digits): 9
3.3. BISECTION Laboratory 3 Problem 3.2 Find the minimum surface area solution using (3.12) with L = 1/2, R = 1. The total area is given by [ ( ) ( )] 2 β L L A = π L + cosh sinh. (3.25) Sketch s(z) for each of the two roots of (3.12) and identify the area-minimizing one does its magnitude make sense (provide a rough estimate)? Problem 3.3 For a charge q moving in a circle in the ˆx ŷ plane: w(t) = R cos(ωt) ˆx + R sin(ωt) ŷ, find the retarded time to the point r = R ˆx at time t = 0 for R = 10 9 m, ω = 1 2 c/r. What is the location of the charge at this time? What does this location imply for the existence of circular motion for a pair of equal and oppositely charged particles? 10