DOC PREVIEW
UMD AMSC 460 - AMSC 460 Project 1

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

AMSC/CMSC 460 Project 1 Due 2/21This project has two objects. The first is to give you some experience with elemen-tary Matlab programming. The second is to explore some of the many ways numericalcomputations can go wrong (or right). There are three subprojects treating 1) seriouserrors without cancellation, 2) convergence criteria for sequences, and 3) computing thehyperbolic function sinh.Repeated square roots.If you enter the number 2 onto a hand calculator and repeatedly take square roots, youwill eventually end up with the number 1. If you try to recover the original number 2by repeated squaring, you will get nowhere.Write a functionfunction repeated_sqrt(x, n)that takes the input x and produces the number y that results from an n-fold applicationof the square root to x. It also computes the number z that is results from an n-foldsquaring of y. Before returning, the function executes the statementsdisp(’ ’)format compactformat longdisp([x, y, z])format short edisp([abs((x-z)/x), abs(y-1), abs((x-z)/x)*abs(y-1)])Run the following script with your function.for i=1:60repeated_sqrt(2, i)end1. What is invariant in the relation between the relative error in z as an approxima-tion to x and in the relative error in y as an approximation to one. In plain wordswhat does this relation mean?2. At some point near the end of the run, the output changes dramatically. Why?You should hand in a listing of your function, the output from the script (the Matlabcommand diary will be useful here), and the answers to the above questions.1AMSC/CMSC 460 Project 1 Due 2/21Testing for convergenceMany algorithms generate a sequence x0, x1, x2, . . . of increasingly accurate approxima-tions to a desired solution x∗. The problem is to determining when to stop the iteration.One procedure is to select a convergence criterion  and return axi+1when|xi+1− xi| ≤  (1)But if convergence is slow enough, this can result in an approximation to x∗with errorgreater than . This part of the project investigates this phenomenon.Let 0 < α < 1. The recursionxi+1= 1 + α(xi− 1) (2)generates a sequence that converges to x∗= 1. Specifically,xi+1− 1 = α(xi− 1) (3)so that each iteration reduces the error |xi− x∗| by a factor of α. In particular if α isnear one the convergence is slow.Write a functionfunction [xconv, index] = conv_test(x0, alpha, epsilon)That computes the recursion (2) until (1) is satisfied and returns the value of xi+1inxconv and i + 1 in index. This function should use no arrays.Write a script that evaluates[xc(k), idx(k)] = conv_test(2, alpha(k), 1e-4);err(k) = xc(k) - 1;disp([xc(k), idx(k), err(k)]);for k = 1,...,10 and alpha(k) = 1 - 2^-k. The sc ript should also give a loglogplot of 1-alpha(k) vs idx(k).For 1 − α ≤ 0.1 the plot looks like a straight line with negative slope one. Here is asketch of an argument why this happens. We have the expressionxi= 1 + αi(x0− x∗) = 1 + αi. (4)Hence|xi+1− xi| = αi(1 − α).Our convergence criterion demands we stop whenαi(1 − α)∼=. (5)2AMSC/CMSC 460 Project 1 Due 2/21Hence the error for stopping with xi+1isei+1= xi+1− 1 = αi+1∼=α1 − α∼=1 − α. (6)Taking logs gives the answer.Give a detailed derivation of (4). Justify the approximations in (6). Show preciselyhow taking logs gives the answer. Also account for the fact that the linearity failsslightly when 1 − α ≤ 0.1.For this part hand in listings of your mfiles, the plot, and your written argument forthe linearity of the plot.The series approximation of the hyperbolic signHow do you computesinh x =ex− e−x2=ex− 1/ex2. (7)if you do not have a reliable library routine? Evaluating it from the formula will notwork when x is sm all, owing to cancellation in forming ex− e−x. On the other handwhen |x| ≥ 1, the formula works well. This suggests that we use the power seriessinh(x) = x +x33!+x55!+ · · · = x1 +x23!+x45!+ · · ·, (8)which converges quickly for |x| ≤ 1, and use the formula sinhform when x > 1. Wewill focus on the power series.Write a functionfunction [y, errest, termnum] = series_sinh(x)to evaluate sinh using the second formula in (8). Denote the terms in the series inparentheses by T1, T3, T5, . . . and use the fact that Ti+2can be easily generated from Tiin your evaluation. Stop summing the series for the first k for which|Ti| ≤ |T1+ T3+ · · · + Ti−2|M.Here Mis the rounding unit, which in Matlab is the variable eps. The function shouldreturnx(T1+ T3+ · · · + Ti−2) for yx|Ti| for erresti for termnumThe return arguments errest and termnum are optional. Their presence can be detectedby interrogating the Matlab variable nargout.3AMSC/CMSC 460 Project 1 Due 2/21For a given integer n let h = 1/n and for i=1:n let x(i) = ih. Write a script thatcomputes[y(i), est(i), tn(i)] = series_sinh(x(i)), i=1:nIn addition compute the relative error relerr(i) in y(i) as an approximation tosinh(x(i)) as computed by the Matlab function sinh. Plot on the same plot x vsrelerr (connected by lines) and x vs est (as the symbol *). Run your script forn = 100.Answer the following two questions.1. Why is est(i) generally smaller than relerr(i)?2. Why the discontinuities in the plot of relerr(i)? [Hint: Try hold, plot(x, 1e-17*tn).]Submit your programing listings, plot, and


View Full Document

UMD AMSC 460 - AMSC 460 Project 1

Download AMSC 460 Project 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view AMSC 460 Project 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view AMSC 460 Project 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?