Assignment approximate solver for non linear equations

3

Click here to load reader

description

Using newton and steffenson's method to find approximate roots to arbitirary equations.

Transcript of Assignment approximate solver for non linear equations

Page 1: Assignment approximate solver for non linear equations

Math 3316, Fall 2014Due Oct. 8, 2013

Project 2 – Nonlinear SolversThe first two sections of this project (Newton and Steffensen) will be checked in lab on Monday,Sep. 29 – this completion grade will count for 10% of the overall project.

The final project is due by 5:00 pm on Tuesday, October 7, and should be uploaded to Black-board. Instructions on what should be turned in are included at the end of this document.

Late work will not be accepted.

1. Newton’s Method:A C++ function bisection.cpp for approximating the root of a nonlinear function using thebisection method over a given interval has been provided on the course web page. In addi-tion, the test routine test bisect.cpp has been provided that tests this solver on the functionf(x) = x3 − 2x − 5, finding a value of x that solves the root-finding problem f(x) = 0 to atolerance of 10−4, using a starting interval of [−5, 5].

Create a new C++ file named newton.cpp that contains a function to perform the Newtonmethod (Section 3.2) to approximate the root of a provided function to a specified tolerance.The solver function should have the calling syntax

double newton(double (*f)(const double, void *data),

double (*df)(const double, void *data),

double x, int maxit, double tol,

bool show_iterates, void *data);

where f defines the nonlinear residual function to solve, df is the derivative of that function, x isthe initial guess, maxit is the number of allowed iterations, tol is the desired solution tolerance,show iterates is a flag to enable/disable printing of iteration information to the screen duringthe Newton solve, and data is a user pointer for any auxiliary data needed to evaluate thefunctions. At each iteration, have your method output the current iteration index, the currentsolution guess, x, the absolute value of the solution update, |h|, and the absolute value of thecurrent residual, |f(x)|.

Create a C++ program test newton.cpp to verify that your newton function works as desiredon the root-finding problem f(x) ≡ 1

5(x− 5)(x+ 2)(x+ 3) = 0. For your tests, start with initialguesses of x0 = {−1, 2, 3}, and solve the problem to tolerances of ε = {10−3, 10−7, 10−11} (i.e. 9tests in total). All of these tests should set show iterates to true and should allow a maximumof 15 iterations. How do the results change as you vary the initial guess? What happens as youvary the tolerance?

2. Steffensen’s Method:As you saw in project 1, we may approximate the derivative of a function using a finite difference,

f ′(x) ≈ δ−DPf(a) =f(x)− f(x− α)

α,

where α is an appropriate increment. This approach is often used to construct an approximationto Newton’s method that no longer requires the user to supply a function for f ′(x). While this

Page 2: Assignment approximate solver for non linear equations

obviously adds to the convenience of the method, a concern is whether the error in the derivativeapproximation,

f ′(a)− δ−DPf(a)

f ′(a)= c1α+

c2α

+O(α2),

could hinder the quadratic convergence of the method. The Steffensen method attempts tobypass this concern by choosing the perturbation so that it shrinks as the iterates approach thesolution, using the choice α = f(x), i.e.

f ′(x) ≈ f(x)− f (x− f(x))

f(x), (1)

which theoretically should result in a quadratically-convergent method.

Create a new C++ file named steffensen.cpp that uses the approximation (1) instead ofevaluating f ′(x) within Newton’s method. This solver function should have the calling syntax

double steffensen(double (*f)(const double, void *data),

double x, int maxit, double tol,

bool show_iterates, void *data);

where the arguments have the same definition as those in newton.cpp. This function shouldprovide the same iteration output as newton.cpp when show iterates is enabled. Create atest program named test steffensen.cpp to verify that your steffensen function works asdesired on the root-finding problem f(x) ≡ 1

5(x − 5)(x + 2)(x + 3) = 0 as above. You shoulduse the same tolerances, initial guesses and display of iteration results as before, but increasethe maximum number of iterations to 20. Do you notice any significant difference between thebehavior of the Steffensen method and Newton’s method? Explain your findings.

3. Application:In celestial mechanics, Kepler’s equation may be used to determine the position of an object inan elliptical orbit. This nonlinear equation is

ω − ε sin(ω) = t,

where

• ε =√

1− b2

a2is the object’s orbital eccentricity,

• t is proportional to time, and

• ω is the angle of the object around its elliptical orbit.

In this problem, we will use the parameters a = 1.0 and b = 0.5 to define the orbit for ourobject. We wish to solve this equation to find the angle ω at various moments in time.

First, determine a nonlinear root-finding residual function f(ω) that you can use to solve thisequation for ω; also determine its derivative function f ′(ω).

Create a C++ file named kepler.cpp, in which you implement your function, its derivative,and a main() routine that performs the following tasks. For each time t = {0, 0.001, . . . , 9}:

Page 3: Assignment approximate solver for non linear equations

(a) Use Newton’s method to solve Kepler’s equation for ω(t). Each solve should use a toler-ance of 10−4, a maximum of 5 Newton iterations, and should disable output of iterationinformation to the screen (you’re solving thousands of nonlinear problems, after all). Theinitial guess for each solve should be the solution from the previous value of t (start thefirst solve off with an initial guess of 0).

NOTE: your code should not use global variables; instead you should use the void *data

argument to pass any needed auxiliary data between your main() routine and your residualfunction.

(b) Using the formula for the radial position of an object at angle ω in it’s elliptical orbit,

r(ω) =ab√

(b cos(ω))2 + (a sin(ω))2

compute the Cartesian coordinates of the object, x(t) = r(t) cos(ω(t)) and y(t) = r(t) sin(ω(t)).

(c) Store the values of t, x(t) and y(t) in Mat objects.

Write these three arrays to the files t.txt, x.txt and y.txt.

Load these data arrays into the IPython notebook proj2.ipynb or Python script proj2.py,and create three plots: x(t) vs t, y(t) vs t, and y(t) vs x(t). If using a Python script instead ofa notebook, save these plots to the files x vs t.png, y vs t.png and y vs x.png.

What to turn in:Everything should be turned in on Blackboard, in a single “.zip” or “.tgz” file containing all ofthe required items listed below.

Turn in all of the requested C++ functions and IPython notebooks (or Python scripts) asseparate “.cpp” and “.ipynb” or “.py” files. Include a Makefile so that all of your executablescan be built from within the same directory with only the “make” command.

You will also discuss your project in a technical report. In this report you should:

• Use complete sentences and paragraphs.

• Explain the problems that were solved, and the mathematical approaches used on them.

• Describe your codes, including a discussion on any unique decisions that you had to make.

• Discuss all of your computed results. In this portion, you should include your plots, andyou should cut/paste any output printed to the screen into the report.

• In your own words, explain why you found the results that you did, justifying them math-ematically if applicable.

• Include all of your C++ and Python code as attachments inside the report file (or includedinline).

This report should be in “.pdf” format.