Question

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 the approximation calculated by your program:

Difference = 1 – approximate result = 0.125

HINT: You will probably want to define some variables before your for loop, such as a variable to keep track of the value of the current term and a variable to keep track of the sign of the current term. For example, initially, you can have approx = 0. As the program iterates through the loop, approx can be modified using approx = approx + current.

(NOTE: Make sure your program is not doing integer division (see p. 59). If you are using a version of Python earlier than Python 3, the division operator “/” may not give you the result you are expecting.)

You may have noticed that the approximation, at least using just 3 terms, is not very accurate. Modify your program from part a to look at all how the approximation improves as more terms are used. Your modified program will NOT do the comparison to 1 – it will just print out the value of the current approximation. For example, if you name your modified program approx2, calling approx2(3) will print out the following:

0.5 0.75 0.875

Now have your modified program round responses to 2 decimal places. How many terms are needed for the approximation to return a result of 0.99 or greater?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
 
double f(double x);
 
int main()
{
    unsigned int start = 1;
    unsigned int end = 1000;
    double sum = 0;
 
    for( unsigned int x = start; x <= end; ++x )
    {
        sum += f(x);
    }
    std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
    return 0;
}
 
 
double f(double x)
{
    return ( 1.0 / ( x * x ) );
}
Add a comment
Know the answer?
Add Answer to:
Write a program that approximates the sum of this geometric series using a user- specified number...
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
  • 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.

  • 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...

  • THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that...

    THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that calculates exp(2) by a Maclaurin series using a while loop exp x )=-+-+-+-+ The while loop should add each of the series terms. The program error as defined below is smaller than 0.01. Error is the exact value (i.e. exp(2)), minus the approximate value (i.e., the current series sum) should exit the loop when the absolute absolute error-lexact-approx Use fprintf within the loop such...

  • Write a program that calculates the average of a stream of non-negative numbers. The user can...

    Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read...

  • This is the given code: /** * This program uses a Taylor Series to compute a...

    This is the given code: /** * This program uses a Taylor Series to compute a value * of sine. * */ #include<stdlib.h> #include<stdio.h> #include<math.h> /** * A function to compute the factorial function, n!. */ long factorial(int n) { long result = 1, i; for(i=2; i<=n; i++) { result *= i; } return result; } int main(int argc, char **argv) { if(argc != 3) { fprintf(stderr, "Usage: %s x n ", argv[0]); exit(1); } double x = atof(argv[1]); int...

  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • In C++ 2. Write a program that allows the user to enter a series of positive...

    In C++ 2. Write a program that allows the user to enter a series of positive numbers between 1 and 100 and displays the smallest and largest of the numbers entered. The program should not store the numbers that were entered, only keep track of the smallest and largest ones. The program should continue accepting numbers until the value 0 is entered and then display the results. If a number out of range is entered, tell the user it is...

  • Write a C program to sum this series through the term 1/199 and output the result....

    Write a C program to sum this series through the term 1/199 and output the result. Use a for loop in your program and include a header, preprocessor directives, function prototype reserved words, main function, standard identifier, function call, function definitions with comments . Note that the series converges very slowly, so your result may not be very accurate. The series ln(2) = 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ... converges to the value...

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • 1) Write and test a well-commented Python function, called “myexp” that approximates the value for the...

    1) Write and test a well-commented Python function, called “myexp” that approximates the value for the function ?(?) = ? ? . This function can be approximated by a Taylor series ? ? ≈ ∑ ? ? ?! ∞ ?=0 , and recall that the factorial function is given by the recursive relationship ?! = ? (? − 1)!, ?ℎ??? ? ∈ ??? ≥ 0, ??? 0! = 1 1. You will need to write your own factorial function, and...

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