Sie sind auf Seite 1von 6

Lab 1, Fixed point iterations

Topics
Mathematics: convergence and divergence of xed point iterations; graphical interpretation of convergence criterion. Newtons method; using a numerical derivative in Newtons method. MATLAB: Review of EMTH/MATH 171: using commands like x=g(x) to update variables; for...end loops; M-les; verifying solutions. Function M-les; improved format for output; decisions using if...end; testing for convergence. Checkpoints indicate tasks or questions which your tutor may ask you about.

Iterations in interactive mode


In this rst part of the lab, we shall look at Exercise 6.1 from page 157 of Chapra and Canale (5th edition). The aim is to solve the equation using iterations of the form xk+1 = g (xk ). One way of nding a suitable function g (x) is to rearrange the given equation to the form x = g (x). Thus the solution x is a xed point for the function g (x). In the rst part of this lab you will use the function g (x) = 2 sin( x) with the initial guess at a solution as x0 = 0.5 One way of calculating the next few iterations is to follow the mathematical notation xk+1 = g (xk ) >> >> >> >> x0 x1 x2 x3 = = = = 0.5 2*sin(sqrt(x0)) ???? ???? 2 sin( x) x = 0 (1)

and so on. (You dont need to type each line in full - just recall the previous command using the up-arrow key and then change the x0 to x1 and so on.) Obviously this is a little tedious! To nd a better way of doing the calculations we need to realize that for MATLAB xk+1 = g (xk ) is a command, not an equation. For MATLAB this command says, Plug xk into the function g , and then store the result in the variable xk+1 . So we do not need to keep introducing new variables x0, x1, x2, x3 and so on. We can use just one variable to store the latest approximation: 1

>> x = 0.5 >> x = 2*sin(sqrt(x)) >> x = 2*sin(sqrt(x)) and for each new iteration you just need to use the up-arrow key to recall the previous command. Checkpoint Do this until successive x-values agree to four decimal places.

Graphical interpretation
We can see why the above iterations converge if we look at the graph of the function g (x). The xed point we are looking for occurs where this graph cuts the line y = x, so we need to plot y = g (x) and y = x on the same graph. The following commands set up a range of x-values covering the interval [0, 4], store the corresponding g (x) values in the variable y, and then plot y = x as a dashed blue line (b--), and y = g (x) as a solid red line (r-). >> >> >> >> x = 0: 0.01: 4; y = 2*sin(sqrt(x)); plot(x, x, b--, x, y, r-) title(Plot for fixed point iterations.)

Try the command >> help plot to see how such plot commands are put together. Checkpoint What would you expect to happen if you started the iterations with x0 = 3.5 instead? (Use the graph! Check your answer by doing a few iterations.)

Automated iterations: for...end loops


If you know how many iterations you want to perform, you can use a for...end loop to carry out all the calculations at once. The following commands should produce the same output as your earlier iterations. >> x = 0.5 >> for k = 1:10, x = 2*sin(sqrt(x)), end Caution: This is a very easy way to generate lots of output very quickly. If you want to suppress the output from such a loop, then use semicolons at the end of each command in the loop. If you need to stop such a loop before it ends then use the key combination Ctrl+C in the command window. Checkpoint: Use a for...end loop to carry out 7 steps of the iteration xk+1 = 2 sin( xk ) using the starting value x0 = 0.01. What happens? Why?

Iterations in programming mode


Most useful loops are longer than the above example, and it is usually more convenient to write and save them as programs. MATLAB programs are called M-les. These are lists of MATLAB commands stored in les with names like name.m and you execute the commands by entering the command >> name 2

Use the MATLAB editor (found via the File New M-le menu, or via the New M-le button on the Toolbar) to write an M-le called mylab1.m that contains the commands you used for the previous Checkpoint. You can see the contents of the M-le using the type command in MATLAB: >> type mylab1 and you can run the program by entering the lename as a command >> mylab1

Another iteration formula


Find a dierent rearrangement of the original equation 2 sin( x) x = 0 into the form x = g (x). Write a new M-le to do 10 iterations using this g (x), starting with x0 = 0.5. Plot y = g (x) and y = x on the same graph (make sure you are plotting the function g (x) over an appropriate range of x values). Checkpoint: What solutions have you found to equation (1)? How can you check that they really are solutions? Why did the second rearrangement you used converge to a dierent value to the rst one?

Newtons Method
We begin by looking at Chapra and Canales Exercise 6.4 which asks us to solve the equation 1 + 5.5x 4x2 + 0.5x3 = 0 both graphically and using Newtons method.

Finding initial approximations


Begin by solving the equation graphically: use the plot command to help you nd approximations to all the solutions to this equation. Your approximations do not need to be very accurate, as you are going to use Newtons method to improve the accuracy.

Building a program for Newtons method


The given equation is in the form f (x) = 0. Newtons method solves such equations by using iterations of the form f (xk ) . xk+1 = xk f ( xk ) Use the following skeleton to write an M-le called mynewt1.m which will carry out Newtons method to nd one of the solutions to the above equation. 3

x = ???? for k=1:??? f=??? dfdx=??? x=??? end

% % % % %

the initial approximation to the solution the number of iterations to be performed an expression for f(x) an expression for the derivative of f(x) the formula for a Newtons method iteration

(Remember that MATLAB ignores anything after the % sign, so you can use this to include comments.) Now run your program!

Structuring the output


Typically we would like to know: whether the iterations x are converging to a stable value, whether the function values f are getting close to zero, and maybe how many iterations have been performed. Chapra and Canale use tables to show this kind of information see Section 6.2, page 140, for example and tables are really just matrices. So we could collect the output into the rows of a matrix. We dont want to see the matrix until the end, so we use semicolons to suppress all the output until then. To do this, add the following line just before the end of the for loop in mynewt1.m: results(k,:) = [k, x, f]; This command stores the values of k, x and f in the kth row of results (which is represented by results(k,:)). Although MATLAB will now happily run your M-le, you REALLY should initialise the variable results at the beginning of the program (i.e. before the for loop), rather than just tacking an extra row on the end at each iteration. This will speed up the program enormously. The best way to do this is just declare results as a matrix of zeros. We know that the number of columns is 3, and the number of rows is equal to the number of iterations, say N: results = zeros(N,3); Run your program and then look at the table by typing >> results into the command line.

More exible M-les: function les


At the moment, if you want to try Newtons method with a dierent starting value or a dierent number of iterations, you have to edit the M-le mynewt1.m. MATLAB function les let us write more exible programs which will accept input values and produce output values just like a mathematical function sin(x) or ln(x). As MATLAB works with arrays, it is not too surprising that both the input and the output can be one or more arrays. Here is a skeleton of a simple function le mynewt2.m. function results = mynewt2(x, niter) % A simple function M-file using Newtons method % to solve Chapra and Canales exercise 6.4 % f(x) = -1 +5.5x-4x^2+0.5x^3 = 0 % 4

% Usage: results = mynewt2(x, niter) % % Input/output variables: % x = initial approximation to solution % niter = number of iterations to be performed % results = output matrix, k-th row records: % - number of iteration k, % - k-th approximation x, % - value of f at latest approximation. results = zeros(??, 3); for k=1:??? f=???; dfdx=???; x=???; results(k,:)=[k, x, f]; end Function les always begin with a line like function results = mynewt2(x, niter) which declares that the M-le is a function le, and indicates the usage in terms of the input and output variables. It is good practice to follow this by comments telling the user how the program works, and what each of the input/output variables represents. This information can then be obtained in the MATLAB command window (without opening an editor) by using MATLABs help: >> help mynewt2 Write a new M-le using the above skeleton as a starting point. Checkpoint: Change MATLABs display to long format by using the command >> format long Show the help for mynewt2.m, and then use the function to nd the rst 6 Newtons method iterations, using an initial approximation of x0 = 1.

Better control within the loop: if...end


Convergence sometimes happens before the set number of iterations, and it makes sense to stop the calculations as soon as we have the desired accuracy. We can do this by using an if...end block to test for convergence and then leave the loop if convergence is achieved. To test for convergence, it is customary to look at successive approximations xk+1 and xk and see if they are close enough. Thus if we had a tolerance of , we could test for |xk+1 xk | < |xk+1 | For Newtons method the dierence between successive approximations is f (xk )/f (xk ) so we just need to separate out this part of the calculation for later use. Open mynewt2.m in the MATLAB editor and make the following changes Make it a function of three variables: function results = mynewt2(x, niter, tol) where tol represents the desired tolerance ( ). Update the help part of the M-le. 5

Modify the commands within the for...end loop so that the function stops as soon as the criterion for convergence is satised. Hints: you will need to use an if...end block to test if the criteron is satised; you can use the return command to tell MATLAB to stop running the function and return the outputs immediately. Checkpoint: Check that the new version does indeed stop early if you ask it to perform 10 iterations with an initial value x0 = 1, and a tolerance of 104 . (MATLABs notation for 104 is 1e-4.)

Slow convergence
Make a copy of mynewt2.m and modify it to solve the equation x3 x 0.3849 = 0. Plot the graph and use it to nd suitable starting values. Then use your programs to produce tables showing iterations which converge to each of the three solutions. Is there a connection between the shape of the graph and the speed of convergence?

No convergence
Now modify mynewt2.m (or a copy) to solve the equation x3 x 0.385 = 0. Produce tables to show what happens if you carry out Newtons method starting with the three solutions to the previous equation. Checkpoint: How many solutions are there to each of the two equations above? What should you be looking for in the 3rd column of the results matrix? Can you explain the dierence in number of iterations required to nd the dierent solutions? Can you explain why, for the nal equation, Newtons method does not converge for certain starting values?

Practice test question


Sometimes you will need to solve an equation of the form f (x) = 0 where f is a complicated function that is not easy to dierentiate analytically, for example f ( x) = x2 cos(x2 ) exp(x3 ) . 3x + 2 + x ln(x)

In such cases, you can calculate a numerical approximation to the derivative f (x) for use in Newtons method. To do this, it is useful to write a separate function M-le for the function f . Write a function M-le myfunc.m that takes an input x and returns the value of f (x) as dened above. Now replace the line f=... in the for...end loop in your main M-le with a call to your new function: f=myfunc(x). Replace the line dfdx=... with a line that calculates a numerical derivative using the formula: f (x + h) f (x) . h (You will need to choose an appropriate value for h.) f (x) Checkpoint: Use your new M-le to nd the root of f (x) = 0 using a starting value of x = 1. 6

Das könnte Ihnen auch gefallen