Skip to content

Instantly share code, notes, and snippets.

@314maro
Created April 15, 2015 14:56
Show Gist options
  • Save 314maro/7c72ed096a2a118f8988 to your computer and use it in GitHub Desktop.
Save 314maro/7c72ed096a2a118f8988 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<script>
function rungekutta(n,xn,f0,f1,x0,y00,y10) {
var h = (xn - x0) / n;
var xi = x0, obj = [y00,y10];
function step(x,obj) {
var y0 = obj[0], y1 = obj[1];
var k01 = h*f0(x, y0, y1),
k11 = h*f1(x, y0, y1),
k02 = h*f0(x+h/2, y0+k01/2, y1+k11/2),
k12 = h*f1(x+h/2, y0+k01/2, y1+k11/2),
k03 = h*f0(x+h/2, y0+k02/2, y1+k12/2),
k13 = h*f1(x+h/2, y0+k02/2, y1+k12/2),
k04 = h*f0(x+h, y0+k03, y1+k13),
k14 = h*f1(x+h, y0+k03, y1+k13);
obj[0] += (k01+2*k02+2*k03+k04)/6;
obj[1] += (k11+2*k12+2*k13+k14)/6;
}
for (var i = 0; i < n; xi = x0 + h*++i) {
step(xi,obj);
}
return obj;
}
function solve() {
var elem0 = document.getElementById("result0"),
elem1 = document.getElementById("result1"),
n = document.getElementById("precision").value,
x = document.getElementById("x").value,
f0str = document.getElementById("function0").value,
f1str = document.getElementById("function1").value,
x0 = document.getElementById("x0").value,
y00 = document.getElementById("y00").value,
y10 = document.getElementById("y10").value;
var f0 = new Function("x","y0","y1",f0str),
f1 = new Function("x","y0","y1",f1str);
var res = rungekutta(+n, +x, f0, f1, +x0, +y00, +y10);
elem0.value = res[0];
elem1.value = res[1];
}
var dict = {
// hoge: { xn: , f0: , f1: , x0: , y00: , y10: }
nothing: {},
cos1: { xn: 1, f0: "return y1", f1: "return -y0", x0: 0, y00: 1, y10: 0 },
second: { f0: "return y1", f1: "/* y0''(x,y0,y0') */" }
}
window.onload = function () {
var ex = document.getElementById("examples");
for (var i in dict) {
var opt = document.createElement("option");
opt.value = i; opt.textContent = i;
ex.appendChild(opt);
}
}
function selectExample(ev) {
var ex = dict[ev.target.value];
document.getElementById("x").value = ex.xn || null;
document.getElementById("function0").value = ex.f0 || null;
document.getElementById("function1").value = ex.f1 || null;
document.getElementById("x0").value = ex.x0 || null;
document.getElementById("y00").value = ex.y00 || null;
document.getElementById("y10").value = ex.y10 || null;
}
</script>
</head>
<body>
<p>precision is <input id="precision" value="100"></input></p>
<p>dy0/dx = <input id="function0"></input></p>
<p>dy1/dx = <input id="function1"></input></p>
<p>y0(<input id="x0"></input>)
= <input id="y00"></input></p>
<p>y1( )
= <input id="y10"></input></p>
<button onclick="solve()">Solve</button>
<p>y0(<input id="x"></input>)
= <input type="text" id="result0" size="25" readonly></input></p>
<p>y1( )
= <input type="text" id="result1" size="25" readonly></input></p>
<p>
you can choose example:
<select id="examples" onchange="selectExample(event)">
</select>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment