-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
137 lines (84 loc) · 2.93 KB
/
Main.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.Scanner;
public class Main {
public static void main (String [] args){
Scanner in = new Scanner(System.in);
System.out.print("N = ");
double N = in.nextDouble();
System.out.print("Left, Right, or Midpoint sum? ('L' or 'R' or 'M'): ");
String next = in.next();
if(next.equals("L")){
System.out.print("LEFT SUM:" + "\n" + "\n" + getLeftSum(N));
}
else if(next.equals("R")){
System.out.print("RIGHT SUM:" + "\n" + "\n" + getRightSum(N));
}
else{
System.out.print("MIDPOINT SUM:" + "\n" + "\n" + getMidSum(N)); }
}
private static double getLeftSum(double N){
double e = Math.E;
double a = 0;
double b = 2;
double deltaX = 2/N;
System.out.println("deltaX = " + deltaX);
//f(a)
double total = 1.0 + Math.pow(e, getExponent(deltaX, 1));;
System.out.println("current total = e^0 + e^" + getExponent(deltaX, 1) + " = " + total);
for(int i = 2; i < N-1; i++){
//repeat until N-1
total += Math.pow(e, getExponent(deltaX, i));
System.out.println("current total = e^" + getExponent(deltaX, i) + " = " + total);
}
// add last f(b-deltaX)
total += Math.pow(e, getExponent(b-deltaX, 1));
System.out.println("current total = e^" + getExponent(b-deltaX, 1) + " = " + total);
total *= deltaX;
System.out.println("after multiplying everything by the outside by " + deltaX);
System.out.println("total is approx: " + total + "\n" + "\n");
return total;
}
private static double getRightSum(double N){
double e = Math.E;
double a = 0;
double b = 2;
double deltaX = 2/N;
System.out.println("deltaX = " + deltaX);
//f(b)
double total = Math.pow(e, -2.0);
System.out.println("current total = e^(-2.0)" + " = " + total);
for(int i = 1; i < N; i++){
//repeat until N-1
total += Math.pow(e, getExponent(deltaX, i));
System.out.println("current total = e^" + getExponent(deltaX, i) + " = " + total);
}
total *= deltaX;
System.out.println("after multiplying everything by the outside by " + deltaX);
System.out.println("total is approx: " + total + "\n" + "\n");
return total;
}
private static double getMidSum(double N){
double e = Math.E;
double a = 0;
double b = 2;
double deltaX = 2/N;
System.out.println("deltaX = " + deltaX);
double total = 0.0;
for(int i = 1; i < 2*N; i+=2){
double num = i * deltaX;
double denom = 2.0;
double newExp = num/denom;
total += Math.pow(e, getExponent(newExp, 1));
System.out.println("current total = e^" + getExponent(newExp, i) + " = " + total);
}
total *= deltaX;
System.out.println("after multiplying everything by the outside by " + deltaX);
System.out.println("total is approx: " + total + "\n" + "\n");
return total;
}
private static double getExponent(double deltaX, int i){
double num = -(Math.pow(i*deltaX, 2.0));
double denom = 2.0;
//return -(x^2)/2
return num/denom;
}
}