Beruflich Dokumente
Kultur Dokumente
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.
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.)
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
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.
% % % % %
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!
% 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.
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?
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