Last active
August 29, 2015 14:18
-
-
Save 314maro/4b287658864845eed707 to your computer and use it in GitHub Desktop.
理屈わからんが解けてるようだ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<script> | |
function rungekutta(n,xn,f,x0,y0) { | |
var h = (xn - x0) / n; | |
var xi = x0, yi = y0; | |
function step(x,y) { | |
var k1 = f(x, y), | |
k2 = f(x+h/2, y+h*k1/2), | |
k3 = f(x+h/2, y+h*k2/2), | |
k4 = f(x+h, y+h*k3); | |
return y + h*(k1+2*k2+2*k3+k4)/6; | |
} | |
for (var i = 0; i < n; xi = x0 + h*++i) { | |
yi = step(xi,yi); | |
} | |
return yi; | |
} | |
function solve() { | |
var elem = document.getElementById("result"), | |
n = document.getElementById("precision").value, | |
x = document.getElementById("x").value, | |
f = document.getElementById("function").value, | |
x0 = document.getElementById("x0").value, | |
y0 = document.getElementById("y0").value; | |
var res = rungekutta(+n, +x, new Function("x","y",f), +x0, +y0); | |
elem.value = res; | |
} | |
var dict = { | |
// hoge: { xn: , f: , x0: , y0: }, | |
nothing: { xn: null, f: null, x0: null, y0: null }, | |
exp: { xn: 1, f: "return y", x0: 0, y0: 1 }, | |
arctan: { xn: 1, f: "return 4/(1+x*x)", x0: 0, y0: 0 } | |
} | |
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; | |
document.getElementById("function").value = ex.f; | |
document.getElementById("x0").value = ex.x0; | |
document.getElementById("y0").value = ex.y0; | |
} | |
</script> | |
</head> | |
<body> | |
<p>precision is <input id="precision" value="100"></input></p> | |
<p>dy/dx = <input id="function"></input></p> | |
<p>y(<input id="x0"></input>) | |
= <input id="y0"></input></p> | |
<button onclick="solve()">Solve</button> | |
<p>y(<input id="x"></input>) | |
= <input type="text" id="result" 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