Commits

Jason S committed 7f68f9b

added Runge-Kutta solver

  • Participants
  • Parent commits aebccef

Comments (0)

Files changed (2)

demos/gravity1/gravity1.html

         if (modkey(event, false)) {  event.preventDefault(true); return false;} });
     function settextvalue(id,value)
     {
-        var s = isNaN(value) ? '---' : (''+value.toFixed(5)); 
+        var s = isNaN(value) ? '---' : (''+value.toFixed(6)); 
         document.getElementById(id).textContent = s;
     }
     function showStatistics(sim)
         <form name='options'>
             <input type="radio" name="solver" value="Euler" />Euler<br>
             <input type="radio" name="solver" value="Trapezoidal" />Trapezoidal
+            <input type="radio" name="solver" value="Runge-Kutta" />Runge-Kutta
         </form>
     </div>
 </body>

demos/gravity1/gravity1.js

         console.log(keymap);
         if (keymap.shift)
         {
+        }
+        else if (keymap.alt)
+        {
+            if (keymap.up)
+                ay += K;
+            if (keymap.down)
+                ay -= K;
+            if (keymap.right)
+                ax += K;
+            if (keymap.left)
+                ax -= K;
+        }
+        else
+        {
             var v = hypot(this.velpos[0],this.velpos[1]);
             if (v > 1e-6)
             {
                 }
             }
         }
-        else if (keymap.alt)
-        {
-        }
-        else
-        {
-            if (keymap.up)
-                ay += K;
-            if (keymap.down)
-                ay -= K;
-            if (keymap.right)
-                ax += K;
-            if (keymap.left)
-                ax -= K;
-        }
-        if (keymap['E-'])
-        {
-            ax -= this.velpos[2];
-            ay -= this.velpos[3];
-        }
-        if (keymap['E+'])
-        {
-            ax += this.velpos[2];
-            ay += this.velpos[3];
-        }
         this.velpos[0] += ax*dt;
         this.velpos[1] += ay*dt;
     },
             var vx1 = weightedsum([1,dt], [vx,dvxdt1], 4);
             var dvxdt2 = f(vx1);
             return weightedsum([1,dt/2,dt/2], [vx,dvxdt1,dvxdt2], 4);
+        },
+        'Runge-Kutta': function(vx, f, dt)
+        {
+            var dvxdt1 = f(vx);
+            var vx1 = weightedsum([1,dt/2], [vx,dvxdt1], 4);
+            var dvxdt2 = f(vx1);
+            var vx2 = weightedsum([1,dt/2], [vx,dvxdt2], 4);
+            var dvxdt3 = f(vx2);
+            var vx3 = weightedsum([1,dt], [vx,dvxdt3], 4);
+            var dvxdt4 = f(vx3);            
+            return weightedsum([1,dt/6,dt/3,dt/3,dt/6], 
+                [vx,dvxdt1,dvxdt2,dvxdt3,dvxdt4], 4);
         }
     },
     getStatistics: function() {