The value of a can be determined by the series equation: 1 1 1 1 That is, a = 4* sum, where sum= Iteration - Using the w
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
The value of a can be determined by the series equation: 1 1 1 1 That is, a = 4* sum, where sum= Iteration - Using the w
Generating new values xold 1 -0.3333 0.2000 0.5333333333333333 3 2 0.2000 -0.1429 0.34285714285714286 -0.1429 0.1111 0.25396825396825395 0.1111 -0.0909 0.20202020202020202 -0.0909 0.0769 0.16783216783216784 0.0769 -0.0667 0.14358974358974358 4 5 6 7 -0.0667 0.0588 0.12549019607843137 8 0.0588 -0.0526 0.11145510835913312 -0.0526 0.0476 0.10025062656641603 0.0476 -0.0435 0.09109730848861283 9 10 MARGIN_OF_ERROR = 0.05 xnew abs(xnew-xold) •j=1 xold ....(your initial guess) *xnew= ...... (generate new value using the expression) false Print result abs(xnew-xold) > MOE true sum-sum + xnew i++ xold = xnew xnew... (generate new x value) Note: MOE means Margin Of Error MARGIN OF ERROR: 0.05
This type of problem requires you to: Analysis • Compare the absolute value of the difference of two successive terms. • If the value is less that the threshold or margin of error, then the last new value generated is the approximation to the actual value. . In other words, while (|xnew-xold | > some margin of error) { } sum the new value to previous amount save the new as old. generate the new value, xnew Analysis In this example let the margin of error between two successive values be 0.05. • In addition, each new term may be generated as follows expression: pow(-1.0, n)/(2* n + 1);
Sketch of the program /* Define any constants. For instance MARGIN_OF_ERROR = 0.05 */ // Include any necessary file. For instance include stdio.h /* Declare any necessary variables for instance double sum; int iterate; */ Initialize the variable sum. That is sum = 1; /* You may want to define a function that finds each new term. For example public double findXnew(int n) { return pow(-1.0, n)/(2* n + 1); Analysis (cont) Your code details the while loop construct // Details to come..... while (abs(xnew - xold) > MARGIN_OF_ERROR) { // Complete............ }
Example void findPi() { } int i = 1; double xold = 2.0; // Choose a value double xnew=findXnew(i); while (abs(xnew - xold) > MARGIN_OF_ERROR) { } sum = sum + xnew; xold = xnew; i++; xnew = findXnew(i); iterate= = i;