-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLagrangeSolver.java
More file actions
67 lines (55 loc) · 1.94 KB
/
Copy pathLagrangeSolver.java
File metadata and controls
67 lines (55 loc) · 1.94 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
public class LagrangeSolver {
int x1;
int y1;
int x2;
int y2;
int x3;
int y3;
Frac factP1;
Frac factP2;
Frac factP3;
public LagrangeSolver(int x1, int y1, int x2, int y2, int x3, int y3) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
private Frac coeff1() {
factP1 = new Frac(y1, ((x1-x2) * (x1-x3)));
factP2 = new Frac(y2, ((x2-x1) * (x2-x3)));
factP3 = new Frac(y3, ((x3-x1) * (x3-x2)));
Frac coeff1 = new Frac(factP1.getNumerator(), factP1.getDenominator());
coeff1.addFrac(factP2);
coeff1.addFrac(factP3);
return coeff1;
}
private Frac coeff2() {
Frac temp = new Frac(factP1.getNumerator(), factP1.getDenominator());
temp.multByInt((-x2-x3));
Frac coeff2 = new Frac(temp.getNumerator(), temp.getDenominator());
temp = new Frac(factP2.getNumerator(), factP2.getDenominator());
temp.multByInt((-x1-x3));
coeff2.addFrac(temp);
temp = new Frac(factP3.getNumerator(), factP3.getDenominator());
temp.multByInt((-x1-x2));
coeff2.addFrac(temp);
return coeff2;
}
private Frac coeff3() {
Frac temp = new Frac(factP1.getNumerator(), factP1.getDenominator());
temp.multByInt((x2*x3));
Frac coeff3 = new Frac(temp.getNumerator(), temp.getDenominator());
temp = new Frac(factP2.getNumerator(), factP2.getDenominator());
temp.multByInt((x1*x3));
coeff3.addFrac(temp);
temp = new Frac(factP3.getNumerator(), factP3.getDenominator());
temp.multByInt((x1*x2));
coeff3.addFrac(temp);
return coeff3;
}
public String toString() {
return "p(x) = "+coeff1()+"x^2 + "+coeff2()+"x + "+coeff3();
}
}