-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTrigonometricSeries.java
More file actions
70 lines (60 loc) · 2.27 KB
/
TrigonometricSeries.java
File metadata and controls
70 lines (60 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Write a method to compute sin(x) and cos(x) using the following series expansion,
* in a class called TrigonometricSeries. The headers of the methods are:
*
* public static double sin(double x, int numTerms) // x in radians
* public static double cos(double x, int numTerms)
*
* ExerciseBasics_TrigonometricSeries.png
*
* Compare the values computed using the series with the JDK methods
* Math.sin(), Math.cos() at x=0, π/6, π/4, π/3, π/2 using various numbers of terms.
*
* Hints: Avoid generating large numerator and denominator (which may cause
* arithmetic overflow, e.g., 13! is out of int range). Compute the terms as:
*
* ExerciseBasics_TrigonometricSeriesHint.png
*/
package javaexercises.difficult;
public class TrigonometricSeries {
public static void main(String[] args)
{
double x = Math.PI/6;
int numTerms = 10;
TrigonometricSeries aTrigonometricSeries = new TrigonometricSeries();
System.out.println("Calculated values:");
System.out.printf("sin(%1$d) = %2$f \n", (int)Math.round(x*180/Math.PI)
, aTrigonometricSeries.sin(x, numTerms));
System.out.printf("cos(%1$d) = %2$f \n", (int)Math.round(x*180/Math.PI)
, aTrigonometricSeries.cos(x, numTerms));
System.out.println("java.lang.Math values:");
System.out.printf("sin(%1$d) = %2$f \n", (int)Math.round(x*180/Math.PI)
, Math.sin(x));
System.out.printf("cos(%1$d) = %2$f \n", (int)Math.round(x*180/Math.PI)
, Math.cos(x));
}
private double calculateTerm(double x, int numTerms)
{
double term = 1D;
for (int i = numTerms; i > 0; i--) {
term *= x/i;
}
return term;
}
private double sin(double x, int numTerms)
{
double result = 0D;
for (int i = 0; i < numTerms; i++) {
result += (i%2 == 0 ? 1 : -1) * calculateTerm(x, (2*i+1));
}
return result;
}
private double cos(double x, int numTerms)
{
double result = 0D;
for (int i = 0; i < numTerms; i++) {
result += (i%2 == 0 ? 1 : -1) * calculateTerm(x, 2*i);
}
return result;
}
}