I'm trying to make a program that will compare the results in calculating the Cosine with powerseries method and Math.cos method. This is what i have.

``````import java.util.*;

class Cosine{

public static void main(String[] args){
int degrees = 0;
Scanner myScanner = new Scanner(System.in);

while(true){
System.out.println("Enter some integer value of degrees (-1 to quit)");
degrees = myScanner.nextInt();
if (degrees == -1)
{
break;
}
System.out.println("\nCosine calculation for "+ degrees +" degrees using...");
System.out.println("      ...Power Series: "+ myCos(degrees * Math.PI/180));// convert degrees to radians
System.out.println("        ...Math class: "+ Math.cos(degrees * Math.PI/180));//check result vs Math class
}
System.out.println("Have a nice day!");
}

private static double myCos(double radians) {
int expFact = 2;//the exponent & factorial term
double negationFactor = 1.0;// used to alternate between addition and subtraction of terms
double powerSeries = 0.0;//accumulator for power series
double factorialResult = 0.0;//result of the factorial

while (factorialResult < Double.POSITIVE_INFINITY) {// stop when factorialResult overflow is reached

factorialResult = factorial(expFact);// calculate the factorial

powerSeries = 1 +(negationFactor * (Math.pow(radians, expFact))/factorialResult);// the next statement calculates power series

negationFactor *= -1.0;// the negation factor alternates + - + - +.....

expFact +=2;//increment exponent and factorial term by two
}
return radians - powerSeries;//subtraction, as specified in the formula
}
//calculates the factorial
private static double factorial(int x) {
double f = (double) x;
for (int i= x-1; i>0; i--)
{
f = f*(double)i;
}
return f;
}

}``````

## All 14 Replies

Are you sure you have the right formula?
Also shouldn't you have this:

``powerSeries [B]+=[/B] 1 +(negationFactor * (Math.pow(radians, expFact))/factorialResult);``

The formula for Cosine in my java book is

cos x = 1 - x2/2! + x4/4! - x6/6! + . . .

x2 is x to the power of 2
x4 is x to the power of 4
x6 is x to the power of 6
! means factorial

Then why this:

``powerSeries = 1 + ....``

and this:

``radians - powerSeries``

Just calculate this in the while loop: `(- x2/2!)` `(x4/4!)` `(-x6/6!)` Then subtract from 1

that's why i have

``negationFactor *= -1.0;// the negation factor alternates + - + - +.....``

it alternates the negative and positive and the return statement, the inputted number is converted to radians then subtracted by the power series.

The calculation for the power series evaluates each term adds 1 and then assigns this to the variable, overwriting any values from previous iterations. The final loop will then assign only 1 plus the last term. You need to accumulate the values from each term as it is caluclated. Hence;

powerseries+=Math.pow(degress,exp)...

If you inititiate power series to 1 before the while loop starts this should do it.

I'm not following, what do you mean? The only problem with this program is the cosine formula.

At line 33 you have the assignment:
powerSeries = 1 +(negationFactor * (Math.pow(radians, expFact))/factorialResult)

This sets the variable powerSeries to a value of 1 plus the current calculated term in the loop, but the Cosine formula requires a total of such terms. You need to repeatedly add each term to a cumulative total as in:

To use this you must first initialise the variable so you need a line:
powerSeries=1;
before starting the while loop

The final result will then be 1 plus(or minus) each term calculated within the loop!

i tried but still gives the wrong answer.

Post the current code

Thought of this late last night!

The first term of the power series is 1. (x^0/0!) The next term is negative (-x^2/2!).
In the program the swapped value negation factor is initialised to 1, Positive, and used to multiply the rest of the term. This needs to be initialised to -1 and swapped from there on!

Further, you finally return the value of power series but subtract the degrees value from it. The value of the cosine will be just the result collected in the power series.
Hence

``return powerseries``
``````import java.util.*;

class Cosine{

public static void main(String[] args){
int degrees = 0;
Scanner myScanner = new Scanner(System.in);

while(true){
System.out.println("Enter some integer value of degrees (-1 to quit)");
degrees = myScanner.nextInt();
if (degrees == -1)
{
break;
}
System.out.println("\nCosine calculation for "+ degrees +" degrees using...");
System.out.println("      ...Power Series: "+ myCos(degrees * Math.PI/180));// convert degrees to radians
System.out.println("        ...Math class: "+ Math.cos(degrees * Math.PI/180));//check result vs Math class
}
System.out.println("Have a nice day!");
}

private static double myCos(double radians) {
int expFact = 2;//the exponent & factorial term
double negationFactor = 1.0;// used to alternate between addition and subtraction of terms
double powerSeries = 1.0;//accumulator for power series
double factorialResult = 0.0;//result of the factorial

while (factorialResult < Double.POSITIVE_INFINITY) {// stop when factorialResult overflow is reached

factorialResult = factorial(expFact);// calculate the factorial

powerSeries += 1 +(negationFactor * (Math.pow(radians, expFact))/factorialResult);// the next statement calculates power series

negationFactor *= -1.0;// the negation factor alternates + - + - +.....

expFact +=2;//increment exponent and factorial term by two
}
return radians - powerSeries;//subtraction, as specified in the formula
}

private static double factorial(int x) {
double f = (double) x;
for (int i= x-1; i>0; i--)
{
f = f*(double)i;
}
return f;
}

}``````

can anyone solve this problem?

can anyone solve this problem?

Yes we can and so can you; after you've been told how in the previous posts.

what they told me was wrong, didn't get the right answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.