Question
Use a while loop to compute a fixed number of iterations and continue computing until a certain convergence condition is met. The idea is that
Use a while loop to compute a fixed number of iterations and continue computing until a certain convergence condition is met. The idea is that each new iteration of the loop produces a slightly better estimate of the value by extending the value from the previous iteration.
For example, let f(i) be the value your program computed for some function in iteration i. Then let delta = fabs(f(i) - f(i-1)). That is, delta is the difference between your approximation in iteration i and your previous approximation from iteration i-1. We use the absolute value function, fabs(), since we are interested only in the magnitude of the difference, not the sign. You then use delta as part of the termination condition within the while loop, as follows: delta = 100;
// Some value large enough to ensure that the loop starts limit = 0.01;
// An example limit at which you want to stop the while loop while (delta > limit) {
// Continue iterating while delta is larger than the limit
// In each iteration, compute a new estimate of f(i) and a new value of delta.
// Note: delta should continue to get smaller with each iteration of the loop.
// The loop will terminate when delta is smaller than the limit. }
With this approach, your program runs until the change from one iteration to the next is smaller than your predefined limit. We say that the program converges to the desired accuracy. Note that you do not need to store all of the values of f(). You only need to save the value from the previous iteration and the current iteration. This approach requires a computation that improves the result with each iteration. For instance, the following infinite series converges to /4: f(i) = 1 1/3 + 1/5 1/7 + 1/9 1/11 + 1/13 1/15 + 1/17
Write a program in c/c++ that uses this type of convergence algorithm to compute the value of using the above series. You should run your program several times, once for each of the following values of limit: {10- 1 , 10-2 , 10-3 , 10-4 , 10-5 , 10-6 }. Print how many iterations are required to reach each limit, and your approximation of for each value of limit.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started