Preview File Edit View Go Tools window Help a- i) 52% L) us. Tue 12:19 AM ENGR10
ID: 3903496 • Letter: P
Question
Preview File Edit View Go Tools window Help a- i) 52% L) us. Tue 12:19 AM ENGR102 Homework 8.pdf (page 1 of 2) ?! Q Search 102 Homew 2.) The Taylor's series expansion for e x is: 2 3 2! 3! 00 TI n! Write a program that calculates ex using Taylor's series expansion. The program asks the user to input a value for x. Then the program uses a loop for adding the terms of the Taylor series. If an is the nth term in the series, then the sum, Sn of the n terms is SnSn-1an. In each pass calculate the estimated error E given by: 72 n-1 Stop adding terms when E s 0.000001. Stop adding terms when the number of iterations is greater than 20. (Hint: Use the break command). Use an output statement to output the number of iterations "The number of iterations was xxx") ENGR102 .. While Loops [2).pptx Show All X 12Explanation / Answer
import math
def fact(n):
if n == 1 :
return 1
return n * fact(n-1)
s = 1.0
count = 1
while True:
a = pow(x,count)/fact(count)
if abs(a /s) > 0.000001 :
s = s + a;
else:
break
count = count + 1
if count > 20:
break
print("The number of iteration was",count)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.