TASK: Given an alternating, decreasing infinite series, add up a finite number of terms that satisfy an accuracy requirement, using a WHILE statement.
COMMENT: An infinite series represents the sum of an infinite sequence of terms, and has the form:
result = a1 + a2 + a3 + ... + an + ...In an alternating infinite series, the odd terms are positive and the even terms are negative, or vice versa, as in:
log(2) = 1 - 1/2 + 1/3 - 1/4 + 1/5 - ...In an alternating infinite series with decreasing terms, each successive term in the series is smaller (in absolute value) than the preceding one. The series for log(2) is an example, since 1/2 is less than 1, 1/3 is less than 1/2, and so on for each term.
There are two important things about such a series:
For the log(2) infinite series, this means that the value of log(2) can be estimated by:
1, plus or minus 1/2 1-1/2=1/2, plus or minus 1/3 1-1/2+1/3=5/6, plus or minus 1/4 1-1/2+1/3-1/4=7/22, plus or minus 1/5, and so on
So if we wanted to estimate log(2), with an error no greater than 1/10, we simply add up terms from the series until the next term is smaller than 1/10.
The same idea works for ANY alternating infinite series with terms of decreasing size. We will use this idea to estimate the following infinite series:
S = 1/2 - 1/4 + 1/8 - 1/16 + 1/32 - ...with an accuracy of ACC. The exact value of this infinite series is 1/3.
INSTRUCTIONS:
Get a value for the desired accuracy ACC using the input() command. Set S_EXACT to 1/3. Initialize S, your estimate for the sum of the series, to 0. Set A, the value of the "next" term, to 1/2. Set N, the number of steps, to 0. AS LONG AS the MAGNITUDE of A is bigger than ACC increase N add A to S Compute the next value of A. You can do this by dividing A by -2, or multiplying it by -1/2. END PRINT S_EXACT PRINT S, your estimate for S PRINT the actual error, | S - S_EXACT | PRINT your estimated error A PRINT the requested accuracy ACC PRINT the number of terms you added up.
CHECK: Here's an example of results:
>> hw027 Enter desired accuracy: 0.01 Exact S = 0.3333333333333333 Estimated S = 0.3281250000000000 Error = 0.0052083333333333 Error est = 0.0078125000000000 Accuracy = 0.0100000000000000 Number of terms = 6
SUBMIT: Your work should be stored in a script file called "hw027.m". Your script file should begin with at least three comment lines:
% hw027.m % YOUR NAME % This script (describe what it does) % Add any comments here that you care to make.If this problem is part of an assignment, then submit it to Canvas.