Chapter 3: Worksheet 10 Jack K. Cohen Colorado School of Mines
Newton's Method, Equation Solving with
Suggested Problems Section 3.9: Read section 3.9.
f[x_] := x^3 - 3x^2 + 1 x = 0.5 x = x - f[x]/f'[x]Here we have defined the ``cork ball'' function in accordance with syntax and defined a starting value at the midpoint of an interval that we have proved contains a root. The third line computes the ``new'' value of x (xn+1) on the left from the ``old'' value (xn) used on the right (the ``old'' value is 0.5 the first time we do this). We can then ``Enter'' the third line over and over again to generate a sequence of approximations. On this example, you should get successively:
0.666667 0.652778 0.652704 0.652704 0.652704This agrees with the value found in the text. Apply this approach to the following problems:
f[x_] := x^3 - 3x^2 + 1 x = 0.5; Do[ x = x - f[x]/f'[x]; Print[x], {8}]which gives the ``cork ball'' output:
0.666667 0.652778 0.652704 0.652704 0.652704 0.652704 0.652704 0.652704The ``8'' that appears in curly brackets at the end of ``Do Loop'' tells how many times to iterate.
Important and subtle point: By default, tries to compute with exact arithmetic. This can take a long time, so it is usually best to include a decimal point (e.g., 0.5 instead of 1/2) in the starting value to signal to that infinite precision is not required.
Since Newton's method often converges quickly, let's print the results to greater precision by altering the Print statement:
f[x_] := x^3 - 3x^2 + 1 x = 0.5; Do[ x = x - f[x]/f'[x]; Print[ N[x, 16] ], {8}]
0.6666666666666666 0.6527777777777778 0.6527036468361321 0.6527036446661393 0.6527036446661393 0.6527036446661393 0.6527036446661393 0.6527036446661393
Apply this approach to the following problems:
First a cubic with a parameter—Solve is our only chance:
equation = x^3 - 2a x^2 - 4 x + 8a == 0; solutions = Solve[equation, x] {{x -> 2 a}, {x -> 2}, {x -> -2}} (* the solutions written as rules *) equation /. solutions (* check solutions by substituting back *) {True, True, True} (* all 3 check exactly *)Solve works ok here, because the equation was special (an answer that is 17 pages long isn't of much use and that can easily happen with an arbitrary cubic). Here is a quadratic for which we get exact solutions with Solve:
equation = x^2 - 3x + 4 == 0; Solve[equation, x] 3 + I Sqrt[7] 3 - I Sqrt[7] {{x -> -------------}, {x -> -------------}} 2 2If we only want numerical approximations, it is more efficient to use NSolve:
equation = x^2 - 3x + 4 == 0; NSolve[equation, x] {{x -> 1.5 - 1.3228756555323 I}, {x -> 1.5 + 1.3228756555323 I}}
Use NSolve to get all three roots of the ``cork ball'' equation.
equation = Cos[1000/r] == r/(r + 10); NSolve[equation, r] Solve::ifun: Warning: Inverse functions are being used by Solve, so some solutions may not be found. Solve::tdep: The equations appear to involve transcendental functions of the variables in an essentially non-algebraic way. 1000 r NSolve[Cos[----] == ------, r] r 10 + rAll those messages are NSolve's way of saying it can't solve the equation. In this case, you must use FindRoot. FindRoot requires additional information—at the minimum a starting guess (as does Newton's method). By previous trial and error, we know there's a root near 50,000, so:
lhs = Cos[1000/r] - r/(r + 10); FindRoot[lhs == 0, {r, 50000}] {r -> 50008.3} solution = N[ FindRoot[lhs, {r, 50000}], 16] {r -> 50008.32913584035} lhs /. solution -11 -1.10986 10Notice the techniques for printing more decimal places and for checking the accuracy of the solution. Also observe that it is still an open question of how to get that starting value without trial and error—the next Chapter will give us a technique for doing that.