| 1 |
<html> |
| 2 |
<head> |
| 3 |
<title>JSXGraph example</title> |
| 4 |
<link rel="stylesheet" type="text/css" href="../distrib/jsxgraph.css" /> |
| 5 |
<script type="text/javascript" src="prototype.js"></script> |
| 6 |
<script type="text/javascript" src="../distrib/jsxgraphcore.js"></script> |
| 7 |
<script type="text/javascript" src="../src/Centroid.js"></script> |
| 8 |
<script type="text/javascript" src="../src/Triangle.js"></script> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<h2>Solving ODE numerically with explicit Runge-Kutta-methods</h2> |
| 12 |
<div style="width:800px"> |
| 13 |
<div id="jxgbox" class="jxgbox" style="width:600px; height:600px; float:left;"></div> |
| 14 |
</div> |
| 15 |
<script type="text/javascript"> |
| 16 |
/* <![CDATA[ */ |
| 17 |
|
| 18 |
(function () { |
| 19 |
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1.5, 1.5, 1.5, -1.5], axis: false, grid: false}), |
| 20 |
mu = 1.0/81.45, |
| 21 |
apolloPath, moonPath, |
| 22 |
moon, apollo, earth, |
| 23 |
ode = function () { |
| 24 |
var I = [17.066, 0], |
| 25 |
x0 = [0.994, 0, 0, -2.0015851063790825], |
| 26 |
N = 10000, |
| 27 |
data, dataX, dataY, i, |
| 28 |
|
| 29 |
f = function(t, x) { |
| 30 |
var m = 1.0/81.45, |
| 31 |
D1 = Math.sqrt(Math.pow((x[0]+m)*(x[0]+m)+x[2]*x[2],3)), |
| 32 |
D2 = Math.sqrt(Math.pow((x[0]-(1-m))*(x[0]-(1-m))+x[2]*x[2],3)), |
| 33 |
y = []; |
| 34 |
|
| 35 |
y[0] = x[1]; |
| 36 |
y[1] = x[0]+2*x[3]-(1-m)*(x[0]+m)/D1-m*(x[0]-(1-m))/D2; |
| 37 |
y[2] = x[3]; |
| 38 |
y[3] = x[2]-2*x[1]-(1-m)*x[2]/D1-m*x[2]/D2; |
| 39 |
|
| 40 |
return y; |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
data = JXG.Math.Numerics.rungeKutta('rk4', x0, I, N, f); |
| 45 |
|
| 46 |
dataX = []; |
| 47 |
dataY = []; |
| 48 |
for(i = 0; i < data.length; i++) { |
| 49 |
dataX[i] = data[i][0]; |
| 50 |
dataY[i] = data[i][2]; |
| 51 |
} |
| 52 |
|
| 53 |
return [dataX, dataY]; |
| 54 |
}, |
| 55 |
circle = function (radius, points) { |
| 56 |
var dataX = [], dataY = [], i; |
| 57 |
|
| 58 |
for (i = 0; i < points; i++) { |
| 59 |
dataX[i] = radius*Math.cos(i/(points-1)*2*Math.PI); |
| 60 |
dataY[i] = -radius*Math.sin(i/(points-1)*2*Math.PI); |
| 61 |
} |
| 62 |
|
| 63 |
return [dataX, dataY]; |
| 64 |
}; |
| 65 |
|
| 66 |
earth = board.create('point', [0, 0], { |
| 67 |
withLabel: false, |
| 68 |
strokeColor: '#4096EE', |
| 69 |
fillColor: '#4096EE', |
| 70 |
size: 40 |
| 71 |
}); |
| 72 |
|
| 73 |
apolloPath = board.createElement('curve', ode(), { |
| 74 |
strokeColor: 'red', |
| 75 |
strokeWidth: 2, |
| 76 |
visible: false |
| 77 |
}); |
| 78 |
|
| 79 |
moonPath = board.create('curve', circle(1-mu, apolloPath.dataX.length), { |
| 80 |
strokeColor: 'green', |
| 81 |
strokeWidth: 2, |
| 82 |
visible: false |
| 83 |
}); |
| 84 |
|
| 85 |
moon = board.create('point', [1, 0], { |
| 86 |
withLabel: false, |
| 87 |
strokeColor: 'gray', |
| 88 |
fillColor: 'gray', |
| 89 |
size: 10 |
| 90 |
}); |
| 91 |
|
| 92 |
apollo = board.create('point', [1, 0], { |
| 93 |
withLabel: false, |
| 94 |
strokeColor: 'white', |
| 95 |
fillColor: 'white', |
| 96 |
size: 2, |
| 97 |
face: '<>' |
| 98 |
}); |
| 99 |
|
| 100 |
moon.moveAlong(function (i) { |
| 101 |
return [moonPath.dataX[i%moonPath.dataX.length], moonPath.dataY[i%moonPath.dataY.length]]; |
| 102 |
}, 2000); |
| 103 |
|
| 104 |
apollo.moveAlong(function (i) { |
| 105 |
return [apolloPath.dataX[i%apolloPath.dataX.length], apolloPath.dataY[i%apolloPath.dataY.length]]; |
| 106 |
}, 2000); |
| 107 |
})(); |
| 108 |
|
| 109 |
/* ]]> */ |
| 110 |
</script> |
| 111 |
<a href="#" onclick="createCurve();">Solve example ODE</a><br /> |
| 112 |
<div id="debug" style="display:block;"></div> |
| 113 |
</body> |
| 114 |
</html> |