Homework/Programming 1. (Due Jan. 29)

advertisement
Homework/Programming 1. (Due Jan. 29)
Math. 417
This homework involves developing simple matlab functions for implementing the bisection and fixed point iteration for computing the roots of a fourth
order polynomial. Specifically, we consider the polynomial
(0.1)
P (x) = 48x4 + 68x3 − 28x2 − 27x + 9.
This polynomial can be implemented in MATLAB and passed to the iteration function as an inline or m-file function:
inline(’9+x*(-27+x*(-28+x*(68+x*48)))’)
The above form of the polynomial P (x) is more efficient than the natural
presentation in (0.1) because it contains only 4 multiplications while (0.1)
contains at least 7 (it takes 3 to compute x2 , x3 and x4 ).
To understand what is going on, it is useful to plot the functions. A simple
MATLAB m-file function plotting a function is given by the file pltf.m. Its
arguments are (a, b, n, f, nplot) where
(i) a < b are the limits of x.
(ii) n is the number of plotting points (typically, a few hundred).
(iii) f is the name of a matlab m-function or an inline function used to
evaluate f (x).
(iv) nplot is an integer plot number, 1,2,... This allows the simultaneous
display of more than one plot (if you use the same number for two
plots, the second overwrites the first).
Problem 1. Write a matlab m-file which implements the fixed point iteration
for a zero of f :
xi+1 = xi − αf (xi ).
The first line of your m-file (called fixed.m) should be:
function [r]=fixed(n,x,al,f)
Here n is the number of iterations, x is the initial iterate, al := α and f is
the name of the function which evaluates f . You should reuse the variable x
so that x = xi after the completion of the i’th step of the iteration. At each
step of the iteration, print the iteration number and the current value of x.
1
2
Use the function plotting routine to plot P (x) and P ′ (x) for x ∈ [−2, 1].
These plots will help you understand what is happening in subsequent fixed
point iterations.
From the class discussion, a necessary condition for the fixed point iteration
to converge to a zero at ζ is that the iteration parameter has the same sign
as P ′ (ζ) (actually α = 1/P ′ (ζ) is a good choice near ζ). From the plot
of P , there is a root around x = −1.5 where P ′ (ζ) looks to be around -200.
Note, however, we shall start the iteration at -2 where the derivative is larger
in absolute value and adjust our iteration parameter accordingly. Make the
following runs:
fixed(30,-2,-1/800,...)
fixed(1000,-2,-1/800,...)
Build a table with headings n, x0 , α, xn to report your results. If you correctly
coded the fixed point iteration, the first line will be
30
−2
− 1/800
− 1.500778.
This suggests that −1.5 is indeed a zero and is confirmed by the output of
the second run.
Unfortunately, x0 and α must be carefully chosen as illustrated by the following two runs:
fixed(30,-2,-1,...)
fixed(30,-2,1,...)
In both cases, the iterations blow up, tending to either ∞ (Inf ) or −∞ (Inf ). In this case, report (Inf ) or (-Inf ) and the first iteration which attains
it.
Next run
fixed(30,-2,-1/100,...)
fixed(1000,-2,-1/100,...)
and add the corresponding results to your table. Note that in this case, the
iteration jumped across the -1.5 root and settled down to the other root which
shared a negative derivative.
To converge to roots with positive derivatives, we need to switch the sign of
α. Run the following 3 examples and report your results.
3
fixed(30,-2,1/100,...)
fixed(10,0,1/100,...)
fixed(1000,0,1/100,...)
The last two runs yield one of the roots with a positive derivative. The following three runs give the value of the remaining root with a positive derivative:
fixed(30,1,1/100,...)
fixed(20,1,1/200,...)
fixed(1000,1,1/200,...)
Again, although the first run appears to converge, it converges to the other
root with positive derivative. Making the iteration parameter smaller keeps
the iteration from jumping out of the desired region. Add these runs to your
table.
Problem 2. Write a matlab function (called bisection.m) implementing the
bisection method for solving f (x) = 0. You should have developed a preliminary bisection routine in recitation and a more general version (pseudo
algorithm) is given in the textbook. Its first line should be:
function [r]=bisection(n,a,b,f)
Here n is the number of iterations, [a, b] is the initial interval, and f is the
name of a matlab function which evaluates the function f (x). As above, f
can be provided by a inline function or matlab m-file.
With good graphical information, it is easy to use the bisection method, i.e.,
find interval limits with function values having opposite signs and bracketing
the roots. Run the bisection method over the intervals [-2,-1], [-1,0], [0,.4],
[.4,1]. Report the results of your iteration (an + bn )/2 for n = 20 for each
interval.
Hand in the results for the two above problems and a copy of
the matlab codes fixed.m and bisection.m implementing the iterations.
Download