File: ode.html

package info (click to toggle)
jsxgraph 1.3.5+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 26,044 kB
  • sloc: xml: 5,869; java: 1,072; python: 747; php: 192; makefile: 146; sh: 47
file content (114 lines) | stat: -rw-r--r-- 3,804 bytes parent folder | download | duplicates (3)
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
<html>
<head>
   <title>JSXGraph example</title>
   <link rel="stylesheet" type="text/css" href="../distrib/jsxgraph.css" />
   <script type="text/javascript" src="/javascript/prototype/prototype.js"></script>
   <script type="text/javascript" src="../src/loadjsxgraph.js"></script>
   <script type="text/javascript" src="../src/Centroid.js"></script>
   <script type="text/javascript" src="../src/Triangle.js"></script>
</head>
<body>
<h2>Solving ODE numerically with explicit Runge-Kutta-methods</h2>
<div style="width:800px">
  <div id="jxgbox" class="jxgbox" style="width:600px; height:600px; float:left;"></div>
</div>
   <script type="text/javascript">
    /* <![CDATA[ */

    (function () {
        var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1.5, 1.5, 1.5, -1.5], axis: false, grid: false}),
            mu = 1.0/81.45,
            apolloPath, moonPath,
            moon, apollo, earth,
            ode = function () {
                var I = [17.066, 0],
                    x0 = [0.994, 0, 0, -2.0015851063790825],
                    N = 10000,
                    data, dataX, dataY, i,

                    f = function(t, x) {
                        var m  = 1.0/81.45,
                            D1 = Math.sqrt(Math.pow((x[0]+m)*(x[0]+m)+x[2]*x[2],3)),
                            D2 = Math.sqrt(Math.pow((x[0]-(1-m))*(x[0]-(1-m))+x[2]*x[2],3)),
                            y = [];

                        y[0] = x[1];
                        y[1] = x[0]+2*x[3]-(1-m)*(x[0]+m)/D1-m*(x[0]-(1-m))/D2;
                        y[2] = x[3];
                        y[3] = x[2]-2*x[1]-(1-m)*x[2]/D1-m*x[2]/D2;

                        return y;
                    };


                data = JXG.Math.Numerics.rungeKutta('rk4', x0, I, N, f);

                dataX = [];
                dataY = [];
                for(i = 0; i < data.length; i++) {
                    dataX[i] = data[i][0];
                    dataY[i] = data[i][2];
                }

                return [dataX, dataY];
            },
            circle = function (radius, points) {
                var dataX = [], dataY = [], i;

                for (i = 0; i < points; i++) {
                    dataX[i] = radius*Math.cos(i/(points-1)*2*Math.PI);
                    dataY[i] = -radius*Math.sin(i/(points-1)*2*Math.PI);
                }

                return [dataX, dataY];
            };

        earth = board.create('point', [0, 0], {
            withLabel: false,
            strokeColor: '#4096EE',
            fillColor: '#4096EE',
            size: 40
        });

        apolloPath = board.createElement('curve', ode(), {
            strokeColor: 'red',
            strokeWidth: 2,
            visible: false
        });

        moonPath = board.create('curve', circle(1-mu, apolloPath.dataX.length), {
            strokeColor: 'green',
            strokeWidth: 2,
            visible: false
        });

        moon = board.create('point', [1, 0], {
            withLabel: false,
            strokeColor: 'gray',
            fillColor: 'gray',
            size: 10
        });
        
        apollo = board.create('point', [1, 0], {
            withLabel: false,
            strokeColor: 'white',
            fillColor: 'white',
            size: 2,
            face: '<>'
        });

        moon.moveAlong(function (i) {
            return [moonPath.dataX[i%moonPath.dataX.length], moonPath.dataY[i%moonPath.dataY.length]];
        }, 2000);

        apollo.moveAlong(function (i) {
            return [apolloPath.dataX[i%apolloPath.dataX.length], apolloPath.dataY[i%apolloPath.dataY.length]];
        }, 2000);
    })();

  /* ]]> */
  </script>
<a href="#" onclick="createCurve();">Solve example ODE</a><br />
<div id="debug" style="display:block;"></div>
</body>
</html>