辅导案例-HW 1
HW 1: Stable Algorithms and FPA Due September 11 1. How can we rewrite sin x− sin y, when x ≈ y to avoid cancelation. 2. How can we rewrite x2 − y2, when x ≈ y to avoid cancelation. 3. For the following examples (a) Estimate f(x0) for the given x0 using Taylor series, or trigonometric identities as indicated. (b) Estimate the relative condition number of f(x) in the neighborhood of x0. Can you calculate f(x0) stably? (c) Does the given expression for f(x) lead to a stable calculation? If not rewrite so that you can estimate f(x0) stably and avoid cancellation. (d) Find an example to illustrate the difference. (I) f(x) = √ x+ 1− 1 for x0 near 0 and use Taylor series (I:a) Estimate f(x0) for the given x0 using Taylor series, or trigonometric iden- tities as indicated. (II) f(x) = (1− cosx)/ sinx , near 0 and use a trigonometric identity and Taylor to estimate. (III) f(θ) = (a2 + b2 − 2ab cos θ)1/2, a ≈ b, and |θ| ≈ 0 Here the variable is θ (III:c) With the given expression we evaluate a − b which leads to cancelation when a ≈ b. 4. Computing (ex−1)/x. Consider two algorithms for the evaluation of f(x) = (ex−1)/x x=logspace(-14,-5,50); % Algorithm A f1=ones(size(x)); f1(x~=0)=(exp(x(x~=0))-1)./x(x~=0); % Algorithm B f2=ones(size(x)); y=exp(x); f2(y~=1)=(y(y~=1)-1)./log(y(y~=1)); Use Matlab to calculate f(x) for x = 10−t, t = 5, . . . , 15 with both algorithms. Tabulate your results. Comment on the results. What is the correct answer? Prove this. In the solution plot all the results. Note that to tabulate the results you can use: table(x(1:10)’, f1(1:10)’, f2(1:10)’,’VariableNames’,{’x’,’f1’,’f2’}) 1 To try and explain whats happening consider the calculation of f(x) for x = 9×10−8 and a machine epsilon approximately 6× 10−8 and 6e− 7 by both algorithms. Next assume that both expx and log y are computed with a relative error not exceeding the machine epsilon and that an extra digit is used in the computation. Verify that Algorithm B performs well because of cancellation. 2