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)
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. diagnostics
to the function that turns the printing on and off. Make diagnostics
default to false
. bisectsearch
function so that it operates equally well on any floating-point type: Float16, Float32, Float64, or BigFloat.if a; b; end
statements for your checks, use the more Juliaesque a && b
short-circuit conditional evaluation. Likewise, instead of using if a; b ; else c ; end
statements, use the a ? b : c
ternary operator. See http://docs.julialang.org/en/release-0.4/manual/control-flow/#man-conditional-evaluation.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
diagnostics
argument that defaults to false
if-then
, where appropriate, to make your code more compact and readableProblem 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?