-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiemannSums.java
More file actions
69 lines (52 loc) · 1.98 KB
/
RiemannSums.java
File metadata and controls
69 lines (52 loc) · 1.98 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
//2008 - 3
//InputFormatExceptions all over the place. It works when input is correct, and that's good enough for me.
import java.util.*;
import java.lang.Math;
public class RiemannSums {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner inputString = new Scanner(System.in);
int times = input.nextInt();
for(int i = 0; i < times; i++){
String rawIn = inputString.nextLine();
String[] rawDoubs = rawIn.split(" ");
double a = Double.parseDouble(rawDoubs[0]);
double b = Double.parseDouble(rawDoubs[1]);
int rawM = Integer.parseInt(rawDoubs[2]);
int k = Integer.parseInt(rawDoubs[3]);
ArrayList<Double> coefs = new ArrayList<Double>();
for(int x = 4; x <= k + 4; x++){
coefs.add(Double.parseDouble(rawDoubs[x]));
}
//Maths and logics
double m = (double) rawM;
double rectWidth = (b - a)/m;
double totalArea = 0.0;//Answer
for(double x = 0; x < m; x ++){
double leftXPoint = a + (rectWidth * x);
//Find height, because we know width
double yCoordinate = 0.0;
for(int j = 0; j < coefs.size(); j++){
double temp = coefs.get(j);
temp *= Math.pow(leftXPoint, j);
yCoordinate += temp;
}
//Have y, do stuff
double area = yCoordinate * rectWidth;
totalArea += area;
}
//OUTPUT TIME
System.out.print("Estimated area under y = " + coefs.get(0) + "x^0");
for(int x = 1; x < coefs.size(); x++){
if(coefs.get(x) < 0.0){
double posX = coefs.get(x) * -1;
System.out.print(" - " + posX + "x^" + x);
} else {
System.out.print(" + " + coefs.get(x) + "x^" + x);
}
}
System.out.println();
System.out.println(" between x = " + a + " and x = " + b + " using " + rawM + " rectangles is " + totalArea);
}
}
}