Question

Write a program in Python that approximates the value of π by summing the terms of...

Write a program in Python that approximates the value of π by summing the terms of this series:

4/1-4/3 + 4/5- 4/7 + 4/9- 4/11 + ... The program should prompt the user for n, the number of terms to sum, and then output the sum of the first n terms of this series. Have your program subtract the approximation from the value of math. pi to see how accurate it is.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Screenshots:

Sample Output:

Code to Copy:

#Import the math module.

import math

#Define the main()

#function.

def main():

    #Prompt the user to

    #enter the value of n.

    n = int(input("Enter the value of n: "))

   

    #Define teh required variables.

    calculated_pi_val = 0

    denominator = 1

    term_sign = 1

    #Run the loop to calculate

    #the sum of the first n

    #terms of the series.

    for i in range(n):

        #Add the next term to the series.

        calculated_pi_val += (term_sign*(4/denominator))

       

        #Increase the

        #denominator value by 2.

        denominator += 2

       

        #Change the sign

        #of the next term.

        term_sign *= -1

    #Calculate the error

    #in the approximated value.

    error = math.pi - calculated_pi_val

    #Display the calculated

    #value of pi.

    print("The calculated value"\

    " of pi is:", calculated_pi_val)

    #Display the error.

    print("Error in the calculated"\

    " value is:", error)

#Call the main()

#function.

main()

Add a comment
Know the answer?
Add Answer to:
Write a program in Python that approximates the value of π by summing the terms of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following function computes by summing the Taylor series expansion to n terms. Write a program...

    The following function computes by summing the Taylor series expansion to n terms. Write a program to print a table of using both this function and the exp() function from the math library, for x = 0 to 1 in steps of 0.1. The program should ask the user what value of n to use. (PLEASE WRITE IN PYTHON) def taylor(x, n): sum = 1 term = 1 for i in range(1, n): term = term * x / i...

  • Write a program that approximates the sum of this geometric series using a user- specified number...

    Write a program that approximates the sum of this geometric series using a user- specified number of terms. For example, if you named your program approx, calling approx(3) should use the first 3 terms of the series to calculate the approximation: Approximate result = 1/2 + 1/4 + 1/8 = 0.875... Note: the values are all powers of 1⁄2: (1/2)1, (1/2)2, (1/2)3, ... Next, have your program calculate the difference (rounded to 3 decimal places) between the value “1” and...

  • Write a c++ program to calculate the approximate value of pi using this series. The program...

    Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough. You will try it with numbers read in from a file. to see the accuracy increase. Use a for loop...

  • Generating a square wave This function approximates a square wave by summing a series of sinusoid...

    Generating a square wave This function approximates a square wave by summing a series of sinusoidal functions of various frequencies and amplitudes square n MATLAB write a function that allows the user to choose the number of terms used to approximate the square wave, i.e. input the value of k. Your function should plot the resulting square wave, show the value used for k in the title (note k needs to be converted to a string data type), and plot...

  • Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds...

    Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds the sum and average of a series of numbers he program should first prompt the user to enter total numbers of numbers are to be summed and averaged. It should then as for input for each of the numbers, add them, and print the total of the numbers and their average 2. Write a progra m that finds the area of a circle. The...

  • Write a matlab program to apply Horner's algorithm to evaluate the sum of the first four...

    Write a matlab program to apply Horner's algorithm to evaluate the sum of the first four nonzero terms of the Taylor series for sin(x) with c=0. Your program should not use any arrays, the exponentiation operator or the factorial function. Print out your program and include the output upon execution for the special case where x = 1. How many nonzero terms of the above Taylor series are needed to guarantee that the sum approximates sin(x) to within .0001 for...

  • Write a PYTHON program that will approximate the value of π. You can do this by...

    Write a PYTHON program that will approximate the value of π. You can do this by computing π to be the ratio of the area of a circle to the area of the square that bounds that circle. Assume a circle of radius 0.5 enclosed by a 1x1 square. The area of the circle then is πr^2=π/4, since r=0.5=1/2 and the area of the square is 1. To approximate the ratio, take a large number of uniformly distributed random points....

  • 7. Use your class to calculate rational approximations for π using the series π=1-1+1-1+, . Your program should ask the user how many terms to include in the approximation. For full credit, use t...

    7. Use your class to calculate rational approximations for π using the series π=1-1+1-1+, . Your program should ask the user how many terms to include in the approximation. For full credit, use the class above as much as possible (to do all the calculations) 7. Use your class to calculate rational approximations for π using the series π=1-1+1-1+, . Your program should ask the user how many terms to include in the approximation. For full credit, use the class...

  • (previous one did not work) You've been hired by PI Throwers to write a C++ console...

    (previous one did not work) You've been hired by PI Throwers to write a C++ console application that approximates PI. Use a validation loop to prompt and get from the user the number of terms to approximate PI to that is at least 1. Use the following Leibniz formula:PI approximation = 4 * (1/1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + ...)The terms appear within the parentheses in the formula. A PI approximation to ... ●...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT