









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Numerical Computing S’10 MATH/CSCI 4800- Homework-2 Solution Outline
Due on Wednesday February 24, 2010
x − 2. Apply two steps of the bisection method, by hand, to find an approximation to the zero within an error of 1/ 8. (b) What is meant by the statement “ x approximates x∗^ to 4 decimal place accuracy”? (c) For the problem in part (a) above, estimate the number of bisection steps required to find the zero to within 4 decimal place accuracy.
(a) With f (x) = 2 ln x +
x − 2 , a simple check shows that f (1) = − 1 < 0 and f (2) = 0. 8005 > 0. Thus there is a root in the bracket [a 1 , b 1 ], where a 1 = 1, b 1 = 2. i. Step 1. The midpoint of the bracket is 3/ 2 , and f (3/2) = 0. 0357 > 0. So the new bracket is a 2 = 1, b 2 = 3/ 2. ii. Step 2. The midpoint of the bracket is 5/ 4 , and f (5/4) = − 0. 436 < 0. So the new bracket is a 2 = 5/ 4 , b 2 = 3/ 2. The corresponding approximation to the root is (5/4 + 3/2)/2 = 11/ 8. The maximum error at this stage is (− 5 /4 + 3/2)/2 = 1/ 8. (b) It is said that x approximates x∗^ to 4 decimal place accuracy if t = 4 is the largest integer for which the following is true: |x − x∗| |x∗|
10 −t.
(c) For an initial bracket of width unity, the error bound after n steps of bisection is given by
|x − x∗| ≤
2 n+^
For the current problem then, the requirement of 4 decimal place accuracy leads to
|x − x∗| |x∗|
|x∗| 2 n+^
The last two members of the above inequality imply the following constraint on n :
n ≥
ln
104 |x∗|
ln 2
Upon substituting the approximate value x∗^ ≈ 11 /8 found in part (a) above, the above relation yields n ≥ 12. 8. Thus, n = 13 should suffice.
(a) Consider f (x) = tan(πx) − x − 6. Graph f in the interval I : [0. 38 , 0 .48] and note that it has a zero in this interval. Use a bisection Matlab code to find the zero of f (x) in the interval I to within an error tolerance of 5 × 10 −^6. (Use a code of your own making, or the one available in the Text on page 28, or the one provided in class and available on the course home page.) How many function evaluations does bisection need to achieve this accuracy?
(b) Repeat part (a) above with your code altsection.m to find the zero to within the same error tolerance. Indicate how many function evaluations were needed in this case, and draw an inference about which of the two approaches (bisection or altsection) was more efficient. (c) Repeat parts (a) and (b) above for f (x) = x^3 + 2x^2 − 3 x − 1 , for the zero of f in the interval I : [− 3 , −2]. What do you conclude now about the relative efficiencies of the two algorithms. What general conclusion do you draw from this exercise?
0
Figure 1: An alternate strategy for root finding.
(a) The graph is shown in Figure 2.
0.38 0.4 0.42 0.44 0.46 0.
−
−
0
2
4
6
8
10
x
Figure 2: Graph of f (x) = tan(πx) − x − 6.
Here the width of the initial bracket is 0.1. Therefore the error after n steps of bisection will be within the specified tolerance if
pause; % The pause command causes the execution of this routine to % be paused at the end of each iteration. Press any key % on the keyboard to continue. end
% print results
format long e fprintf(’\n\n Computed Solution = %e \n\n’,xb); fprintf(’ Iteration count = %i \n\n’,it); %%%%%%%%%%%%%%%%%%%%%%% % function file f.m function y = f(x) y = tan(pi*x)-x-6; %
n = 1 Solution = 4.550000000000000e-01 error = 2.499999999999999e-
n = 2 Solution = 4.425000000000000e-01 error = 1.249999999999998e-
n = 3 Solution = 4.487500000000000e-01 error = 6.249999999999978e-
n = 4 Solution = 4.518750000000000e-01 error = 3.124999999999989e-
n = 5 Solution = 4.503125000000000e-01 error = 1.562499999999994e-
n = 6 Solution = 4.510937500000000e-01 error = 7.812499999999833e-
n = 7 Solution = 4.507031250000000e-01 error = 3.906249999999778e-
n = 8 Solution = 4.508984375000000e-01 error = 1.953124999999889e-
n = 9 Solution = 4.509960937499999e-01 error = 9.765624999999445e-
n = 10 Solution = 4.510449218749999e-01 error = 4.882812500001110e-
n = 11 Solution = 4.510693359375000e-01 error = 2.441406250000555e-
n = 12 Solution = 4.510571289062499e-01 error = 1.220703125001665e-
n = 13 Solution = 4.510510253906249e-01 error = 6.103515624994449e-
n = 14 Solution = 4.510479736328125e-01 error = 3.051757812483347e-
(b) The code altsection.m is listed below, along with the numerical output. It is invoked by the command
altsection(’f’,0.38,0.48,5e-6)
in the command window. The error tolerance of 5× 10 −^6 is now applied to the difference between successive iterates. We note that 18 iterations are now required, compared to 14 for bisection. Thus, even though it appears that the strategy behind altsection should make it more efficient than bisection, it turns out that altsection is slightly less efficient than bisection for the example at hand.
function xb=altsection(f,a,b,tol) % Matlab function altsection.m % Solves f(x) = 0 using the alternate method suggested in HW2, problem 2 % % Inputs: % a = left endpoint of interval % b = right endpoint of interval % tol = tolerance for stopping % % Required m-file: % f.m is m-file for function f(x)
% Outputs: % Root by altsection % % it=0; % set iteration count to zero fa=feval(f,a); fb=feval(f,b); xb=a+0.5(b-a); % initial estimate of root % % check bracketing % if sign(fa)==sign(fb), error(’Root not in bracket’); end % % begin while loop % while it<20 % limit the maximum number of iterations to 20 it=it+1; c=a-(b-a)fa/(fb-fa); % next iterate fc=feval(f,c); if sign(fa)~=sign(fc); % root lies in [a,c] b=c; fb=fc; else % root lies in [c,b] a=c; fa=fc; end
% print progress
err=abs(xb-c); % error is the difference between successive iterates xb=c; % rename current iterate % fprintf(’\n n = %i solution = %20.15e error = %20.15e \n’,it,c, err); pause; % The pause command causes the execution of this routine to % be paused at the end of each iteration. Press any key % on the keyboard to continue. end %%%%%%%%%%%%%%%%%%%%%%% % function file f.m function y = f(x) y = tan(pi*x)-x-6;
−3 −2.8 −2.6 −2.4 −2.2 −
−
0
1
2
3
4
5
x
Figure 3: Graph of f (x) = x^3 + 2x^2 − 3 x − 1.
n = 5 Solution = -2.921875000000000e+00 error = 1.562500000000000e-
n = 6 Solution = -2.914062500000000e+00 error = 7.812500000000000e-
n = 7 Solution = -2.910156250000000e+00 error = 3.906250000000000e-
n = 8 Solution = -2.912109375000000e+00 error = 1.953125000000000e-
n = 9 Solution = -2.913085937500000e+00 error = 9.765625000000000e-
n = 10 Solution = -2.912597656250000e+00 error = 4.882812500000000e-
n = 11 Solution = -2.912353515625000e+00 error = 2.441406250000000e-
n = 12 Solution = -2.912231445312500e+00 error = 1.220703125000000e-
n = 13 Solution = -2.912170410156250e+00 error = 6.103515625000000e-
n = 14 Solution = -2.912200927734375e+00 error = 3.051757812500000e-
n = 15 Solution = -2.912216186523438e+00 error = 1.525878906250000e-
n = 16 Solution = -2.912223815917969e+00 error = 7.629394531250000e-
n = 17 Solution = -2.912227630615234e+00 error = 3.814697265625000e-
% % Results for altsection % n = 1 solution = -2.833333333333333e+00 error = 3.333333333333335e-
n = 2 solution = -2.907928388746803e+00 error = 7.459505541346978e-
n = 3 solution = -2.912002629312925e+00 error = 4.074240566121823e-
n = 4 solution = -2.912217266736004e+00 error = 2.146374230793668e-
n = 5 solution = -2.912228552236375e+00 error = 1.128550037066134e-
n = 6 solution = -2.912229145560214e+00 error = 5.933238389488338e-
(b) If an iterative method squares the error every two iterations, what is its convergence rate? (c) Which of the following behaviors are possible in using Newton’s method for solving a nonlinear equation? i. It may converge linearly. ii. It may converge quadratically. iii. It may not converge at all.
(a) Let the iterative method produce a sequence of approximations {xk} to the exact quantity α. Then the sequence is said to converge quadratically if the following holds:
lim n→∞
|xn+1 − α| |xn − α|^2
where C > 0 is a constant. (b) The convergence rate is
(c) All three behaviors are possible.
(a) In solving f (x) = 0, if the cost of evaluating f (x) and its derivative f ′(x) is about the same, how does the cost of Newton’s method compare to the cost of the secant method per iteration? (b) Consider the polynomial f (x) = x^5 − 300 x^2 − 126 x + 5005 , which has a zero α = 5. Consider the perturbed function f(x) = (1 + )x^5 − 300 x^2 − 126 x + 5005 where is a small number. Examine the perturbed problem to determine if finding the zero α = 5 of f (x) is a well-conditioned or an ill-conditioned problem.
(a) The Newton and secant algorithms are, respectively,
xn+1 = xn − f (xn) f ′(xn)
xn+1 = xn − f (xn)
xn − xn− 1 f (xn) − f (xn− 1 )
While Newton requires two function evaluations per iteration ( f (xn) and f ′(xn) ), secant requires only f (xn), since f (xn− 1 ) is available from the preceding step. (b) The perturbed function can be written as f(x) = f (x) + g(x), where g(x) = x^5. Now, if the perturbed root is 5 + h, then f (5 + h) + g(5 + h) = 0. Expanding the left-hand side in Taylor series,
f (5) + hf ′(5) + · · · + [g(5) + · · · ] = 0.
x2=x1-f1((x1-x0)/(f1-f0)); fprintf(’k =%2.0i, x =%23.15e\n’,k,x2); k=k+1; x0=x1;x1=x2; end disp(’Secant Method, Version 1, with eps’) disp(’~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~’) k=1; x0=a0;x1=a1; while k< f0=feval(f,x0); f1=feval(f,x1); x2=x1-f1((x1-x0)/(f1-f0+2eps)); fprintf(’k =%2.0i, x =%23.15e\n’,k,x2); k=k+1; x0=x1;x1=x2; end disp(’Secant Method, Version 2, without eps’) disp(’~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~’) k=1; x0=a0;x1=a1; while k< f0=feval(f,x0); f1=feval(f,x1); x2=(f1x0-f0x1)/(f1-f0); fprintf(’k =%2.0i, x =%23.15e\n’,k,x2); k=k+1; x0=x1;x1=x2; end disp(’Secant Method, Version 2, with eps’) disp(’~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~’) k=1; x0=a0;x1=a1; while k< f0=feval(f,x0); f1=feval(f,x1); x2=(f1x0-f0x1)/(f1-f0+2eps); fprintf(’k =%2.0i, x =%23.15e\n’,k,x2); k=k+1; x0=x1;x1=x2; end %%%%%%%%%%%%%%%%% % function file secant_f.m function y=secant_f(x) y=x-x^(1/3)-2; %%%%%%%%%%%%%%%%%
NUMERICAL OUTPUT
I. Secant Method, Version 1, without eps
k = 1, x = 3.529761633735423e+ k = 2, x = 3.521438667808709e+ k = 3, x = 3.521379714663710e+ k = 4, x = 3.521379706804575e+ k = 5, x = 3.521379706804568e+ k = 6, x = 3.521379706804568e+ k = 7, x = NaN k = 8, x = NaN k = 9, x = NaN k =10, x = NaN II. Secant Method, Version 1, with eps ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ k = 1, x = 3.529761633735423e+ k = 2, x = 3.521438667808709e+ k = 3, x = 3.521379714663710e+ k = 4, x = 3.521379706804575e+ k = 5, x = 3.521379706804568e+ k = 6, x = 3.521379706804568e+ k = 7, x = 3.521379706804568e+ k = 8, x = 3.521379706804568e+ k = 9, x = 3.521379706804568e+ k =10, x = 3.521379706804568e+ III. Secant Method, Version 2, without eps k = 1, x = 3.529761633735423e+ k = 2, x = 3.521537671956202e+ k = 3, x = 3.521379727859997e+ k = 4, x = 3.521379706804620e+ k = 5, x = 3.521379706804567e+ k = 6, x = 3.521379706804567e+ k = 7, x = 3.521379706804568e+ k = 8, x = 3.521379706804568e+ k = 9, x = NaN k =10, x = NaN
IV. Secant Method, Version 2, with eps
k = 1, x = 3.529761633735425e+ k = 2, x = 3.521438667808713e+ k = 3, x = 3.521379714663930e+ k = 4, x = 3.521379706835563e+ k = 5, x = 3.521379940174600e+ k = 6, x = 3.521379698975161e+ k = 7, x = 3.521379714378822e+ k = 8, x = 3.521379588202504e+ k = 9, x = 3.521379721283561e+ k =10, x = 3.521379693076798e+ Both equations (5.1) and (5.2), corresponding to versions 1 and 2 respectively, are susceptible to catas- trophic cancellation due to roundoff errors in the computation of f (xn) − f (xn− 1 ). This is confirmed by the appearance of NaNs in Tables I and III above. In (5.1) the error due to cancellation will not have a significant impact on convergence. To see this, note that equation (5.1) may be written as xn+1 = xn + h, where h = f (xn) xn − xn− 1 f (xn) − f (xn− 1 ) The fixed-point iteration g 3 (x) = x yields 6 + 2x^1 /^3 3 − x−^2 /^3 = x which can be manipulated to yield f (x) = x − x^1 /^3 − 2 = 0. (c) The results appear below, along with the Matlab code written as a script M-file. See comments following the results. % Script fixed.m % Carries out the fixed-point iteration % % Input: g = function being iterated % % In the calling program, use a command such as % g=inline(’x^3-1’); % to supply the input function g % Otherwise, supply the function g in this script % % r = initial guess % % Output: plot of iteration % r = the fixed point if it exists % clc maxit=8; % max iterations allowed r0=3; % initial guess g=inline(’x.^(1/3)+2’); % g=g_ r = r0; % initial guess k = 0; % initialize iteration counter disp(’ g=g1’); while k< maxit k=k+1; r1=g(r); r=r1; fprintf(’\n k = %i r = %10.5e \n’,k,r); end % k = 0; % initialize iteration counter g=inline(’(x-2).^3’); %g=g_ r = r0; % initial guess disp(’ g=g2’); while k< maxit k=k+1; r1=g(r); r=r1; fprintf(’\n k = %i r = %10.5e \n’,k,r); end % k = 0; % initialize iteration counter r = r0; % initial guess g=inline(’(6+2*x.^(1/3))./(3-x.^(-2/3))’); %g=g_ disp(’ g=g3’); while k< maxit k=k+1; ## r1=g(r); r=r1; fprintf(’\n k = %i r = %10.5e \n’,k,r); - g=g end - k = 1 r = 3.44225e+ - k = 2 r = 3.50990e+ - k = 3 r = 3.51972e+ - k = 4 r = 3.52114e+ - k = 5 r = 3.52135e+ - k = 6 r = 3.52137e+ - k = 7 r = 3.52138e+ - k = 8 r = 3.52138e+ - g=g - k = 1 r = 1.00000e+ - k = 2 r = -1.00000e+ - k = 3 r = -2.70000e+ - k = 4 r = -2.43890e+ - k = 5 r = -1.45107e+ - k = 6 r = -3.05539e+ - k = 7 r = -2.85233e+ - g=g k = 8 r = -Inf - k = 1 r = 3.52664e+ - k = 2 r = 3.52138e+ - k = 3 r = 3.52138e+ - k = 4 r = 3.52138e+ - k = 5 r = 3.52138e+ (a) The given data are as follows. T 0 = 40, T 1 = 0, T = 32, k = 5. 4 × 10 −^6 ft^2 /s, t = 30 × 24 × 3600s. Here we have converted time into seconds, to be consistent with the units of k. The unknown x is then the root of the following equation G(x) = T (x, t) − T 1 T 0 − T 1 − erf x 2 kt To apply Newton’s method we also need the derivative G′(x) = − πkt e−x (^2) /(4kt) . The graph in Figure 6 shows the root to be approximately x = 7. We use this as the starting ## x ## G(x) Figure 6: Graph of G(x) = T^ ( Tx,t 0 −)T− 1 T 1 − erf x 2 √ kt guess for Newton’s method, and use a tolerance of 0.001 to find the root. The Matlab code in the form of a function M-file Newton.m and a separate M-file G.m for the function G are given below. The code is invoked by the command >> Newton(’G’,7,0.001) The numerical output is also given below. The desired depth for pipe burial is found to be no shallower than 6.78 feet. >> Newton(’G’,7,0.001) function r = Newton(fun,x0,tol) % % Input: fun = (string) name of mfile that returns fun(x) and fun’(x) % x0 = initial guess % tol = tolerance for stopping % % Output: r = the root x = x0; % initial guess k = 0; % initialize iteration counter maxit=10; % max iterations allowed while k<= maxit k=k+1; [f,fp] = feval(fun,x); dx = f/fp; x = x-dx; err = abs(dx); fprintf(’\n k = %i Solution = %10.5e Error = %15.10e \n’,k,x,err); if (err < tol), r=x; return; end end warning(sprintf(’root not found within tolerance after %d iterations\n’,k)); %%%%%%%%%%%%%%%%%%%%% % file G.m % Specifies G and Gprime function [g,gp] = G(x) % provide data T0=40; T1=0; T=32; k=5.4e-6; t=30*24*3600; % g=(T-T1)/(T0-T1)-erf(x/(2*sqrt(k*t))); z1=pi*k*t; z2=x*x/(4*k*t); gp=-exp(-z2)/sqrt(z1); %%%%%%%%%%%%%% k = 1 Solution = 6.77449e+00 Error = 2.2550839478e- k = 2 Solution = 6.78055e+00 Error = 6.0623576954e- k = 3 Solution = 6.78056e+00 Error = 4.4527222282e- ans = 6. ## days ## Temperature, degrees F Figure 7: Graph of T (12, t). (b) The graph in Figure 7 is the plot of T (12, t) = T 1 + (T 0 − T 1 )erf kt