Using a while loop which stops due to meeting a tolerance co

Using a while loop which stops due to meeting a tolerance condition (see Lecture- Chapter 13-15, 17, the example of using a while loop for computing the Maclaurin Series for sin(x), for reference), design a function to find the following sum. sigma^infinity_n=0 e^nx, x

Solution

import math

def p2(x, tol):
   n = 0
   ans = 0.0
   if x >= 0:
       print \"x must be less than 0\"
       return 0
   while math.exp(n*x) >= tol:      
       ans += math.exp(n*x)
       n +=1
   return ans


print p2(-0.5, 0.00000001)

print 1/(1- math.exp(-0.5))


  

\'\'\'
2.54149405906
2.54149408254
\'\'\'

 Using a while loop which stops due to meeting a tolerance condition (see Lecture- Chapter 13-15, 17, the example of using a while loop for computing the Maclau

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site