# calculate sin(x)

• November 6th, 2013, 09:33 AM
anon314
calculate sin(x)
How can I transform this code to calculate the sin(x) in the method?

public static double getCos(double degrees, int n) {
double cos = 0;
double numerator = 0;
int denominator = 0;
for (int i = 0; i <= n; i++, i++) {
numerator = getNumerator(degrees, i);
denominator = getFactorial(2 * i + 1);
cos += numerator / denominator;
• November 6th, 2013, 11:44 AM
Norm
Re: calculate sin(x)
Can you use the Math class's method?

Otherwise what is the algorithm for the computation? Where are you having problems coding it?

[code=java]
[/code]
to get highlighting and preserve formatting.
• November 6th, 2013, 11:55 AM
anon314
Re: calculate sin(x)
Code java:

```package cosx;   import java.util.Scanner;   public class cosx2 {   public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Enter the degree: "); double degree = input.nextDouble();   double radian = getRadian(degree);   calculateCos(input, radian,degree);   }     public static double getRadian(double x) { return x = (Math.PI/180)*x; }   private static void calculateCos(Scanner std, double rad, double deg) { System.out.print("How many terms do you want to calculate the cos: "); int terms = std.nextInt(); double top; double bottom; double sum=0;   for(int i = 0; i<= terms; ++i){ top = Math.pow(-1, i); bottom =1; int repeat = 2*i+1; for(int j = 1; j <= repeat; ++j){ bottom *= j; } if(i%2 == 0) sum += (top/bottom); else sum -= (top/bottom); } System.out.printf("The sin (%.1f", deg); System.out.printf(") is %.6f", sum); } }```

I cannot figure out how to input the function to calculate the cosine?
• November 6th, 2013, 11:58 AM
anon314
Re: calculate sin(x)
• November 6th, 2013, 12:19 PM
Norm
Re: calculate sin(x)
Quote:

how to input the function to calculate the cosine?
What do you mean by "input the function"?
Are you trying to type code for a method into an editor?
Are you trying to pass data as input to a method?

The attachment is useless. Too small.
• November 6th, 2013, 12:29 PM
anon314
Re: calculate sin(x)
If you click the image it will expand....
Code java:

```import java.util.Scanner;   public class cosx2 {   public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Enter the degree please: "); double degree = input.nextDouble();   double radian = getRadian(degree);   calculateCos(input, radian,degree);   }     public static double getRadian(double x) { return x = (Math.PI/180)*x; }   private static void calculateCos(Scanner std, double rad, double deg) { System.out.print("How many terms do you want to calculate the cos: "); int terms = std.nextInt(); double num; double den; double sum=0;   for(int i = 0; i<= terms; ++i){ num = Math.pow(-1, i); den = factorial(2*i); int repeat = 2*i+1; for(int j = 1; j <= repeat; ++j){ den *= j; } if(i%2 == 0) sum += (num/den); else sum -= (num/den); } System.out.printf("The cos (%.1f", deg); System.out.printf(") is %.6f", sum); System.out.println("The calcuated value of cos was " +sum+ " Howver the real value of cose was " + Math.cos(deg));   } }```
Attachment 2455
• November 6th, 2013, 12:48 PM
Norm
Re: calculate sin(x)
How are you debugging the code? I use println() statements to print out intermediate results to validate the code. Does the code compute each term in the series correctly? Print out the first few and manually verify that they are correct.

The code should include comments showing the series that is being computed.

It is impossible to copy the series shown in an image to be able to add it as comments to the code.