-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathscript.js
170 lines (130 loc) · 4.68 KB
/
script.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
document.getElementById("myForm").addEventListener("submit", function (event) {
event.preventDefault();
var m = document.getElementById(`method`).value;
console.log(m)
if (m == "Euler Method") {
solveEuler();
}
else if (m == "Improved Euler Method") {
solveImprovedEuler();
}
else if (m == "R-K Method") {
solveRK();
}
});
function solveEuler() {
const myFunction = document.getElementById("functionInput").value;
const x0 = parseFloat(document.getElementById("x0Input").value);
const y0 = parseFloat(document.getElementById("y0Input").value);
const h = parseFloat(document.getElementById("stepHeightInput").value);
const n = parseFloat(document.getElementById("nInput").value);
// Define the derivative function (dy/dx)
function derivative(x, y) {
if (myFunction.includes("sqrt")) {
const func = new Function("x", "y", `return Math.sqrt(y)`);
return func(x, y);
} else if (myFunction.includes("log")) {
const func = new Function("x", "y", `return Math.log(y)`);
return func(x, y);
} else {
const func = new Function("x", "y", `return (${myFunction})`);
return func(x, y);
}
}
// Euler's method for solving the initial value problem
function eulerMethod(h, x0, y0, xTarget) {
let x = x0;
let y = y0;
let results = "";
while (x <= xTarget) {
const slope = derivative(x, y);
results += `x = ${x.toFixed(4)}, y = ${y.toFixed(4)}<br>`;
y += h * slope;
x += h;
x = parseFloat(x.toFixed(10)); // Handle floating-point precision error
}
return results;
}
const xTarget = n;
const results = eulerMethod(h, x0, y0, xTarget);
document.getElementById("output").innerHTML = results;
}
function solveImprovedEuler() {
const myFunction = document.getElementById("functionInput").value;
const x0 = parseFloat(document.getElementById("x0Input").value);
const y0 = parseFloat(document.getElementById("y0Input").value);
const h = parseFloat(document.getElementById("stepHeightInput").value);
const n = parseFloat(document.getElementById("nInput").value);
// Define the derivative function (dy/dx)
function derivative(x, y) {
if (myFunction.includes("sqrt")) {
const func = new Function("x", "y", `return Math.sqrt(y)`);
return func(x, y);
} else if (myFunction.includes("log")) {
const func = new Function("x", "y", `return Math.log(y)`);
return func(x, y);
} else {
const func = new Function("x", "y", `return (${myFunction})`);
return func(x, y);
}
}
function improvedEulerMethod(h, x0, y0, xTarget) {
let x = x0;
let y = y0;
let results = "";
while (x <= xTarget) {
const slope = derivative(x, y);
results += `x = ${x.toFixed(4)}, y = ${y.toFixed(4)}<br>`;
const predictor = y + h * slope;
const correctedSlope = derivative(x + h, predictor);
y += (h / 2) * (slope + correctedSlope);
x += h;
x = parseFloat(x.toFixed(100)); // Handle floating-point precision error
}
return results;
}
const xTarget = n;
const results = improvedEulerMethod(h, x0, y0, xTarget);
document.getElementById("output").innerHTML = results;
}
// Runge-Kutta Method
function solveRK() {
const myFunction = document.getElementById("functionInput").value;
const x0 = parseFloat(document.getElementById("x0Input").value);
const y0 = parseFloat(document.getElementById("y0Input").value);
const h = parseFloat(document.getElementById("stepHeightInput").value);
const n = parseFloat(document.getElementById("nInput").value);
// Define the derivative function (dy/dx)
function derivative(x, y) {
if (myFunction.includes("sqrt")) {
const func = new Function("x", "y", `return Math.sqrt(y)`);
return func(x, y);
} else if (myFunction.includes("log")) {
const func = new Function("x", "y", `return Math.log(y)`);
return func(x, y);
} else {
const func = new Function("x", "y", `return (${myFunction})`);
return func(x, y);
}
}
function rkMethod(h, x0, y0, xTarget) {
let x = x0;
let y = y0;
let results = "";
while (x <= xTarget) {
const k1 = h * derivative(x, y);
const k2 = h * derivative(x + h / 2, y + k1 / 2);
const k3 = h * derivative(x + h / 2, y + k2 / 2);
const k4 = h * derivative(x + h, y + k3);
const k = (k1 + 2 * k2 + 2 * k3 + k4) / 6;
results += `x = ${x.toFixed(4)}, y = ${y.toFixed(4)}<br>`;
y += k;
x += h;
x = parseFloat(x.toFixed(100)); // Handle floating-point precision error
}
return results;
}
const xTarget = n;
const results = rkMethod(h, x0, y0, xTarget);
document.getElementById("output").innerHTML = results;
}