Difference between revisions of "Differential equations"

From JSXGraph Wiki
Jump to navigationJump to search
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Display solutions of the ordinary differential equation
 
Display solutions of the ordinary differential equation
:<math> x'= f(t,x)</math>
+
:<math> y'= f(t,y)</math>
 
with initial value <math>(x_0,y_0)</math>.
 
with initial value <math>(x_0,y_0)</math>.
 +
 +
It is easy to incorporate sliders: give the slider a (unique) name and use this name in the equation. In the example below, the slider name is <math>c</math>.
 
<html>
 
<html>
 
<form>
 
<form>
f(t,x)=<input type="text" id="odeinput" value="(2-t)*x"><input type=button value="ok" onclick="doIt()">
+
f(x,y)=<input type="text" id="odeinput" value="(2-x)*y + c"><input type=button value="ok" onclick="doIt()">
 
</form>
 
</form>
 
</html>
 
</html>
Line 10: Line 12:
 
var brd = JXG.JSXGraph.initBoard('jxgbox', {axis:true, boundingbox:[-11,11,11,-11]});
 
var brd = JXG.JSXGraph.initBoard('jxgbox', {axis:true, boundingbox:[-11,11,11,-11]});
 
var N = brd.create('slider',[[-7,9.5],[7,9.5],[-15,10,15]], {name:'N'});
 
var N = brd.create('slider',[[-7,9.5],[7,9.5],[-15,10,15]], {name:'N'});
var P = brd.create('point',[0,1], {name:'(x_0,y_0)'});
+
var slider = brd.create('slider',[[-7,8],[7,8],[-15,0,15]], {name:'c'});
 +
var P = brd.create('point',[0,1], {name:'(x_0, y_0)'});
 +
var f;
  
 
function doIt() {
 
function doIt() {
   var t = JXG.GeonextParser.geonext2JS(document.getElementById("odeinput").value);
+
   var snip = brd.jc.snippet(document.getElementById("odeinput").value, true, 'x, y');
   f = new Function("t", "xx", "var x = xx[0]; var y = " + t + "; return [y]");
+
   f = function (x, yy) {
 +
      return [snip(x, yy[0])];
 +
  }
 
   brd.update();
 
   brd.update();
 
}
 
}
  
 
function ode() {
 
function ode() {
   return JXG.Math.Numerics.rungeKutta(JXG.Math.Numerics.predefinedButcher.Heun, [P.Y()], [P.X(), P.X()+N.Value()], 200, f);
+
   return JXG.Math.Numerics.rungeKutta('heun', [P.Y()], [P.X(), P.X()+N.Value()], 200, f);
 
}
 
}
  
var g = brd.createElement('curve', [[0],[0]], {strokeColor:'red', strokeWidth:'2px'});
+
var g = brd.create('curve', [[0],[0]], {strokeColor:'red', strokeWidth:2});
 
g.updateDataArray = function() {
 
g.updateDataArray = function() {
 
     var data = ode();
 
     var data = ode();
 
     var h = N.Value()/200;
 
     var h = N.Value()/200;
 +
    var i;
 
     this.dataX = [];
 
     this.dataX = [];
 
     this.dataY = [];
 
     this.dataY = [];
     for(var i=0; i<data.length; i++) {
+
     for(i=0; i<data.length; i++) {
 
         this.dataX[i] = P.X()+i*h;
 
         this.dataX[i] = P.X()+i*h;
 
         this.dataY[i] = data[i][0];
 
         this.dataY[i] = data[i][0];
Line 37: Line 44:
  
 
===See also===
 
===See also===
 +
* [[Systems of differential equations]]
 
* [[Lotka-Volterra equations]]
 
* [[Lotka-Volterra equations]]
 +
* [[Epidemiology: The SIR model]]
 +
* [[Population growth models]]
 +
* [[Autocatalytic process]]
 +
* [[Logistic process]]
 +
* Paul Pearson has written a very nice variation: [http://faculty.fortlewis.edu/Pearson_P/jsxgraph/slopefield.html Slope fields and solution curves (using the Runge-Kutta)]
  
 
===The underlying JavaScript code===
 
===The underlying JavaScript code===
 
<source lang="xml">
 
<source lang="xml">
 
<form>
 
<form>
f(t,x)=<input type="text" id="odeinput" value="(2-t)*x"><input type=button value="ok" onclick="doIt()">
+
f(x,y)=<input type="text" id="odeinput" value="(2-x)*y + c"><input type=button value="ok" onclick="doIt()">
 
</form>
 
</form>
 
</source>
 
</source>
Line 48: Line 61:
 
var brd = JXG.JSXGraph.initBoard('jxgbox', {axis:true, boundingbox:[-11,11,11,-11]});
 
var brd = JXG.JSXGraph.initBoard('jxgbox', {axis:true, boundingbox:[-11,11,11,-11]});
 
var N = brd.create('slider',[[-7,9.5],[7,9.5],[-15,10,15]], {name:'N'});
 
var N = brd.create('slider',[[-7,9.5],[7,9.5],[-15,10,15]], {name:'N'});
var P = brd.create('point',[0,1], {name:'(x_0,y_0)'});
+
var slider = brd.create('slider',[[-7,8],[7,8],[-15,0,15]], {name:'c'});
 +
var P = brd.create('point',[0,1], {name:'(x_0, y_0)'});
 +
var f;
  
 
function doIt() {
 
function doIt() {
   var t = JXG.GeonextParser.geonext2JS(document.getElementById("odeinput").value);
+
   var snip = brd.jc.snippet(document.getElementById("odeinput").value, true, 'x, y');
   f = new Function("t", "xx", "var x = xx[0]; var y = " + t + "; return [y]");
+
   f = function (x, yy) {
 +
      return [snip(x, yy[0])];
 +
  }
 
   brd.update();
 
   brd.update();
 
}
 
}
  
 
function ode() {
 
function ode() {
   return JXG.Math.Numerics.rungeKutta(JXG.Math.Numerics.predefinedButcher.Heun, [P.Y()], [P.X(), P.X()+N.Value()], 200, f);
+
   return JXG.Math.Numerics.rungeKutta('heun', [P.Y()], [P.X(), P.X()+N.Value()], 200, f);
 
}
 
}
  
var g = brd.createElement('curve', [[0],[0]], {strokeColor:'red', strokeWidth:'2px'});
+
var g = brd.create('curve', [[0],[0]], {strokeColor:'red', strokeWidth:2});
 
g.updateDataArray = function() {
 
g.updateDataArray = function() {
 
     var data = ode();
 
     var data = ode();
 
     var h = N.Value()/200;
 
     var h = N.Value()/200;
 +
    var i;
 
     this.dataX = [];
 
     this.dataX = [];
 
     this.dataY = [];
 
     this.dataY = [];
     for(var i=0; i<data.length; i++) {
+
     for(i=0; i<data.length; i++) {
 
         this.dataX[i] = P.X()+i*h;
 
         this.dataX[i] = P.X()+i*h;
 
         this.dataY[i] = data[i][0];
 
         this.dataY[i] = data[i][0];

Latest revision as of 12:39, 19 January 2017

Display solutions of the ordinary differential equation

[math] y'= f(t,y)[/math]

with initial value [math](x_0,y_0)[/math].

It is easy to incorporate sliders: give the slider a (unique) name and use this name in the equation. In the example below, the slider name is [math]c[/math].

f(x,y)=

See also

The underlying JavaScript code

<form>
f(x,y)=<input type="text" id="odeinput" value="(2-x)*y + c"><input type=button value="ok" onclick="doIt()">
</form>
var brd = JXG.JSXGraph.initBoard('jxgbox', {axis:true, boundingbox:[-11,11,11,-11]});
var N = brd.create('slider',[[-7,9.5],[7,9.5],[-15,10,15]], {name:'N'});
var slider = brd.create('slider',[[-7,8],[7,8],[-15,0,15]], {name:'c'});
var P = brd.create('point',[0,1], {name:'(x_0, y_0)'});
var f;

function doIt() {
  var snip = brd.jc.snippet(document.getElementById("odeinput").value, true, 'x, y');
  f = function (x, yy) {
      return [snip(x, yy[0])];
  }
  brd.update();
}

function ode() {
   return JXG.Math.Numerics.rungeKutta('heun', [P.Y()], [P.X(), P.X()+N.Value()], 200, f);
}

var g = brd.create('curve', [[0],[0]], {strokeColor:'red', strokeWidth:2});
g.updateDataArray = function() {
    var data = ode();
    var h = N.Value()/200;
    var i;
    this.dataX = [];
    this.dataY = [];
    for(i=0; i<data.length; i++) {
        this.dataX[i] = P.X()+i*h;
        this.dataY[i] = data[i][0];
    }
};
doIt();