-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
64 lines (50 loc) · 1.51 KB
/
Copy pathTest.java
File metadata and controls
64 lines (50 loc) · 1.51 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
public class Tesdddt {
public static void main (String args[]){
double x = 3.0;
double result = x;
double term = x;
for (int i = 3; i<=41;i+=2) {
//y = (x*x)/((i-1)*i);
term *= (-x*x)/((i-1)*i);
result+=term;
}
//System.out.println("The sin of "+x+" is equal to" "+);
System.out.println("The sin of "+x+" is equal to: "+result);
}
}
/*
/*************************************************************************
* Compilation: javac Sin.java
* Execution: java Sin x
*
* Prints out sin(x) using Taylor expansion.
*
* sin x = x - x^3/3! + x^5/5! - x^7/7! + ...
*
* % java sin java Sin 0.523598775598299
* 0.5000000000000002
*
* Note: sin(pi/6) = sin(0.523598775598299...) = 1/2
*
* We use identity sin(x) = sin(x + 2 PI) to pre-process
* x to be between -2 PI and 2 PI. Yes, % works with doubles!
*
*************************************************************************/
/*
public class Sin {
public static void main(String[] args) {
double x = 1.0;//Double.parseDouble(args[0]);
// convert x to an angle between -2 PI and 2 PI
x = x % (2 * Math.PI);
// compute the Taylor series approximation
double term = 1.0; // ith term = x^i / i!
double sum = 0.0; // sum of first i terms in taylor series
for (int i = 1; term != 0.0; i++) {
term *= (x / i);
if (i % 4 == 1) sum += term;
if (i % 4 == 3) sum -= term;
}
System.out.println(sum);
}
}
*/