# Loops & Series Question

Printable View

• November 28th, 2011, 07:15 PM
nuttaay
Loops & Series Question
Assigned Question:

The infinite series:
" 1 + 22 * x + 32 * x2 + 42 * x 3 + ....."

Each term in the series is made up of a coefficient multiplying a power of x. The coefficients are the squares of the successive integers starting with 1, and the powers uses successive integers, starting with 1, and the powers uses successive exponents starting at 0. The sum converges when |x| is less than 1.

Write a program that reads x, verifies that |x| < 1 or else crashes, and then computes and outputs the sum together with the oracle's answer:
Oracle is: (1 + x) / (1 - x)3

To compute the sum, keep adding terms until you reach a term whose value is less than 0.0001 in absolute value.

I wrote the program.. but something is missing because when I compiled it & ran it... my program would keep printing the correct value ( the answer of the series of the sum) an infinite amount of times.. How do I fix it?

Here is my program:

Code Java:

```import java.io.PrintStream; import java.util.Scanner; import type.lib.ToolBox;   public class Check07C { public static void main(String[] args) { PrintStream out = System.out; Scanner in = new Scanner(System.in);     out.print("Enter the value of x ... "); double x = in.nextDouble(); double series = 0;   if (Math.abs(x) > 1.0) { ToolBox.crash(true, "|x| must be < 1."); } else { double y = Math.pow(1 - x, 3); double oracle = (1 + x) / y; out.println("The Oracle's answer is: " + oracle);   for (int i = 0; ;i++) { double pow = Math.pow(i, 2); double pow1 = Math.pow(x, i - 1.0); double term = pow * pow1;   if (Math.abs(term) < 0.0001) {   out.println("The sum of the series = " + series); } else { series = series + term; } }   } } }```
• November 29th, 2011, 07:10 AM
KevinWorkman
Re: Loops & Series Question
Quote:

Originally Posted by nuttaay
Code Java:

` for (int i = 0; ;i++)`

When do you expect that loop to exit?
• November 29th, 2011, 11:41 AM
nuttaay
Re: Loops & Series Question
I want it to exit the loop when a term's absolute value is less than 0.0001. Thus I added a condition in the for loop:

Code Java:

```  boolean check = true;   out.print("Enter the value of x ... "); double x = in.nextDouble(); double series = 0;   if (Math.abs(x) > 1.0) { ToolBox.crash(true, "|x| must be < 1."); } else { double y = Math.pow(1 - x, 3); double oracle = (1 + x) / y; out.println("The Oracle's answer is: " + oracle);   for (int i = 0; check == true; i++) { double pow = Math.pow(i, 2); double pow1 = Math.pow(x, i - 1.0); double term = pow * pow1;   if (Math.abs(term) < 0.0001) {   check = false; series = series + term; out.println("The sum of the series = " + series); } else { series = series + term; } }   } } }```

but now, the answer to the sum of the series is incorrect. :(
• November 29th, 2011, 12:13 PM
KevinWorkman
Re: Loops & Series Question
What do you mean by incorrect? How off is it?
• November 29th, 2011, 12:26 PM
nuttaay
Re: Loops & Series Question
COMMAND PROMPT:

Enter the value of x ... 0.001254
The Oracle\'s answer is: "1.005030184256893"
The sum of the series = 0.0

From my textbook, it says the answer for the sum of the series should be : 1.005016
• November 29th, 2011, 12:46 PM
KevinWorkman
Re: Loops & Series Question