I Have A Homework With Python Can You Help With Task 3,4? I Did The Task 1,2 But I Think I Am Stuck To Connect With These Problem Solved As Below Thank You

匿名用户 最后更新于 2021-11-29 17:10 计算机类Computing

Task 3 Consider x = 1.41. Plot the absolut value of the error versus n. Task 4 In the above article a method is suggested to accelerate the convergence: do,i = a i = 0,...,n k = 1,...,n i = 0,...,n Ai, dki 3 1-4-k As approximation to in the value 2-1 is taken. Write a function fast_approx_ln (x, n) in which this approach is implemented.
읽 V 1271 v · 강조 표시 y & ㅇ 지두 It is based of computing the arithmetic and geometric mean of two values ai, gi: (1+r) • For a given value x > 0, initialize ao • Iterate ai+1 = 09i and 9i+1 = Vai+19i, as an approximation to ln(x). 2 +, 90 = Vă, • Consider 2-1 ai Task 1 I Write a function approx_ln (x, n) that approximates the logarithm by n steps of the above algorithm. Task 2 Plot both functions,In and approx_ln, in one plot and the difference of both functions in another plot. Do this for different values of n.
: #*TASK 1,2## import math def approx_in(x,n): #printl 'n = 'strinj ans = 0 if x > 0: ai = (1+x)/2 gi = math.sart(x) for i in range(n): aiplus1 = (ai+gi)/2 giplust = math.sart (aiplusi+gi) ans = (x-1)/ai ai - aiplust gi = giplus return ans else print('Cant work with negative numbers!') #plor iaport matplot lib.pyplot as plt arr=[] orig=0 approx [] x = 6 for i in range(1, 10): orig.append(math.log(x))#original values of In(x) unchanged approx.append(approx_In(x, i)) #approx value using our algorithm #Append the difference arr.append(orig[-1] approx[-1]) plt.plot(arr) plt.title( 'Difference Plot) plt.ylabel(Difference value') plt.xlabel( ---> n') plt.show plt.plot(orig) plt.plot(approx) plt.title('inix) and approx_in(x)) plt.xlabel(-_---> n) plt.legend([ Original, Predicted']) Difference Plot 0.35 1C

已邀请: