Difference between revisions of "Differential equations"

From JSXGraph Wiki
Jump to navigationJump to search
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> x'= f(t,x)</math>
 
with initial value <math>(x_0,y_0)</math>.
 
with initial value <math>(x_0,y_0)</math>.
 
<html>
 
<html>
Line 37: Line 37:
  
 
===The underlying JavaScript code===
 
===The underlying JavaScript code===
 +
<source lang="xml">
 +
<form>
 +
f(t,x)=<input type="text" id="odeinput" value="(2-t)*x"><input type=button value="ok" onclick="doIt()">
 +
</form>
 +
</source>
 
<source lang="javascript">
 
<source lang="javascript">
 +
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 P = brd.create('point',[0,1], {name:'(x_0,y_0)'});
 +
 +
function doIt() {
 +
  var t = JXG.GeonextParser.geonext2JS(document.getElementById("odeinput").value);
 +
  f = new Function("t", "xx", "var x = xx[0]; var y = " + t + "; return [y]");
 +
  brd.update();
 +
}
 +
 +
function ode() {
 +
  return JXG.Math.Numerics.rungeKutta(JXG.Math.Numerics.predefinedButcher.Heun, [P.Y()], [P.X(), P.X()+N.Value()], 200, f);
 +
}
 +
 +
var g = brd.createElement('curve', [[0],[0]], {strokeColor:'red', strokeWidth:'2px'});
 +
g.updateDataArray = function() {
 +
    var data = ode();
 +
    var h = N.Value()/200;
 +
    this.dataX = [];
 +
    this.dataY = [];
 +
    for(var i=0; i<data.length; i++) {
 +
        this.dataX[i] = P.X()+i*h;
 +
        this.dataY[i] = data[i][0];
 +
    }
 +
};
 +
doIt();
 
</source>
 
</source>
  
 
[[Category:Examples]]
 
[[Category:Examples]]
 
[[Category:Calculus]]
 
[[Category:Calculus]]

Revision as of 15:21, 14 July 2010

Display solutions of the ordinary differential equation

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

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

f(t,x)=

The underlying JavaScript code

<form>
f(t,x)=<input type="text" id="odeinput" value="(2-t)*x"><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 P = brd.create('point',[0,1], {name:'(x_0,y_0)'});

function doIt() {
  var t = JXG.GeonextParser.geonext2JS(document.getElementById("odeinput").value);
  f = new Function("t", "xx", "var x = xx[0]; var y = " + t + "; return [y]");
  brd.update();
}

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

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