$30
Problem 1: Newton’s method in 1 dimension
(a) Newton’s method for solving a scalar nonlinear equation π (π₯) = 0 requires computation of the derivative of π at each iteration. Suppose that we instead replace the true derivative with a constant value π, that is, we use the iteration scheme:
π₯π+1 = π₯π − π (π₯π)/π.
i) Under what conditions on the value of π will this scheme be locally convergent?
ii) What will be the convergence rate in general?
iii) Is there any value of π that would still yield quadratic convergence?
(b) Write a Python function to implement Newton’s method in 1d. Your function should take as inputs the 1d function, its derivative, an initial guess and a tolerance for checking convergence. Use this function to find the roots of the following functions using Newton’s method with the provided initial guess.
i) π₯2 − 1 = 0, π₯0 = 106. ii) (π₯ − 1)4 = 0, π₯0 = 10. iii) π₯ − cos π₯ = 0, π₯0 = 1.
What is the observed convergence rate of Newton’s method for each problem? Why does each problem exhibit this convergence rate?
You can compute the rate by using the fact that the error at each iteration ππ = π₯∗ − π₯π satisfies |ππ+1| / |ππ|π = πΆ. Thus, |ππ+1| / |ππ|π = |ππ| / |ππ−1|π from which you should derive an equation for obtaining π for each iteration. You can use the computed solution in the last iteration as the true solution π₯∗ and the last of these computed rates to be the rate of convergence.
Problem 2: Newton’s method for a system
Consider the conversion formula from spherical to Cartesian coordinates:
π₯ = π sin π cos π
π¦ = π sin π sin π
π§ = π cos π
(a) Write a function newton(f, J, x0, tol=1e-12, maxit=500) that implements Newton’s method generically, with a starting guess of x0.
f is a function that accepts a numpy array π₯ of the current state and returns the function value π (π₯) as another numpy array. Write f so that π (π₯) = 0 at the sought solution. J is the Jacobian of f, so given a numpy array x, it returns a Jacobian matrix of the appropriate dimensions.
Terminate the iteration once either the residual is smaller than tol or maxit iterations have been performed.
(b) Write a function that, given π₯, π¦, and π§, finds π, π and π, using the Newton implementation from part (a). Do not reimplement Newton’s method, simply pass in appropriate functions f and J to newton from part (a).
Find a starting guess that leads to convergence in your experiments.
The routine should accept π₯, π¦, π§ in one input vector and return π, π and π in one output vector.
(c) Test your work from (a) and (b) by drawing 10 random vectors from π₯∗ ∈ β3 using np.random.randn and finding their spherical coordinates.
For each case, print the final relative residual βπ₯ − π₯∗β2 / βπ₯∗β2, where π₯ are the Cartesian coordinates corresponding to the spherical coordinates returned by your routine.
π
Also compare the polar coordinates π€Μ = [πΜ πΜ πΜ] output by your routine with the true values computed using the formulas:
π = √π₯2 + π¦2 + π§2
π = arccos (π§ )
π
π¦
π = arctan ( )
π₯
π
Let π€ = [π π π] . For each example, print the relative error βπ€ − π€Μβ2 / βπ€β2 in your computed values. Is this small whenever the residual is small? Why/why not?
Hints:
• Read the documentation for and use np.arctan2 in implementing the formulas to find π, π, and π.
• It is instructive to watch the conditioning of the Jacobian. A singular Jacobian will cause the method to break down or compute inaccurate results. Some starting guesses are more prone to this than others.
Problem 3: Chebyshev polynomials, Vandermonde matrices 50 points
(a) A function πΉπ(π‘) is known to satisfy the Chebyshev three-term recurrence if the following properties hold:
πΉ0(π‘) = 1
πΉ1(π‘) = π‘
πΉπ+1(π‘) = 2π‘πΉπ(π‘) − πΉπ−1(π‘)
Show that the function
πΉπ(π‘) = cos(π arccos(π‘))
satisfies the Chebyshev three-term recurrence. Hint: Remember the addition formula for cosines:
cos(πΌ + π½) = cos(πΌ) cos(π½) − sin(πΌ) sin(π½).
How would this apply to cos((π + 1) arccos(π‘))?
(b) Deduce that πΉπ(π‘) is a polynomial. This is called the Chebyshev polynomial.
(c) The generalized Vandermonde matrix π for a set of points π₯1, … , π₯π with a set of functions
π1, … , ππ is given by
πππ = ππ(π₯π).
Notice that this matrix captures an essential bit of interpolation: it maps coefficients with respect to the functions (ππ) to point values at the points (π₯π):
Clearly, π −1 describes the reverse process, and the conditioning of π can tell us quite a bit about how well-behaved of an operation interpolation is with respect to the given sets of functions and nodes.
Perform the following steps:
i) Construct π × π generalized Vandermonde matrices for π = 5, 10, 15, … , 100.
ii) Plot the condition number of the matrix with respect to π. for each of the cases below:
i. Equispaced nodes π₯π in the interval [−1, 1] (endpoints included), with the monomials ππ(π₯) = π₯π.
ii. Chebyshev nodes in the range [−1, 1] are given by
π₯π = cos ( π) ,
with the monomials.
iii. Equispaced nodes π₯π in the interval [−1, 1] (again, endpoint included), with the Chebyshev polynomials πΉπ.
iv. Chebyshev nodes in the interval [−1, 1] (see equation above) with the Chebyshev polynomials πΉπ.
Your output should include one (clearly labeled) plot as described above showing (and comparing) the behavior of the condition number for all four cases.
Hint: Use matplotlib.pyplot.semilogy() to plot the values with a linear scale in π on the π₯ axis and a logarithmic scale for the condition number on the π¦ axis.
(d) Which of the combinations performs best?
Problem 4: Interpolation, Newton and Cubic Spline
(a) The following formula is called the divideddifferences approach to computing the coefficients of an interpolating polynomial using Newton’s polynomials as a basis:
π [π‘1, π‘2, … , π‘π] β π [π‘2, π‘3, … , π‘π] −π‘π −π [π‘π‘11, π‘2, … , π‘π−1]
π [π‘π] β π (π‘π)
Prove that, using mathematical induction, that indeed this approach gives the coefficient of the πth basis function using the Newton interpolation polynomial.
(b) Given the three data points (−1, 1), (0, 0), (1, 1), determine the interpolating polynomial of degree 2 (using hand calculations alone and showing all necessary steps) using:
i) Using the monomial basis. ii) Using the Lagrange basis. iii) Using the Newton basis.
iv) Show that all three representations give the same polynomial.
(c) Consider interpolating a given set of data points (π₯π, π¦π), π = 1, … , π using natural cubic splines. Write a code to set up and solve the linear system that performs this interpolation. Plot the resulting cubic spline along with the data. For the data, pick π = 6 random points (π₯π)ππ=1 on [0, 1) with values (π¦π)ππ=1 in [0, 1).
Hint: Make sure you sort the π₯π’s after you draw the random numbers and before you start constructing the spline, to avoid confusing your spline construction code.
SubmissionNotes
1. The answer to all theoretical problems, output of Python code for computational problems including figures, tables and accompanying analyses should be provided in a single PDF file along with all your code files.
2. You can typeset (please consider using LATEX if you choose to do so), or write by hand and scan/take photographs of solutions for theoretical questions. For scanning or photography, please put in the extra effort and provide a single PDF file of your submission.
3. For Problem 2, submit your code in one file: problem_2.py. You can use the Python file provided as a boilerplate.
4. For Problem 3, submit your code in one file again: problem_3.py. You will of course, copypaste the functions written in problem_2.py for Gaussian elimination without and with partial pivoting into this file.