Specified x values, epsilon accuracy. To compile a program for calculating the function y with an epsilon accuracy using recursive and iterative algorithms for solving the problem. Determine how many members of the series must be summed to achieve the specified accuracy (compare the result of the summation with the value of the standard function).

enter image description here

Here's what happened so far:

def NonRec(x, eps): s=1 term=1 i=0 while (abs(term) > eps): term=term*(x/(i+1)) s+=term i+=1 return s 
  • I could not implement a recursive cosine finding algorithm, so I opened a new question . You might be interested ... - MaxU

1 answer 1

Non-recursive function:

 import math def cosine(x, eps=1e-5): res = 1 i = 1 x = x * math.pi / 180. while True: delta = (x**(2*i) / math.factorial(2*i)) res += (-1)**i * delta i += 1 if delta <= eps: return res, i 

Example:

 In [50]: res, n = cosine(123, eps=1e-6) In [51]: print(res, n) -0.544639044595552 8