MATH 125 - Computing Worksheet 2 Name: Attach Mathematica (or Matlab) code solving the problems listed below. Computers are good at doing recursive tasks. Suppose, for example, we would like to compute factorials. By definition n! = n(n − 1) · · · 2 · 1. To turn this observation into something recursive, observe that 1! = 1 and n! = n · (n − 1)!. Here is how we would tell Mathematica to do this computation. Type the following in Mathematica. Clear[f]; f[1] = 1 f[n_] := nf[n-1] Then, if we want to compute, say, 10!, we just evaluate f [10]. By modifying the code above appropriately, you will be able to solve the following problems. Problem 1 Following the scheme above, define a recursive function that, starting with a seed value x0 , takes as input an integer n ≥ 0 and returns as output the n-th iteration of the Newton–Raphson approximation to the cube root of 5 with the given seed value. In this case, make sure you clearly indicate your starting value x0 . Run your function with x0 = 1, 5, 10, 25. In each case, how large must you take n to obtain the cube root of 5 to an accuracy of 5 decimal places (do this experimentally)? Problem 2 Newton’s method can also do strange things. The Newton–Raphson method for approximating x2n +1 . Starting with roots of x2 + 1 will start with some x0 (a seed value) and then defines xn+1 = xn − 2x n a seed value x0 , modify the code above to produce a function taking as input an integer n and returning as output the n-th iteration of the Netwon–Raphson approximation for the roots of x2 + 1. Note that x2 + 1 has no real roots! • What happens for large n when − √13 ≤ x0 ≤ • What happens for large n when |x0 | > √1 ? 3 √1 ? 3 • What happens as n varies if we seed with x0 = ± √13 ? Can you explain these observations? (Hint, define f (x) = x − which f (f (x)) = x?) x2 +1 2x . What are the (real) values of x for Problem 3 Consider the function h(x) = x3 − 2x + 2. As above, create a function that outputs the n-th iterate of the Newton–Raphson method for approximating roots with seed value x0 . What happens when you apply Newton’s method and seed with x0 = 0 or x0 = 1? Numerically evaluate this for the first 10 values of n. What happens if you begin with a seed value close to 0 or 1 (take seed value 0.1 and study what happens for different values of n)? What about the seed value 2? Graph the function. Can you explain this behavior (hint: draw a graph of the function and think pictorially about what Newton’s method does)? What happens if you choose different seed values? 1
© Copyright 2026 Paperzz