Hello there, I have a take-home final exam due and the content is a bit tricky. Will still give the assignment a shot, but will like some extra assurance that'll I'll do alright for this assignment if...

Hello there, I have a take-home final exam due and the content is a bit tricky. Will still give the assignment a shot, but will like some extra assurance that'll I'll do alright for this assignment if I'm not so confident with my work.


I've attached the assignment as well as the chapters required to use pre-existing code and to modify them when dealing with the questions. If you can explain the function of what the code is suppose to do on each side of your pivotal lines of code, by commenting them on, that would be much appreciative.


And if you could follow these guidelines when finishing up, that would be great as well:


1) You need to submit one .ipynb file from Jupyter Notebook and one pdf file(this file has to be the pdf version of the code necessarily) that can be done on Jupyter Notebook as well.


2)The .ipynb files should be submitted with the title as (YourName_Assignment_2).


3)The output should be visible in the notebook and/or the pdf files. You shouldn't leave any cell unexecuted.


4) Whenever file operations are being done, you need to print put the contents of the output file in the same/next cell, or at least provide me with the output file.


Thank you.


NANO3600 – Take-Home Final Exam Alex Gezerlis Posted on: April 10th, 2021 Due on: April 27th, 2021, 5 pm [A reminder: an answer of the type “The number of boojums is 7” is not good enough. You should always explain/derive your answers.] [Another reminder: If you need to write a code to answer a specific problem, you should provide a listing of that program, even if the question doesn’t explicitly ask you to do so.] [A clarification: I leave the style of assignment solutions to you: should you type them or handwrite them? You could also do a mixture: hand-write some parts and include a listing of the code on a separate sheet. In any case, at the end of this process you must produce a single pdf file and upload that onto the Dropbox on courselink. All the programs that were produced should also be separately provided in a single Jupyter Notebook.] 1. The following is implicitly defining a recurrence relation: f0,f1 = 0,1 for i in range(n-1): f0,f1 = f1,f0+2*f1 We will now produce increasingly fancier versions of this code snippet. (a) Define a function that takes in the cardinal number n and returns the corresponding latest number following the above recurrence relation. In other words, for n = 0 you should get 0, for n = 1 you should get 1, for n = 2 you should get 2, for n = 3 you should get 5, and so on. (b) Define a recursive function (i.e., a function that calls itself) taking in the cardinal number n and returning the corresponding latest number. The interface of the function will be identical to that of the previous part but, obviously, the implementation will be different. (c) Define a function that is similar to that of the previous part, but is more efficient. Outside the function, define a dictionary ntoval = {0:0, 1:1}. Inside the function, you should check to see if the n that was passed in exists as a key in ntoval: if it does, then simply return the corresponding value; if it doesn’t, then carry out the necessary computation and augment the dictionary with a new key-value pair. (d) If you take separation of concerns seriously, you may be feeling uncomfortable about accessing and modifying ntoval inside your function (since it is not being passed in as a parameter). Write a new function that looks like the one in the previous part, but takes in two parameters: n and ntoval. (e) While the function in the previous part respects separation of concerns, unfortunately it is not actually efficient. Write a new function which is similar to it, but uses a mutable default parameter value, i.e., it is defined by saying def f5(n, ntoval = {0:0, 1:1}): 2 Test all five functions with n = 8: each of them should return 408. The functions in parts (c) and (e) should be efficient in the sense that if you now call them with, say, n = 6 they won’t need to recompute the answer since they have already done so. 2. This problem studies the quantity (1 + 1/n)n where n = 101, 102, . . . , 107. Print out a nicely formatted table where the three columns are: (a) the value of n, (b) the quantity of interest computed with single-precision floating point numbers, (c) the same quantity but now using doubles. You will need to use NumPy to get the singles to work. Keep in mind that the numbers shown in the second and third columns should be different (if they aren’t, your’re doing it wrong.) 3. This problem discusses error buildup when trying to evaluate polynomials without and with the use of Horner’s rule. Take a polynomial of degree n− 1: P (x) = p0 + p1x+ p2x 2 + p3x 3 + · · ·+ pn−1xn−1 (1) Write a function that takes in a list containing the pi’s, say coeffs, and the point x and evaluates the value of P (x) in the naive way, i.e., from left to right. Notice that this way of coding up the polynomial contains several (needless) multiplications. This is so because xi is evaluated as x × x × · · · × x (where there are i − 1 multiplications). Thus, this way of approaching the problem corresponds to: 1 + 2 + · · ·+ n− 2 = (n− 1)(n− 2) 2 (2) multiplications, from x2 all the way up to xn−1. If we rewrite the polynomial: P (x) = p0 + x (p1 + x (p2 + x (p3 + · · ·+ x (pn−2 + xpn−1) · · · ))) (3) then we can get away with only n− 1 multiplications (i.e., no powers are evaluated). This is obviously more efficient, but equally important is the fact that this way we can substantially limit the accumulation of rounding error (especially for polynomials of large degree). Write a function that takes in a list containing the pi’s, say coeffs, and the point x and evaluates the value of P (x) in the new way, i.e., from right to left. Apply the previous two functions to the (admittedly artificial) case of: coeffs = [(-11)**i for i in reversed(range(8))] and x = 11.01. Observe any discrepancy and discuss its origin. 4. This problem deals with the error behaviour of the first central-difference approximation to the second derivative. (a) Start with the error analysis, including both approximation and roundoff error. Derive expressions for the hopt and the Eopt. Then, produce numerical estimates for hopt and the Eopt. Compare these results to those for the first derivative. (b) Now code this problem up in Python (for the function f(x) = esin(2x) at x = 0.5) to produce both a table of numbers and a plot for the absolute error, with h taking on the values 10−1, 10−2, 10−3, . . ., 10−10. 3 5. Update gauelim.py such that it can handle multiple constant vectors. In other words, gen- eralize gauelim() such that it takes in an n × n matrix A and an n ×m matrix B, where m is the number of distinct constant vectors you will be handling. The output should be an n×m matrix X, collecting all the solution vectors: X = ( x0 x1 . . . xm−1 ) , B = ( b0 b1 . . . bm−1 ) (4) Test out the case where A is a 4× 4 matrix and B is 4× 3. 6. Explore the roots of: f(x) = −x5 + 4x4 − 4x3 + x2ex − 2x2 − 4xex + 8x+ 4ex − 8 (5) using a method of your choice. 7. An intriguing generalization of Newton’s method for multidimensional root-finding borrows ideas from multidimensional minimization. The goal is to ensure that Newton’s method can do a good job of finding the solution to a system of equations, even if the initial guess was poor; in other words, we wish to make sure that each step we make decreases the function- component values.1 To do so, we now examine the quantity (f(x(k−1)))T f(x(k−1)): it makes sense to require that after the step the magnitude of the functions is reduced, i.e., that (f(x(k)))T f(x(k)) is smaller. It’s easy to show that the Newton step is a descent direction; in other words, the step prescribed by Newton’s method picks a direction that reduces the functions’ values. Here’s how to see this: 1 2 { ∇ [( f(x(k−1)) )T f(x(k−1)) ]}T q(k) = ( f(x(k−1)) )T J(x(k−1)) ( − ( J(x(k−1)) )−1 f(x(k−1)) ) = − ( f(x(k−1)) )T f(x(k−1)) < 0 (6) on the first line, q(k) is the newton step as per eq. (5.83). we also took the gradient of our ft f quantity using a standard vector identity, and also re-expressed the newton step from eq. (5.80). from there, we cancelled the jacobians and were left with a negative value. indeed, the newton step is a descent direction. now the question arises how to pick the magnitude of our step in that direction (similarly to what we saw when studying the gradient-descent method for minimization). we could, at this point, employ a line-search approach as we did in an earlier problem. instead, we will try the empirical approach of multiplying the full newton step with λ = 0.25, if the function value would have otherwise increased. try out this backtracking strategy for our example system from eq. (5.72): starting from xolds = np.array([-100.,100.]), compare the unmodified and modified newton’s runs. make sure to print out a message each time the step is/would have been bad. 8. use chebyshev points and lagrange interpolation for (a) f(x) = esin(20x) (for n = 15 and n = 60), and (b) f(x) = 100(x− 1)3 cos(4(x− 1))e5(x−1) (for n = 7 and n = 15). 1in jargon, we are looking for a globally convergent method. 4 9. in adaptive.py we gave a general adaptive integration routine. note, however, that this was wasteful: when you double the number of panels from n to n ′ = 2n , you don’t need to throw away all the function values that you’ve already computed. explicitly write out the first few cases (i1, i2, i4, i8) to convince yourself that: in ′ = 1 2 in + hn ′ n ′−1∑ k=1,3,... f (a+ khn ′) (7) implement this approach to produce an adaptive trapezoid integrator that is efficient. 10. generalize eq. (7.162) so that you can tackle the following five-dimensional integral: i = ∫ 1 −1 du dv dw dx dy ( 2 + u+ v 5 + w + x )y (8) using gauss–legendre quadrature (via np.polynomial.legendre.leggauss()) and n = 5–30. then, apply uniform multidimensional monte carlo integration as per eq. (7.211), with n = 107–108. observe that the n ’s in the two cases are vastly different, but the total runtimes not so much. 11. generalize code 8.1 to use chebyshev points. then, employ lagrange interpolation in be- tween the points and produce a new version of fig. 8.4. 12. derive eq. (8.4) for the problem of the two-dimensional projectile in the presence of air resistance. implement this programmatically, using rk4 gen() and plot the trajectory (i.e., y(t) as a function of x(t)). to be concrete, take k = 1, g = 9.81, x(0) = 1, vx(0) = 2, y(0) = 5, and vy(0) = 7.808; integrate from t = 0 up to t = 2.5. 13. solve eq. (8.4) for the problem of the two-dimensional projectile in the presence 0="" (6)="" on="" the="" first="" line,="" q(k)="" is="" the="" newton="" step="" as="" per="" eq.="" (5.83).="" we="" also="" took="" the="" gradient="" of="" our="" ft="" f="" quantity="" using="" a="" standard="" vector="" identity,="" and="" also="" re-expressed="" the="" newton="" step="" from="" eq.="" (5.80).="" from="" there,="" we="" cancelled="" the="" jacobians="" and="" were="" left="" with="" a="" negative="" value.="" indeed,="" the="" newton="" step="" is="" a="" descent="" direction.="" now="" the="" question="" arises="" how="" to="" pick="" the="" magnitude="" of="" our="" step="" in="" that="" direction="" (similarly="" to="" what="" we="" saw="" when="" studying="" the="" gradient-descent="" method="" for="" minimization).="" we="" could,="" at="" this="" point,="" employ="" a="" line-search="" approach="" as="" we="" did="" in="" an="" earlier="" problem.="" instead,="" we="" will="" try="" the="" empirical="" approach="" of="" multiplying="" the="" full="" newton="" step="" with="" λ="0.25," if="" the="" function="" value="" would="" have="" otherwise="" increased.="" try="" out="" this="" backtracking="" strategy="" for="" our="" example="" system="" from="" eq.="" (5.72):="" starting="" from="" xolds="np.array([-100.,100.])," compare="" the="" unmodified="" and="" modified="" newton’s="" runs.="" make="" sure="" to="" print="" out="" a="" message="" each="" time="" the="" step="" is/would="" have="" been="" bad.="" 8.="" use="" chebyshev="" points="" and="" lagrange="" interpolation="" for="" (a)="" f(x)="esin(20x)" (for="" n="15" and="" n="60)," and="" (b)="" f(x)="100(x−" 1)3="" cos(4(x−="" 1))e5(x−1)="" (for="" n="7" and="" n="15)." 1in="" jargon,="" we="" are="" looking="" for="" a="" globally="" convergent="" method.="" 4="" 9.="" in="" adaptive.py="" we="" gave="" a="" general="" adaptive="" integration="" routine.="" note,="" however,="" that="" this="" was="" wasteful:="" when="" you="" double="" the="" number="" of="" panels="" from="" n="" to="" n="" ′="2N" ,="" you="" don’t="" need="" to="" throw="" away="" all="" the="" function="" values="" that="" you’ve="" already="" computed.="" explicitly="" write="" out="" the="" first="" few="" cases="" (i1,="" i2,="" i4,="" i8)="" to="" convince="" yourself="" that:="" in="" ′="1" 2="" in="" +="" hn="" ′="" n="" ′−1∑="" k="1,3,..." f="" (a+="" khn="" ′)="" (7)="" implement="" this="" approach="" to="" produce="" an="" adaptive="" trapezoid="" integrator="" that="" is="" efficient.="" 10.="" generalize="" eq.="" (7.162)="" so="" that="" you="" can="" tackle="" the="" following="" five-dimensional="" integral:="" i="∫" 1="" −1="" du="" dv="" dw="" dx="" dy="" (="" 2="" +="" u+="" v="" 5="" +="" w="" +="" x="" )y="" (8)="" using="" gauss–legendre="" quadrature="" (via="" np.polynomial.legendre.leggauss())="" and="" n="5–30." then,="" apply="" uniform="" multidimensional="" monte="" carlo="" integration="" as="" per="" eq.="" (7.211),="" with="" n="107–108." observe="" that="" the="" n="" ’s="" in="" the="" two="" cases="" are="" vastly="" different,="" but="" the="" total="" runtimes="" not="" so="" much.="" 11.="" generalize="" code="" 8.1="" to="" use="" chebyshev="" points.="" then,="" employ="" lagrange="" interpolation="" in="" be-="" tween="" the="" points="" and="" produce="" a="" new="" version="" of="" fig.="" 8.4.="" 12.="" derive="" eq.="" (8.4)="" for="" the="" problem="" of="" the="" two-dimensional="" projectile="" in="" the="" presence="" of="" air="" resistance.="" implement="" this="" programmatically,="" using="" rk4="" gen()="" and="" plot="" the="" trajectory="" (i.e.,="" y(t)="" as="" a="" function="" of="" x(t)).="" to="" be="" concrete,="" take="" k="1," g="9.81," x(0)="1," vx(0)="2," y(0)="5," and="" vy(0)="7.808;" integrate="" from="" t="0" up="" to="" t="2.5." 13.="" solve="" eq.="" (8.4)="" for="" the="" problem="" of="" the="" two-dimensional="" projectile="" in="" the="">
Apr 26, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here