Docsity

Study Guide for Assignment 2 | Numerical Computing | MATH 4800, Assignments of Mathematics

Toggle sidebar
bg1
Numerical Computing F’05 A. Kapila
Assignment 2
1. (a) Section 3.1, page 78, problem 11.
(b) Section 3.1, page 79, problem 14.
(a) A possible bracket is [1,2] since f(1) = sin 1 >0 while f(2) = −1 + sin 2 <0.The initial
approximation to the root, r0= (2 −1)/2=1/2,is accurate to within an error E0= 1/2.
At the end of nsteps the error is reduced to En= 1/2n+1 .This is less than 5 ×10−8for
n > ln(2 ×107)/ln 2 −1 = 23.25 . Thus n= 26 will do.
(b) There is no point in using a tolerance smaller than the accuracy of the machine. Let us recall
that the relative error in representing a number αby its float is bounded by machine epsilon
(assuming rounding by chopping), i.e.,
|α−float(α)|
α≤mach.
Then,
|α−float(α)| ≤ α mach.
We are given that 1 < α < 2.Therefore, from above,
mach <|α−float(α)|<2mach.
This suggests that the tolerance should not be smaller than the smallest value of |α−float(α)|,
i.e.,mach.Note that had the root been bracketed by 100 < α < 200,the smalletst tolerance
would have been 100mach.
2. Section 3.2, page 88, problem 6.
We are asked to use Newton’s method to compute the root of
f(x) = x+e−Bx2cos x= 0, B > 0.
We are given the bracket [−1,0] and are asked to use the following values of B: 1,5,10,25,50.We
are also asked to consider, among other choices, x0= 0 as the initial guess, and to expect interesting
behavior at larger values of B.
We begin by examining the behavior of ffor various B. We note that f(0) = 1 for any B. For
x6= 0,the exponential is increasingly small as Bincreases, and we expect f≈x, i.e., the graph of
fapproaches the straight line y=x. Now consider the derivative
f0(x) = 1 −e−Bx2sin x−2Bxe−B x2cos x.
For any B, f0(0) = 1.For x6= 0 and large B, f 0≈1 since f≈x. This behavior is captured in
the plots of fbelow.
Now consider Newton’s method with x0= 0.By geometric construction, the tangent to the graph
of fat x0has unit slope, and therefore intersects the x-axis at x=−1,yielding that as the next
iterate, i.e.,x1=−1.For large values of B, f(x1)≈1 and the tangent there has slope ≈1,yielding
x2= 0 as the next iterate to a good approximation. We are back to the starting guess and therefore
in a cycle that switches back and forth between the iterates -1 and 0, never converging. This difficulty
does not arise for small B , and does not arise for any Bif the starting guess is somewhat removed
from x0= 0.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Study Guide for Assignment 2 | Numerical Computing | MATH 4800 and more Assignments Mathematics in PDF only on Docsity!

Numerical Computing F’05 A. Kapila Assignment 2

  1. (a) Section 3.1, page 78, problem 11. (b) Section 3.1, page 79, problem 14.

(a) A possible bracket is [1, 2] since f (1) = sin 1 > 0 while f (2) = −1 + sin 2 < 0. The initial approximation to the root, r 0 = (2 − 1)/2 = 1/ 2 , is accurate to within an error E 0 = 1/ 2. At the end of n steps the error is reduced to En = 1/ 2 n+1. This is less than 5 × 10 −^8 for n > ln(2 × 107 )/ ln 2 − 1 = 23.25. Thus n = 26 will do. (b) There is no point in using a tolerance smaller than the accuracy of the machine. Let us recall that the relative error in representing a number α by its float is bounded by machine epsilon (assuming rounding by chopping), i.e.,

|α − float(α)| α

≤ mach.

Then, |α − float(α)| ≤ α mach. We are given that 1 < α < 2. Therefore, from above,

mach < |α − float(α)| < 2 mach.

This suggests that the tolerance should not be smaller than the smallest value of |α − float(α)|, i.e., mach. Note that had the root been bracketed by 100 < α < 200 , the smalletst tolerance would have been 100mach.

  1. Section 3.2, page 88, problem 6. We are asked to use Newton’s method to compute the root of

f (x) = x + e−Bx

2 cos x = 0, B > 0.

We are given the bracket [− 1 , 0] and are asked to use the following values of B : 1, 5 , 10 , 25 , 50. We are also asked to consider, among other choices, x 0 = 0 as the initial guess, and to expect interesting behavior at larger values of B. We begin by examining the behavior of f for various B. We note that f (0) = 1 for any B. For x 6 = 0, the exponential is increasingly small as B increases, and we expect f ≈ x, i.e., the graph of f approaches the straight line y = x. Now consider the derivative

f ′(x) = 1 − e−Bx

2 sin x − 2 Bxe−Bx

2 cos x.

For any B, f ′(0) = 1. For x 6 = 0 and large B, f ′^ ≈ 1 since f ≈ x. This behavior is captured in the plots of f below. Now consider Newton’s method with x 0 = 0. By geometric construction, the tangent to the graph of f at x 0 has unit slope, and therefore intersects the x -axis at x = − 1 , yielding that as the next iterate, i.e., x 1 = − 1. For large values of B, f (x 1 ) ≈ 1 and the tangent there has slope ≈ 1 , yielding x 2 = 0 as the next iterate to a good approximation. We are back to the starting guess and therefore in a cycle that switches back and forth between the iterates -1 and 0, never converging. This difficulty does not arise for small B, and does not arise for any B if the starting guess is somewhat removed from x 0 = 0.

−1−1 −0.9 −0.8 −0.7 −0.6 −0.5 −0.4 −0.3 −0.2 −0.1 0

−0.

−0.

−0.

−0.

0

1

B

x

y

A

C

We are asked to solve the problem as accurately as possible. Therefore we take the tolerance to be extremely small, at 10 times mach. Recalling that Matlab works in double precision, this should give us about 15 digit accuracy. (Note that the symbol eps in Matlab stands for mach. ) We write three Matlab routines that are attached. The first, a2prob2.m, is the main or calling program. It declares B as a global variable, so that the value of B is passed from the main program to all other subprograms in which the same global declaration is made. We specify the tolerance and the initial guess x 0 , and print out x 0. We then set up a for loop that cycles over all allowable values of B. For each B the value is printed and the subprogram Newton2.m is invoked to apply the Newton rootfinder. The resulting approximation to the root and the number of iterations needed is printed. The second routine, Newton2.m is defined as a function. The function requires a list of three inputs: the tolerance, the initial guess, and the function being examined. Again, B is declared as a global variable at the outset. A maximum number of iterations is defined. Here one uses a while loop to compute the Newton iterates, until either convergence is reached to the specified tolerance or the maximum number of iterations is exceeded. Every iterate is printed, as is a warning statement if convergence is not achieved. The third routine is also defined as a function. It specifies both f and f ′, as required by Newton’s method. Note the global declaration of B. It is to this routine that the value of B is passed, from the calling program via the Newton2.m routine. Results are presented for two choices of x 0 ; − 0 .15 and 0. There is no difficulty with the first choice, and as the output shows, convergence is achieved within 6 iterations for all B. The second choice is fine for the first two values of B, but then gets into a non-convergent cycle for the higher values of B. The iteration loops between two values, one close to 0 and the other close to -1, as anticipated above. The results are collected in the attached Matlab log.

  1. Section 3.4, page 107, problem 8.

The first iteration xn+1 = g(xn) has α = 1 and

g(x) =

15 x^2 − 24 x + 13 4 x We note that g(1) = 1 so that 1 is indeed a fixed point. Further,

g′(x) =

4 x^2

at x = 1.

The last error bound above is 1/ 615 = 2. 13 × 10 −^12 , so that the error tolerance of 10−^8 has been reached. Recalling that bisection halves the error estimate at each iteration, the initial error estimate of 1 will be reduced to 1/ 2 n^ after n iterations. We seek n satisfying

2 −n^ < 10 −^8

and find that n > 8 ln 10/ ln 2 = 26. 57 , so that n = 27 will be needed. The superiority of Newton’s method is self-evident. (b) We proceed as in problem 2 above. The routine Newton2.m is used again, but the calling program is now a2prob4.m, and the function is defined in fun4.m. The calling program has a for loop set up to run for several initial guesses in the interval [− 2 , 2]. The results are presented in the Matlab log. We see that for initial guess x 0 = 0, our theoretical estimate n = 4 is spot on. For other guesses in [− 2 , 2], at most one additional iteration is needed to satisfy the prescribed tolerance.

  1. A catenary is the shape assumed by a heavy cable which is hung between two points not in the same vertical line. If (0, y 0 ) are the coordinates of the lowest point on the catenary, (H, L) those of an end point, w the weight per unit length of the cable, and T 0 the tension at the lowest point, then the equation of the catenary, y = f (x), satisfies

d^2 y dx^2

w T 0

dy dx

and can be solved to yield y =

T 0

w

cosh

w T 0

x

  • y 0 −

T 0

w

(a) Select a suitable tolerance for accuracy and use bisection to calculate T 0 , given that y 0 = 5, w = 10 , H = 15 and L = 20. (b) Repeat the above with the secant method, keeping the error tolerance the same. How do the number of iterations compare with those for bisection? (c) Plot y(x) for x ∈ [− 100 , 50].

(a) Let us set, for ease of writing, T 0 w

= z, (2)

so that the equation of the catenary becomes

y = z cosh(x/z) + y 0 − z. (3)

We are given that y 0 = 5, and that the point (15, 20) lies on the curve. Upon substitution of this information into (3) above one obtains

20 = z cosh(15/z) + 5 − z,

or, upon rewriting, f (z) = 15 + z − z cosh(15/z) = 0. We must solve the above equation numerically to find its root z. Once z is known, (1) can be used to find T 0 since w is known. We know the behavior of cosh(α) = (eα^ + e−α)/2; it is 1 at α = 0 and rises exponentially to ∞ as α → ∞. Therefore, the function f above approaches 15 as z → ∞ and −∞ as z → 0 +. This change in sign indicates the existence of a positive root. With some trial and error we can come up with a bracket for the root. The following plot helps in this exercise, and shows that [8, 10] is a reasonable bracket.

−10 7 8 9 10 11 12 13 14

−

−

−

−

0

2

4

6

8

z

f

We can now use bisection. The script is written as for the Newton method, along with a function file and a calling program. The script and the results are in the attached Matlab log. We select a tolerance of 10−^6 and it takes 21 bisection iterations to produce the result

z = 9. 281389.

(b) We repeat the problem with the secant method, using initial guesses of 8.5 and 9.0. Again, the details appear in the Matlab log, and we note that only five secant iterations are needed to get the answer. (c) With y 0 = 5 and z known, the catenary equation (3) can be plotted, and the plot appears below. We also note that T 0 = wz = 10z = 92. 8139.

(^10) −100 −50 0 50 0

101

102

103

104

105

106

x

y (log scale)

x = x-dx; err = abs(dx); fprintf('\n k = %i Solution = %20.15e 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 fun2.m % Specifies f and fprime for problem 2

% function [f,fp] = fun2(x) global B t=exp(-Bxx); f=x+t*cos(x);

fp=1-tsin(x)+tcos(x)(-2B*x);


Run (a)

x0 = -0.

B = 1.

k = 1 Solution = -7.187254185152172e-01 Error = 5.6872541852e-

k = 2 Solution = -5.863923218301569e-01 Error = 1.3233309669e-

k = 3 Solution = -5.884018102706385e-01 Error = 2.0094884405e-

k = 4 Solution = -5.884017765009962e-01 Error = 3.3769642255e-

k = 5 Solution = -5.884017765009962e-01 Error = 5.3247072272e- root = -0.58840178, iterations = 5

B = 5.

k = 1 Solution = -4.483319702284051e-01 Error = 2.9833197023e-

k = 2 Solution = -4.034171646015186e-01 Error = 4.4914805627e-

k = 3 Solution = -4.049101354726796e-01 Error = 1.4929708712e-

k = 4 Solution = -4.049115482080357e-01 Error = 1.4127353561e-

k = 5 Solution = -4.049115482093092e-01 Error = 1.2734657206e-

k = 6 Solution = -4.049115482093092e-01 Error = 1.9733252558e- root = -0.40491155, iterations = 6

B = 10.

k = 3 Solution = -9.999999798869161e-01 Error = 9.9999997949e-

k = 4 Solution = -3.943746440526752e-10 Error = 9.9999997949e-

k = 5 Solution = -9.999999798868935e-01 Error = 9.9999997949e-

k = 6 Solution = -3.943746440526752e-10 Error = 9.9999997949e-

k = 7 Solution = -9.999999798868935e-01 Error = 9.9999997949e-

k = 8 Solution = -3.943746440526752e-10 Error = 9.9999997949e-

k = 9 Solution = -9.999999798868935e-01 Error = 9.9999997949e- Warning: root not found within tolerance after 9 iterations

B = 50.

k = 1 Solution = -1.000000000000000e+00 Error = 1.0000000000e+

k = 2 Solution = 0.000000000000000e+00 Error = 1.0000000000e+

k = 3 Solution = -1.000000000000000e+00 Error = 1.0000000000e+

k = 4 Solution = 0.000000000000000e+00 Error = 1.0000000000e+

k = 5 Solution = -1.000000000000000e+00 Error = 1.0000000000e+

k = 6 Solution = 0.000000000000000e+00 Error = 1.0000000000e+

k = 7 Solution = -1.000000000000000e+00 Error = 1.0000000000e+

k = 8 Solution = 0.000000000000000e+00 Error = 1.0000000000e+

k = 9 Solution = -1.000000000000000e+00 Error = 1.0000000000e+


PROBLEM 4(b), MATLAB log.

% Assignment 2, problem 4(b) % % script a2prob4.m % % Application of Newton's method to solve 4x-cos x = 0. % This main program will call the generic Newton routine Newton2.m % The function 4x-cos x will be specified in the file fun1.m % The computation will be done for initial guesses x0 = -2:0.2: % The printout will contain the initial guess, the root, and the number % of iterations required to compute it. % disp(' ') tol=1e-8; for x0=-2:0.5: fprintf('initial guess = %10.8f\n',x0); [x,N]=Newton2('fun4',x0,tol);

fprintf(' root = %10.8f, iterations = %2i\n',x,N); disp(' ') end


% file fun4.m % Specifies f and fprime for problem 4 % function [f,fp] = fun4(x) f=4*x-cos(x); fp=4+sin(x);


initial guess = -2.

k = 1 Solution = 4.537635000134719e-01 Error = 2.4537635000e+

k = 2 Solution = 2.473241701612419e-01 Error = 2.0643932985e-

k = 3 Solution = 2.426771504932028e-01 Error = 4.6470196680e-

k = 4 Solution = 2.426746806415885e-01 Error = 2.4698516143e-

k = 5 Solution = 2.426746806408902e-01 Error = 6.9823901784e- root = 0.24267468, iterations = 5

initial guess = -1.

k = 1 Solution = 5.218907793933947e-01 Error = 2.0218907794e+

k = 2 Solution = 2.505382120430970e-01 Error = 2.7135256735e-

k = 3 Solution = 2.426817363738486e-01 Error = 7.8564756692e-

k = 4 Solution = 2.426746806465884e-01 Error = 7.0557272602e-

k = 5 Solution = 2.426746806408902e-01 Error = 5.6982357285e- root = 0.24267468, iterations = 5

initial guess = -1.

k = 1 Solution = 4.374736733555056e-01 Error = 1.4374736734e+

k = 2 Solution = 2.466653145379250e-01 Error = 1.9080835882e-

k = 3 Solution = 2.426765005825059e-01 Error = 3.9888139554e-

k = 4 Solution = 2.426746806412693e-01 Error = 1.8199412366e-

k = 5 Solution = 2.426746806408902e-01 Error = 3.7912483044e- root = 0.24267468, iterations = 5

initial guess = -0.

k = 1 Solution = 3.173616531744941e-01 Error = 8.1736165317e-

initial guess = 2.

k = 1 Solution = 2.856718375710707e-01 Error = 1.7143281624e+

k = 2 Solution = 2.428826565202644e-01 Error = 4.2789181051e-

k = 3 Solution = 2.426746855913809e-01 Error = 2.0797092888e-

k = 4 Solution = 2.426746806408902e-01 Error = 4.9504907261e- root = 0.24267468, iterations = 4

PROBLEM 5 MATLAB log.

5(a)

% Assignment 2, problem 5(a) %

% script a2prob5a_B.m % % Application of bisection to solve %

% f = 15+z-z*cosh(z/w)=0. % % % This main program will call the generic bisection routine bisect.m % The function f will be specified in the file fun5b.m

% % The printout will contain the initial bracket, the root, and the number % of iterations required to compute it. % Specify bracket and tolerance

a=8; b=10; tol=1e- fprintf(' Initial bracket. a = %10.8f, b = %10.8f\n',a,b); x=bisect(a,b,tol);

function xb=bisect(a,b,tol) % Matlab function bisect.m % Solves f(x) = 0 using the bisection method %

% Inputs: % a = left endpoint of interval % b = right endpoint of interval % tol = tolerance for stopping bisection method

% % Required m-file: % fun5b.m is m-file for function f(x)

% Outputs:

% Root by bisection, number of iterations

it=0; % set iteration count to zero

fa=fun5b(a); fb=fun5b(b); % % check bracketing

% if sign(fa)==sign(fb), error('Root not in bracket'); end % % begin while loop %

while (b-a)>tol it=it+1; c=a+0.5*(b-a); fc=fun5b(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

xb=0.5(a+b); err=0.5abs(b-a); fprintf('\n n = %i Solution = %15.10e error = %15.10e \n',it,xb, 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

% print results

format long e

fprintf('\n\n Computed Solution = %e \n\n',xb); fprintf(' Iteration count = %i \n\n',it);


% file fun5b.m

% Specifies f for problem 5 % function f = fun5b(z) a=15/z; f=15+z-z*cosh(a);

Computed Solution = 9.281389e+

Iteration count = 21

5(b)

% Assignment 2, problem 5(b) % % a2prob5bS.m % % Application of the secant method to solve

% % f = 15+z-z*cosh(z/w)=0. % % This main program will call the generic secant routine secant.m

% The function f will be specified in the file fun5b.m % % The printout will contain the initial guesses, the root, and the number % of iterations required to compute it.

% Specify initial guesses and tolerance x0=8.5; x1=9; tol=1e- fprintf(' Initial guesses. x0 = %10.8f, x1 = %10.8f\n',x0,x1); x=secant('fun5b',x0,x1,tol);


function r = secant(fun,x0,x1,tol) % % Input: fun = (string) name of mfile that returns f(x) % x0, x1 = initial guesses

% % Output: r = the root % k = number of iterations used

k = 0; % initialize iteration counter maxit=8; % max iterations allowed

global B

while k<= maxit k=k+1; f0 = feval(fun,x0); % Evaluation of function fun at x0. f1 = feval(fun,x1); % Evaluation of function fun at x1. dx = f1*(x1-x0)/(f1-f0); x0=x1; x1 = x1-dx; err = abs(dx); fprintf('\n k = %i Solution = %20.15e Error = %15.10e

\n',k,x1,err);

if (err < tol), r=x1; return; end end warning(sprintf('root not found within tolerance after %d iterations\n',k));


tol = 1.0000e-

Initial guesses. x0 = 8.50000000, x1 = 9.

k = 1 Solution = 9.246045323952151e+00 Error = 2.4604532395e-

k = 2 Solution = 9.279790066724759e+00 Error = 3.3744742773e-

k = 3 Solution = 9.281379339068522e+00 Error = 1.5892723438e-

k = 4 Solution = 9.281388414282407e+00 Error = 9.0752138847e-

k = 5 Solution = 9.281388416613833e+00 Error = 2.3314262000e-