Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. def approximate_alternating_series(f, num_decimal_points):
  2. '''
  3. returns the approximation of a convergent alternating series
  4. f = a function that would produce a convergent alternating series
  5. num_decimal_points = expected precision of approximation
  6. '''
  7. #get rid of (-1)**n in the numerator
  8. f_without_neg_one_to_the_n = f / sympify((-1)**n)
  9. #sub in n+1 for all n in the function
  10. f_of_n_plus_one = f_without_neg_one_to_the_n.subs(n, n+1)
  11. precision = 1
  12. for _ in range(num_decimal_points):
  13. precision = precision / 10
  14. terms = 0
  15. check_num_terms = f_of_n_plus_one.subs(n, terms)
  16. while check_num_terms > precision:
  17. terms = terms + 1
  18. check_num_terms = f_of_n_plus_one.subs(n, terms)
  19. approximation = 0
  20. for term in range(terms):
  21. approximation = approximation + f.subs(n, term)
  22. return approximation.evalf()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement