# channelflow.org

### Site Tools

gibson:teaching:fall-2016:math753:hw2

# Math 753/853 HW2

Here is the Julia Notebook for HW2. Please download the notebook, fill it in, save your completed work as a notebook file with name math753-hw2-lastname.ipynb, and submit it either through Canvas or via email.

For your convenience reading online, the questions for HW2 are listed below.

Problem 1. Write a Julia function bisectsearch that take a function and an interval such that , returns a root of within that interval. Find the root to as many digits accuracy as possible.

Challenges: (you don't have to do these, but you should do some if you want an A)

• Add checks to your bisectsearch function that verify that the starting conditions for bisection search are met. The checks should print a helpful error message and exit the function while returning the most reasonable value for the root from available information.
• Think carefully about the stopping conditions for bisection search. What kinds of problems are possible for this algorithm when it's implemented in floating-point arthmetic? Add checks to your function that stop iteration and return the most reasonable value for the root. In some cases you might want to print an error or warning message.
• Add some diagnostic printing to your function that prints out , and at each iteration. Add another argument diagnostics to the function that turns the printing on and off. Make diagnostics default to false.
• Write your bisectsearch function so that it operates equally well on any floating-point type: Float16, Float32, Float64, or BigFloat.

Problem 2. Apply the bisection search algorithm to find all real roots of

(a)

(b)

(c)

Problem 3. Write a Julia function newtonsearch that take a function and an initial guess , and returns a root of using the Newton method. Find the root to as many digits accuracy as possible.

Challenges:

The Newton method requires the derivative as well as the function . How are you going to compute the derivative?

Unlike bisection, which is guaranteed to converge, the Newton method can go haywire. Think of a good way to test if the Newton method is failing, and print an error message and exit in this case. Your function should still return a number of the same floating-point type as . In Julia, this is known as type stability.

As in the bisection search challenges

• think carefully about the appropriate stopping conditions for the search, given the finite precision of computer arithmetic
• make sure your code operates equally well on all floating-point types.
• add diagnostic printing to your code and turn it on/off with a diagnostics argument that defaults to false
• use short-circuit boolean and ternary operators instead of if-then, where appropriate, to make your code more compact and readable

Problem 4. Apply the Newton method to find all real roots of

(a)

(b)

(c)

Problem 5. Modify your bisection-search and Newton-method functions so that, along with the root they return a vector of errors for . Then solve using both bisection and Newton method, and make a plot comparing versus for the two methods. Put both the bisection errors and the Newton method errors on the same plot. Use blue circles for bisection errors and red squares for Newton method. Make the vertical axis logarithmic.

We have theoretical estimates of the convergence rates of bisection and Newton method. Do your error plots fit this theory, or not? Explain your answer in reference to the error plots.

Bonus problem 6. Use your Newton-method function to find the root of starting with initial guess , and plot the error versus as in problem. Does the Newton method converegence toward the true solution at the expected rate? Why or why not?

Bonus problem 7. Consider . What is the root of this function? What happens when you apply your Newton-method algorithm to this function with a starting guess ? Why?