You are on page 1of 4

Lab 3, Multidimensional Newtons method and more on solving linear systems

Topics
Mathematics: multidimensional Newtons method; estimating errors when solving linear systems; condition number; numerical calculation of the Jacobian. MATLAB: using MATLABs backslash operator to solve linear systems; generalising an M-le for singlevariable Newtons method to multidimensional Newtons method; testing for convergence of a sequence of vectors.

MATLABs built-in command for solving linear systems


MATLAB has a built-in function that will solve Ax = b using the backslash (\) operator. The command to do this is: >> x=A\b Mathematically, this is equivalent to x = A1 b (although MATLAB doesnt actually work out the inverse matrix A1 as this can take a long time). Checkpoint: Use the backslash operator to solve one of the systems you studied in lab 3. Check that this gives you the same answer as your Gaussian elimination program. From now on, well use MATLABs built-in backslash command, rather than our own M-le, because it automatically does partial pivoting and can solve a linear system of any size.

Multidimensional Newtons method


We want to nd a solution to the following nonlinear system of equations x2 x2 = = ln x1 , 1 2 (x + 2x1 24). 25 1

First, rearrange this system into the form f (x) = 0 on paper, and write down the Jacobian matrix J associated with the system. Now look at your Newtons method function M-le mynewt2.m from lab 2. You should have a version that tests for convergence to some tolerance tol which begins function results = mynewt2(x, niter, tol) where x is the initial approximation to the solution, niter is the maxium number of iterations to carry out and tol is the desired tolerance. You are going to use this function M-le as a template for doing multidimensional Newtons method. Rename the M-le mymultinewt.m. Now, modify the M-le so that it solves a system of equations (the system above), rather than just a single equation. Remember, the key dierence is that the variables x and f are now vectors and, instead of the derivative df /dx, we now have a Jacobian matrix J . The parts of the M-le that you need to modify are: 9

f should now be dened as the vector function you are trying to nd the zeros of: f = [f1, f2]. Instead of dfdx=..., you now need to dene J to be the matrix you wrote down earlier. The change x in x at each iteration of Newtons method is the solution of the linear system J x = f . Use MATLABs backslash operator to solve for deltax. Then update the variable x by adding deltax. Your M-le should now do the correct multidimensional Newtons method iterations. However, you still need to modify the convergence test to account for the fact that you are using vectors instead of scalars. So, instead of using the absolute value (abs), you now need to use the innity norm (norm) to test whether x(k+1) x(k) x(k+1)

< tol,

where x(k) is the value of the vector x after k iterations. Note that, by denition x = x(k+1) x(k) . Type help norm (or refer to the MATLAB guide in your course reader) to see how to use the norm command. Like your original Newtons method M-le, your new M-le should return all the iterations in a matrix called results. The k th row of results should contain the value of the vectors x and f after k iterations. Hopefully, the nal row will contain the solution for x. Checkpoint: Use your M-le to nd the solution(s) to the nonlinear system (there may be more than one!). Hint: Use a graph to nd suitable initial approximation(s) to ther solution(s). Check that your answer seems reasonable.

Errors when solving linear systems


To assess the reliability of any equation solver, you need a way of checking the accuracy of a supposed solution x1 to a system Ax = b. If we write x for the exact solution, then what wed really like to know is the size of: x x1 x x1 / x (the absolute error), (the relative error).

or

In real life, of course, we do not know what x is, so we cannot calculate these numbers. However, we can use this strategy if we test the equation solver on equations with known solutions. Such equations are easily constructed. Start with your favourite non-singular 2 2 matrix A and vector x and dene b by b = Ax. Now pretend you dont know what x is. Use the backslash operation to nd MATLABs solution for x using A and b. Hint: Store the calculated solution in x1 so you dont overwrite x. Calculate the absolute and relative errors compared to the exact solution x (remember to use the innity norm). Are they zero? What does this tell you? Another way to check the accuracy of a supposed solution is to see how well x1 ts the given system. Thus we calculate b1 = Ax1 and see if b1 is close to b. The dierence b b1 is called the residual vector. So we might hope that if the residual vector is small, then x1 is a good approximation to the true solution x. We measure the size of the residual vector by looking at the one of the following quantities: b b1 b b1 / b (the absolute residual), (the relative residual).

or

Calculate the absolute and relative residuals. Are they zero? You will use both these checking methods in the following exercise. Write an M-le myerrors.m that calculates MATLABs solution (using the backslash operation) and works out the relative error and relative residual for a given matrix A and known solution vector x.

10

Hilbert matrices
Hilbert matrices arise if you try to nd least squares polynomial ts without using orthogonal polynomials. MATLAB has a built-in function which sets up n n Hilbert matrices. Type help hilb to see how to get a Hilbert matrix. Dene A to be the 5 5 Hilbert matrix, and x to be a 5 1 vector of 1s. As above, dene b by the equation b = Ax . Run myerrors.m to nd the relative error and relative residual. MATLAB has a built-in function cond which gives the condition number of a matrix. What is the condition number of the 5 5 Hilbert matrix? Lets investigate how the errors, residuals and condition number behave for dierent sized Hilbert matrices. Modify myerrors.m so that it calculates the relative error, the relative residual and the condition number for n = 2, 3, . . . , 15. You do not need to print out any other details of the solving process. Your M-le should store the results for each n as a row of numbers results(K,:)=... (as you did in Lab 2), which should contain the number n, the relative error, relative residual and condition number. This builds up a matix of results. Once all the results have been calculated, your M-le should display the results in table form using the fprintf command: fprintf(%3d %12.3e %12.3e %12.3e\n,results) This command sets aside 3 spaces (%3d) for the rst entry (which should be the number n) of each row. It then displays the next three entries using the exponential format with 3 decimal places (%12.3e). \n moves the output to a new line. For more on the fprintf command, refer to section 4 of the MATLAB reference guide in the course reader. Checkpoint: What is the rst value of n for which the calculated solution is no longer accurate to 4 signicant gures? Are the residual vectors a good guide to the accuracy of the calculated solution? How large does n have to be before MATLAB warns you that the solutions may be inaccurate? Is the condition number a good guide to the accuracy of the calculated solution?

Practice test question multidimensional Newtons method


Obviously, you dont always want to solve the same nonlinear system. You could adapt your M-le mymultinewt.m to solve any nonlinear system by using a function M-le to calculate the vector f. The tricky part is calculating the Jacobian. Luckily, this can be done numerically. The formula for calculating the partial derivative fi /xj numerically is really the same as the formula for ordinary derivatives (which you used at the end of lab 2): fi (x + hej ) fi (x) fi , xj h where ej is a vector of zeros, except for the j th entry, which is 1. Checkpoint: Use your M-le to nd the solution to the system x2 1 x1 x2 10 x2 +
2 3x1 y2

= = =

0 0 0

57

x1 + x2 2x3

using an initial approximation of x = (1.5, 3.5, 2.5). Check that your answer gives a small residual.

11

12

You might also like