-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoylution.java
More file actions
84 lines (74 loc) · 2.67 KB
/
Soylution.java
File metadata and controls
84 lines (74 loc) · 2.67 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
public class Soylution {
public static void main(String[] args) {
ArrayList<Values> poly = new ArrayList<Values>();
boolean done = false;
Scanner scan = new Scanner(System.in);
while(!done) {
try {
System.out.print("Coefficient = ");
double a = Double.parseDouble(scan.nextLine());
System.out.print("Exponent = ");
int n = Integer.parseInt(scan.nextLine());
System.out.print("Done (y/n) = ");
String s = scan.nextLine().trim().toLowerCase();
if(s.equals("y")) {
done = true;
}
poly.add(new Values(a,n));
}
catch(NumberFormatException nfe) {
System.err.println("You inputed your stuff wrong. Plz " +
"input your last value again.");
}
}
System.out.println();
System.out.println();
Polynomial p = new Polynomial(poly);
System.out.println("Polynomial = ");
for(Values v : p.getValues()) {
System.out.print(v.getCoefficient() + "x^" + v.getExponent() + " ");
}
System.out.println();
System.out.println("Finding possible zeros:");
ArrayList<Double> ps =findPossibleZeros(p, 1000000);
for(Double i : ps) {
//System.out.println("Trying x = " + i);
Double solution = halley(p,i);
//3.14159265358979323846...
if(solution.isNaN() || solution.isInfinite()) {
continue;
}
else {
if(Math.abs(p.evaulate(solution)) < 0.001) {
System.out.println(solution);
}
}
}
}
public static ArrayList<Double> findPossibleZeros(Polynomial p,
double range) {
ArrayList<Double> possibleSolutions = new ArrayList<Double>();
double a = -range;
while(a < range) {
double sol1 = Math.abs(p.evaulate(a));
if(sol1 <= 2) {
possibleSolutions.add(sol1);
}
a += .1;
}
return possibleSolutions;
}
public static double halley(Polynomial p, double guess) {
Polynomial dp = p.derivative();
Polynomial d2p = dp.derivative();
double valP = p.evaulate(guess);
while(Math.abs(valP) > 0.001) {
double valDP = dp.evaulate(guess);
double valD2P = d2p.evaulate(guess);
guess = guess - (2*valP*valDP)/(2*valD2P*valD2P-valP*valD2P);
valP = p.evaulate(guess);
}
return guess;
}
}