MATH 1170: Calculus for Biologists I (Fall 2010) Lab Meets: November 23, 2010 Report due date: November 30, 2010 Section 002: Tuesday 9:40−10:30am Section 003: Tuesday 10:45−11:35am Lab location − LCB 115 Lab instructor − Erica Graham, graham@math.utah.edu Lab webpage − www.math.utah.edu/~graham/Math1170.html Lab 11 General Lab Instructions In−class Exploration Review: Last time, we used Maple to approximate functions using linear and quadratic functions, as well as with Taylor polynomials. We learned that we can use approximations to determine a function’s value at a particular point. Background: In this week’s lab, we will learn how to implement Newton’s method, in order to approximate the solution of an equation. This will require our extensive knowledge of discrete−time dynamical systems. restart; read("/u/ma/graham/public_html/Math1170/files/cobweb"): Suppose we have the discrete−time dynamical system described by the updating function g(x). We want to find the equilibrium point satisfying g(x) = x or, equivalently, g(x) − x = 0. g:=x−>(−6*x^3/3+9*x^2−11*x+9/2)/(x^2+1)^2; 9 2 x3 9 x2 11 x 2 g := x (2.1) 2 x2 1 As usual, we will plot the updating function with the diagonal. plot([x,g(x)],x=0..2,0..5,labels=["x[t]","x[t+1]"]); 5 4 x[t+1] 3 2 1 0 0 0.5 1 x[t] 1.5 2 We can see from the graph there is one equilibrium, for which we can easily solve in Maple. eqm:=fsolve(g(x)=x); eqm := 0.5349475021 (2.2) But, what if Maple didn’t have this built−in capability? How could we advise it to proceed, if we knew that the equilibrium existed? This is where Newton’s method comes in. First, we will define a new function f(x) = g(x) − x. Notice that when f(x) = 0, this is exactly equivalent to when g(x) = x, which is what we want to solve in the first place. We can use unapply to view f(x) easily. f:=unapply(g(x)−x,x); plot(f(x),x=0..2,−2..5,color="Black"); 9 2 x3 9 x2 11 x 2 f := x x 2 2 x 1 5 4 3 2 1 0 1 0.5 1 x 1.5 2 2 Now that we’ve modified our problem to one of solving f(x) = 0, we can get a broad idea of where the solution might lie. We know it’s between 0 and 1, so we can choose 1 arbitrarily and create a tangent line approximation around this point. Recall that the tangent line approximation has the form f_tan(x) = f(a) + f’(a)*(x−a). To proceed, we will find f’(x) and use it to define a general tangent line at base point a. Like last week, we will define f_tan as a function of two things: x and a. fprime:=simplify(unapply(diff(f(x),x),x)); f_tan:=unapply(f(a)+fprime(a)*(x−a),[x,a]); 9 4 2 x3 9 x2 11 x x 2 6x 18 x 11 2 fprime := x 1 2 3 x2 1 x2 1 9 2 a3 9 a2 11 a 2 6 a2 18 a 11 f_tan := x, a a (2.3) 2 2 2 2 a 1 a 1 9 3 2 4 2a 9a 11 a a 2 1 x a 3 a2 1 Now we can evaluate f_tan(x, 1), which is the tangent line approximation of f(x) at 1. Since f_tan(x) is a linear approximation, we can easily solve f_tan(x)=0 for x. For later use, we will assign 1 to x[0]. x[0]:=1; We can simply plot the original function along with the approximation. plot([f(x),f_tan(x,x[0])],x=0..2,−2..5,color=["Black", "DodgerBlue"],linestyle=[1,4],legend=["f(x)","a = x[0]"]); x0 := 1 5 4 3 2 1 0 1 0.5 1 x 1.5 2 2 f(x) a = x[0] Notice where f_tan(x,x[0]) hits the x−axis. It’s pretty close to where f(x) does. What if we created another tangent line approximation, this time starting at the point where f_tan(x,x[0])=0? Let’s solve for this point first, calling it x[1]. x[1]:=fsolve(f_tan(x,x[0])=0,x); Now we can plot f(x) with our 2 approximations. plot([f(x),f_tan(x,x[0]),f_tan(x,x[1])],x=0..2,−2..5,color= ["Black","DodgerBlue","LimeGreen"],linestyle=[1,4,4],legend=["f(x) ","a = x[0]","a = x[1]"]); x1 := 0.1250000000 5 4 3 2 1 0 1 0.5 1 x 1.5 2 2 f(x) a = x[1] a = x[0] The new linear approximation, f_tan(x,x[1]), seems to cross the x−axis at a point that’s closer than our previous approximation. We can see how much more we have to go by solving f_tan(x,x[1])=0 for x, which we will save to x[2], and comparing this to the actual ’eqm’ value we solved for at the beginning. x[2]:=fsolve(f_tan(x,x[1])=0,x); eqm; x2 := 0.3978663298 (2.4) 0.5349475021 We went from 0.125 to about 0.3979, but still have to make it above 0.5. If we do the process one more time, we can try to do better. plot([f(x),f_tan(x,x[0]),f_tan(x,x[1]),f_tan(x,x[2])],x=0..2,−2. .5,color=["Black","DodgerBlue","LimeGreen","DarkOrange"], linestyle=[1,4,4,4],legend=["f(x)","a = x[0]","a = x[1]","a = x[2] "]); 5 4 3 2 1 0 1 0.5 1 x 1.5 2 2 f(x) a = x[1] a = x[0] a = x[2] It looks like our new approximation is even closer. Let’s verify this by calculating the value at which the ’a = x[2]’ line crosses the x−axis. x[3]:=fsolve(f_tan(x,x[2])=0,x); x3 := 0.5077624488 (2.5) We’ve now achieved one digit of accuracy. If we continued this process, we would get closer and closer to the actual solution. Recap: So far, what we’ve done is made an initial guess of where we thought the solution was and called it x[0]. Then, we created a tangent line approximation with the base point x[0]: f_hat(x,x[0]) = f(x[0]) + f’(x[0])*(x− x[0]). Solving f_hat(x,x[0])=0 for x, we were able to find a new point, x[1], which has the following formula: x[1] = x[0] − f(x[0])/f’(x[0]). We contined by doing this process again with an approximation at base point x[1]: f_hat(x,x[1]) = f(x[1]) + f’(x[1])*(x−x[1]). Solving f_hat(x,x[1])=0 for x gave us yet another point, x[2], with the formula x[2] = x[1] − f(x[1])/f’(x[1]). Carrying out the approximate/solve steps once more, we ended up with x[3], which got us closer to the answer we sought. The formula for this last point follows the same trend as above: x[3] = x[2] − f(x[2])/f’(x [2]). See the pattern? Notice that it’s the x[t] values that we actually care about. We’ve just created the following discrete dynamical system to solve an equation: x[t+1] = x[t] − f(x[t])/f’(x[t]). This system is called the Newton’s Method discrete−time dynamical system. We can take almost any equation and solve it numerically by solving this system. Now, instead of going through the trouble of computing tangent lines (which we use because they are easy to solve) at each step, we can just solve the discrete system with the updating function f_newton(x) = x − f(x)/f’(x), using our normal iterative procedure. f_newton:=unapply(x−f(x)/fprime(x),x); (2.6) 2 x3 9 x2 2 f_newton := x x x 6 x2 18 x 11 4 9 2 11 x x 2 1 3 2x 2 9x 11 x 9 2 (2.6) x 1 2 3 2 2 x 1 x 1 This function may look kind of ugly, but we can use some of the functions in the ’cobweb’ file that we’ve used in the past to iterate it and create a cobweb diagram. We can then compare the iterated solutions to the target solution. The input form for the three commands we will use is (function,start t, end t, initial guess) cweb(f_newton,0,10,x[0]); ## cobweb diagram iterplot(f_newton,0,10,x[0]); ## plot solutions x[t] versus t iterlist(f_newton,0,10,x[0]); ## list of the t values and their corresponding solutions eqm; ## to remind ourselves of the real answer 2 1.5 x[t+1] 1 0.5 solution 0 0 0.5 1 x[t] 1.5 2 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0 2 4 6 iteration number 8 10 0 1 1 0.1250000000 2 0.3978663299 3 0.5077624487 4 0.5336514467 5 0.5349444301 6 0.5349475021 7 0.5349475021 8 0.5349475021 9 0.5349475021 10 0.5349475021 0.5349475021 (2.7) From the cobweb diagram, we can see that the equilibrium point of the discrete system x[t+1] = f_newton(x [t]) is the solution to our original equation f(x) = 0, which solves the equation g(x)=x for our original discrete−time dynamical system. From the list we printed, it appears that Newton’s Method arrives at the correct answer after 6 iterations. Depending on the function, it could take even fewer iterations than this. But, it can also take many more depending on both the function and the initial guess. In some cases, Newton’s Method may fail completely. Lab 11 Homework Problems Please copy this entire section into a new worksheet, and save it as something you’ll remember. Your Full Name: Your (registered) Lab Section: Useful Tip #1: Read each problem carefully, and be sure to follow the directions specified for each question! Useful Tip #2: Don’t be afraid to troubleshoot! Does your answer make sense to you? If not, explore why. If you’re still unsure, ask me. Paper−saving tip: Make the size of your output graphs smaller to save paper when you print them. Please ask me if you’re unsure of how to do this. (You can see how much paper you’d use beforehand by going to File Print Preview.) This assignment will explore different examples that affect both the efficiency and success of Newton’s Method. (0) Read in the ’cobweb’ file by simply executing the following line. read("/u/ma/graham/public_html/Math1170/files/cobweb"): Part I: Efficiency. In the in−class exercises, we used Newton’s Method to find an equilibrium point, which basically amounted to solving a particular equation (i.e. g(x) =x). There are some equations that are not solvable algebraically, for which numerical solutions are the only option. exp(−x) = 2*x is one of these. (1)(a) Define the function h(x), so that the solution of h(x)=0 is exactly equal to the solution of exp(−x) = 2*x. Refer to the in−class portion for assistance. ## define h(x) (b) Define and compute the function hprime(x), the first derivative of h(x) with respect to x. (You’ll want to use unapply( ).) ## find hprime(x) (2)(a) Define the updating function for Newton’s Method, N(x), that will numerically solve h(x) = 0, using the function and derivative you defined above. ## N(x) goes here (b) Plot N(x) and the diagonal for x values between 0 and 1. ## plot (3)(a) Use iterlist( ) (from the ’cobweb’ file) to iterate the solution of the equation exp(−x) = 2*x from t=0 to t=10, with an initial guess anywhere between 0 and 1 (you choose). ## iteration 1 (b) At what value of t does x[t] stop changing? (c) Choose a different initial guess that’s 0.5 away from your first one, and use iterlist( ) again to iterate the solution from 0 to 10. Note: your new initial guess should still be between 0 and 1. So, if you started with something smaller than 0.5, you need to add 0.5 to get your new guess; if you started with something bigger than 0.5, you need to subtract. ## iteration 2 (d) Is the t value at which x[t] stops changing here the same as, larger or smaller than the you had in part (b)? (4)(a) What does this say about the efficiency of Newton’s Method in the two different cases? (b) How would you explain the increased, decreased or unchanged efficiency? (5)(a) Based on your two iteration experiments, what value of x satisfies exp(−x)=2*x? Write your answer to 10 decimal places. (b) Use fsolve( ) to compare your previous answer to the actual one. ## find the actual solution Part II: Success. We just saw an example of an extremely efficient performance of Newton’s Method applied to an impossible−to−solve−algebraically equation. Now we’ll explore a case in which we have an easy−to−solve− algebraically equation using the function s(x) = x^(2/5). (6)(a) Solve (by hand!) s(x) = 0 for x. (b) Define s(x) for Maple. ## define s(x) (c) Define and compute sprime(x), the derivative of s(x) with respect to x. (You’ll want to use unapply( ).) ## find sprime(x) (7)(a) Define the updating function for Newton’s Method, N(x), that will numerically solve s(x) = 0, using the function and derivative you defined above. ## N(x) goes here (b) Define x[0] = 1, and compute the first 4 iterations of the discrete system x[t+1] = N(x[t]), by typing out the individual terms. Save these terms to x[1], x[2], x[3] and x[4]. ## x[0] ## x[1] ## x[2] ## x[3] ## x[4] (c) Do your iterations seem to be approaching anything? If not, what do they appear to be doing? (8)(a) Use cweb( ) to view a cobweb diagram of the Newton’s method discrete−time dynamical system from t=0 to t=14, with the initial guess x[0]. Then use iterplot( ) to plot the solution x[t] versus t. ## cobweb ## plot iterations (b) What’s happening to the solution as the iterations increase? (c) Does this result match the pattern you noticed with the 4 iterations you computed above? (d) Is Newton’s Method a success in this case? (9)(a) Write N’(x), the derivative of N(x) below. (b) Recall solution you wrote for s(x) = 0, noting that this is also the equilibrium for the system x[t+1] = N (x[t]). What is the value of N’(x) at the equilibrium? Write your answer below. (c) Given this answer, explain what major factor impacts the success of Newton’s Method. (Hint: Think about what your answer in part (b) says about the equilibrium point.)