-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolveEquation.js
176 lines (156 loc) · 8.19 KB
/
SolveEquation.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class SolveEquation {
constructor(size) {
printHeading("Solving Linear Equation", true, "h1");
printHeading("Ly = b", true, "h1");
this.size = size;
this.y = [
["y<sub>1</sub>", "=" , bMatrix[0]],
["y<sub>2</sub>", "=" , bMatrix[1]],
["y<sub>3</sub>", "=" , bMatrix[2]]
]
this.printSize([["","L",""]], true);
this.printSize(LSolved, false);
this.printSize([["y","=","b"]], true);
this.printSize(this.y, false);
math.config({number: 'Fraction'});
this.y1 = math.fraction(math.divide(bMatrix[0],LSolved[0][0]));
this.y2 = math.fraction(math.divide(math.subtract(bMatrix[1],math.multiply(LSolved[1][0],this.y1)),LSolved[1][1]));
this.y3 = math.fraction(math.divide(math.subtract(math.subtract(bMatrix[2],math.multiply(LSolved[2][0],this.y1)), math.multiply(LSolved[2][1],this.y2)),LSolved[2][2]));
this.ForwardSolution = [
[
[
LSolved[0][0]+"y<sub>1</sub> = " + bMatrix[0],
"y<sub>1</sub> = " + this.y1.toFraction(),
LSolved[1][0]+"y<sub>1</sub> + "+LSolved[1][1]+"y<sub>2</sub> = " + bMatrix[1],
LSolved[2][0]+"y<sub>1</sub> + "+ LSolved[2][1]+"y<sub>2</sub> + "+LSolved[2][2]+"y<sub>3</sub> = " + bMatrix[2]
]
],
[
[
LSolved[1][0]+"y<sub>1</sub> + "+LSolved[1][1]+"y<sub>2</sub> = " + bMatrix[1],
`${LSolved[1][0]}(${this.y1.toFraction()}) + ${LSolved[1][1]}y<sub>2</sub> = ` + bMatrix[1],
`${math.multiply(LSolved[1][0],this.y1).toFraction()} + y<sub>2</sub> = ` + bMatrix[1],
`${LSolved[1][1]}y<sub>2</sub> = ${bMatrix[1]} - ${math.multiply(LSolved[1][0],this.y1).toFraction()}`,
`${LSolved[1][1]}y<sub>2</sub> = ${math.subtract(bMatrix[1],math.multiply(LSolved[1][0],this.y1)).toFraction()}`,
`y<sub>2</sub> = ${this.y2.toFraction()}`
],
],
[
[
LSolved[2][0]+"y<sub>1</sub> + "+ LSolved[2][1]+"y<sub>2</sub> + "+LSolved[2][2]+"y<sub>3</sub> = " + bMatrix[2],
`${LSolved[2][0]}(${this.y1.toFraction()}) + ${LSolved[2][1]}(${this.y2.toFraction()}) + ${LSolved[2][2]}y<sub>3</sub> = ${bMatrix[2]}`,
`${math.multiply(LSolved[2][0],this.y1).toFraction()} + ${math.multiply(LSolved[2][1],this.y2).toFraction()} + ${LSolved[2][2]}y<sub>3</sub> = ${bMatrix[2]}`,
`${math.add(math.multiply(LSolved[2][0],this.y1),math.multiply(LSolved[2][1],this.y2)).toFraction()} + ${LSolved[2][2]}y<sub>3</sub> = ${bMatrix[2]}`,
`${LSolved[2][2]}y<sub>3</sub> = ${bMatrix[2]} - ${math.add(math.multiply(LSolved[2][0],this.y1),math.multiply(LSolved[2][1],this.y2)).toFraction()}`,
`${LSolved[2][2]}y<sub>3</sub> = ${math.subtract(math.subtract(bMatrix[2],math.multiply(LSolved[2][0],this.y1)), math.multiply(LSolved[2][1],this.y2)).toFraction()}`,
`y<sub>3</sub> = ${this.y3.toFraction()}`
],
]
]
for(let i=0; i<this.ForwardSolution.length; i++)
{
if(i >= this.size) break;
println("<hr>")
for(let j=0; j<this.ForwardSolution[i].length; j++)
{
if(j >= this.size) break;
for(let k=0; k<this.ForwardSolution[i][j].length; k++)
println(this.ForwardSolution[i][j][k]);
println("<br>")
}
}
printHeading("Ux = y", true, "h1");
this.x = [
["x<sub>1</sub>", "=" , this.y1],
["x<sub>2</sub>", "=" , this.y2],
["x<sub>3</sub>", "=" , this.y3]
]
this.printSize([["","U",""]], true);
this.printSize(USolved, false);
this.printSize([["x","=","y"]], true);
this.printSize(this.x, false);
this.x3 = math.fraction(math.divide(this.y3,USolved[2][2]));
this.x2 = math.fraction(math.divide(math.subtract(this.y2,math.multiply(USolved[1][2],this.x3)),USolved[1][1]));
this.x1 = math.fraction(math.divide(math.subtract(this.y1,math.add(math.multiply(USolved[0][1],this.x2),math.multiply(USolved[0][2],this.x3))), USolved[0][0]));
this.BackwardSolution = [
[
[
USolved[0][0]+"x<sub>1</sub> + "+ USolved[0][1]+"x<sub>2</sub> +"+USolved[0][2]+"x<sub>3</sub> = " + this.y1,
USolved[1][1]+"x<sub>2</sub> + "+ USolved[1][2]+"x<sub>3</sub> = " + this.y2,
USolved[2][2]+"x<sub>3</sub> = " + this.y3,
"x<sub>3</sub> = " + this.x3.toFraction(),
]
],
[
[
USolved[1][1]+"x<sub>2</sub> + "+ USolved[1][2]+"x<sub>3</sub> = " + this.y2,
`${USolved[1][1]}x<sub>2</sub> + ${USolved[1][2]}(${this.x3.toFraction()}) = ${this.y2}`,
`${USolved[1][1]}x<sub>2</sub> + ${math.fraction(math.multiply(USolved[1][2],this.x3)).toFraction()} = ${this.y2}`,
`${USolved[1][1]}x<sub>2</sub> = ${this.y2} - ${math.fraction(math.multiply(USolved[1][2],this.x3)).toFraction()}`,
`${USolved[1][1]}x<sub>2</sub> = ${math.fraction(math.subtract(this.y2,math.multiply(USolved[1][2],this.x3))).toFraction()}`,
`x<sub>2</sub> = ${this.x2.toFraction()}`,
],
],
[
[
USolved[0][0]+"x<sub>1</sub> + "+ USolved[0][1]+"x<sub>2</sub> +"+USolved[0][2]+"x<sub>3</sub> = " + this.y1.toFraction(),
`${USolved[0][0]}x<sub>1</sub> + ${USolved[0][1]}(${this.x2.toFraction()}) + ${USolved[0][2]}(${this.x3.toFraction()}) = ${this.y1.toFraction()}`,
`${USolved[0][0]}x<sub>1</sub> + ${math.multiply(USolved[0][1],this.x2).toFraction()} + ${math.multiply(USolved[0][2],this.x3).toFraction()} = ${this.y1.toFraction()}`,
`${USolved[0][0]}x<sub>1</sub> + ${math.add(math.multiply(USolved[0][1],this.x2),math.multiply(USolved[0][2],this.x3)).toFraction()} = ${this.y1.toFraction()}`,
`${USolved[0][0]}x<sub>1</sub> = ${this.y1.toFraction()} - ${math.add(math.multiply(USolved[0][1],this.x2),math.multiply(USolved[0][2],this.x3)).toFraction()}`,
`${USolved[0][0]}x<sub>1</sub> = ${math.subtract(this.y1,math.add(math.multiply(USolved[0][1],this.x2),math.multiply(USolved[0][2],this.x3))).toFraction()}`,
`x<sub>1</sub> = ${this.x1.toFraction()}`,
],
]
]
for(let i=0; i<this.BackwardSolution.length; i++)
{
if(i >= this.size) break;
println("<hr>")
for(let j=0; j<this.BackwardSolution[i].length; j++)
{
if(j >= this.size) break;
for(let k=0; k<this.BackwardSolution[i][j].length; k++)
println(this.BackwardSolution[i][j][k]);
println("<br>")
}
}
printHeading("The solution is", true, "h3");
createTable(["x"], true);
addTableRow([this.x1.toFraction(), this.x2.toFraction(), this.x3.toFraction()], false);
}
printSize(data, heading = false)
{
for(let i=0; i<data.length; i++)
{
if(i >= this.size) break;
var dis = []
for(let j=0; j<data[i].length; j++)
{
if(j >= this.size) break;
dis[j] = data[i][j];
}
if(heading)
createTable(dis);
else
addTableRow(dis);
}
}
printWithData(data, heading = false)
{
for(let i=0; i<data.length; i++)
{
if(i >= this.size) break;
var dis = []
for(let j=0; j<data[i].length; j++)
{
if(j >= this.size) break;
dis[j] = data[i][j]+" = "+ this.data[i][j];
}
if(heading)
createTable(dis);
else
addTableRow(dis);
}
}
}