Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def approximate_alternating_series(f, num_decimal_points):
- '''
- returns the approximation of a convergent alternating series
- f = a function that would produce a convergent alternating series
- num_decimal_points = expected precision of approximation
- '''
- #get rid of (-1)**n in the numerator
- f_without_neg_one_to_the_n = f / sympify((-1)**n)
- #sub in n+1 for all n in the function
- f_of_n_plus_one = f_without_neg_one_to_the_n.subs(n, n+1)
- precision = 1
- for _ in range(num_decimal_points):
- precision = precision / 10
- terms = 0
- check_num_terms = f_of_n_plus_one.subs(n, terms)
- while check_num_terms > precision:
- terms = terms + 1
- check_num_terms = f_of_n_plus_one.subs(n, terms)
- approximation = 0
- for term in range(terms):
- approximation = approximation + f.subs(n, term)
- return approximation.evalf()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement