Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Build a program in Python that approximates ln(1.9) within ten digits of accurac

ID: 667233 • Letter: B

Question

Build a program in Python that approximates ln(1.9) within ten digits of accuracy using the following Taylor series: ln[(1+x)/1-x)] = 2 (x2k-1)/(2k-1). Your code should print out the number of terms needed, the value obtained from the series, the error.

I know I should use a while loop, but my code below seems to run an infinite loop:

import numpy

ln = numpy.log(1.9)
print ln
x = (0.9 / 2.9)
i = 1
taySum = 0

while abs(taySum - ln) <= .0000000005:
    taySum += (pow(x,i))/(i)
    i += 2
    print 2 * taySum

What am I doing wrong? Could someone explain how to build this using Python code?

Explanation / Answer

#!/usr/bin/python # -*- coding: utf-8 -*- import math #Calculate n! def factorial(n): temp = 1 for i in range(1, n + 1): temp *= i return temp #Calculate n-th member of Taylor series def Taylor(x, n): return (-1.0) ** n * x ** (2 * n + 1) / factorial(2 * n + 1) epsilon = 10 ** -100 x = float(raw_input("Enter angle(in radians):")) sum = Taylor(x, 0) + Taylor(x, 1) prevSum = Taylor(x, 0) n = 1 while (abs(sum - prevSum) > epsilon): prevSum = sum n += 1 sum += Taylor(x, n) approx = x - 1.0 * x ** 3 / factorial(3) + 1.0 * x ** 5 / factorial(5) - 1.0 * x ** 7 / factorial(7) print "Taylor: sin(" + str(x) + ")=" + str(sum); print "Python: sin(" + str(x) + ")=" + str(math.sin(x)); print "Approx: sin(" + str(x) + ")=" + str(approx);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote