Even simpler function plotter: Difference between revisions
From JSXGraph Wiki
A WASSERMANN (talk | contribs) No edit summary |
A WASSERMANN (talk | contribs) No edit summary |
||
(20 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
<input type="text" id="input" value="sin(x)*x"> | <input type="text" id="input" value="sin(x)*x"> | ||
<input type="button" value="plot" onClick="plotter()"> | <input type="button" value="plot" onClick="plotter()"> | ||
<input type="button" value="clear all" onClick="clearAll()"> | |||
<input type="button" value="add slope info" onClick="addSlopeInfo()"> | |||
<input type="button" value="add tangent" onClick="addTangent()"> | <input type="button" value="add tangent" onClick="addTangent()"> | ||
<input type="button" value="add | <input type="button" value="add Derivative" onClick="addDerivative()"> | ||
</form> | </form> | ||
</html> | </html> | ||
Line 12: | Line 13: | ||
function plotter() { | function plotter() { | ||
var | var txtraw = document.getElementById('input').value; | ||
f = | f = board.jc.snippet(txtraw, true, 'x', true); | ||
curve = board.create('functiongraph',[f, | curve = board.create('functiongraph',[f, | ||
function(){ | function(){ | ||
Line 23: | Line 24: | ||
return c.usrCoords[1]; | return c.usrCoords[1]; | ||
} | } | ||
]); | ],{name:txtraw, withLabel:true}); | ||
} | } | ||
Line 31: | Line 32: | ||
f = null; | f = null; | ||
curve = null; | curve = null; | ||
} | |||
function addSlopeInfo() { | |||
if (JXG.isFunction(f)) { | |||
var txtraw = document.getElementById('input').value; | |||
board.suspendUpdate(); | |||
var q = board.create('glider', [2, 1, curve], {withLabel:false}); | |||
var t = board.create('text', [ | |||
function(){ return q.X()+0.1; }, | |||
function(){ return q.Y()+0.1; }, | |||
function(){ return "The slope of the function f(x)=" + txtraw + "<br>at x=" + q.X().toFixed(2) + " is equal to " + (JXG.Math.Numerics.D(f))(q.X()).toFixed(2); } | |||
], | |||
{fontSize:15}); | |||
board.unsuspendUpdate(); | |||
} | |||
} | } | ||
Line 44: | Line 60: | ||
function addDerivative() { | function addDerivative() { | ||
if (JXG.isFunction(f)) { | if (JXG.isFunction(f)) { | ||
board.create('functiongraph',[JXG.Math.D(f), | board.create('functiongraph',[JXG.Math.Numerics.D(f), | ||
function(){ | function(){ | ||
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board); | var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board); | ||
Line 52: | Line 68: | ||
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board); | var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board); | ||
return c.usrCoords[1]; | return c.usrCoords[1]; | ||
} | }], {dash:2}); | ||
], {dash:2}); | |||
} | } | ||
} | } | ||
</jsxgraph> | </jsxgraph> | ||
===The underlying JavaScript code=== | ===The underlying JavaScript code=== | ||
Line 64: | Line 77: | ||
<input type="button" value="plot" onClick="plotter()"> | <input type="button" value="plot" onClick="plotter()"> | ||
<input type="button" value="clear all" onClick="clearAll()"> | <input type="button" value="clear all" onClick="clearAll()"> | ||
<input type="button" value="add tangent" onClick="addTangent()"> | |||
<input type="button" value="add Derivative" onClick="addDerivative()"> | |||
</source> | </source> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true}); | var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true}); | ||
var f, curve; // global objects | |||
function plotter() { | function plotter() { | ||
var | var txtraw = document.getElementById('input').value; | ||
f = board.jc.snippet(txtraw, true, 'x', true); | |||
board.create('functiongraph',[f, | curve = board.create('functiongraph',[f, | ||
function(){ | function(){ | ||
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board); | var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board); | ||
Line 80: | Line 96: | ||
return c.usrCoords[1]; | return c.usrCoords[1]; | ||
} | } | ||
]); | ],{name:txtraw, withLabel:true}); | ||
var q = board.create('glider', [2, 1, curve], {withLabel:false}); | |||
var t = board.create('text', [ | |||
function(){ return q.X()+0.1; }, | |||
function(){ return q.Y()+0.1; }, | |||
function(){ return "The slope of the function f(x)=" + txtraw + "<br>at x=" + q.X().toFixed(2) + " is equal to " + (JXG.Math.Numerics.D(f))(q.X()).toFixed(2); } | |||
], | |||
{fontSize:15}); | |||
} | } | ||
function clearAll() { | function clearAll() { | ||
JXG.JSXGraph.freeBoard(board); | JXG.JSXGraph.freeBoard(board); | ||
board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true}); | board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true}); | ||
f = null; | |||
curve = null; | |||
} | |||
function addSlopeInfo() { | |||
if (JXG.isFunction(f)) { | |||
var txtraw = document.getElementById('input').value; | |||
board.suspendUpdate(); | |||
var q = board.create('glider', [2, 1, curve], {withLabel:false}); | |||
var t = board.create('text', [ | |||
function(){ return q.X()+0.1; }, | |||
function(){ return q.Y()+0.1; }, | |||
function(){ return "The slope of the function f(x)=" + txtraw + "<br>at x=" + q.X().toFixed(2) + " is equal to " + (JXG.Math.Numerics.D(f))(q.X()).toFixed(2); } | |||
], | |||
{fontSize:15}); | |||
board.unsuspendUpdate(); | |||
} | |||
} | |||
function addTangent() { | |||
if (JXG.isFunction(f)) { | |||
board.suspendUpdate(); | |||
var p = board.create('glider',[1,0,curve], {name:'drag me'}); | |||
board.create('tangent',[p], {name:'drag me'}); | |||
board.unsuspendUpdate(); | |||
} | |||
} | |||
function addDerivative() { | |||
if (JXG.isFunction(f)) { | |||
board.create('functiongraph',[JXG.Math.Numerics.D(f), | |||
function(){ | |||
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board); | |||
return c.usrCoords[1]; | |||
}, | |||
function(){ | |||
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board); | |||
return c.usrCoords[1]; | |||
}], {dash:2}); | |||
} | |||
} | } | ||
</source> | </source> |
Latest revision as of 10:58, 21 January 2019
The underlying JavaScript code
<input type="text" id="input" value="sin(x)*x">
<input type="button" value="plot" onClick="plotter()">
<input type="button" value="clear all" onClick="clearAll()">
<input type="button" value="add tangent" onClick="addTangent()">
<input type="button" value="add Derivative" onClick="addDerivative()">
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
var f, curve; // global objects
function plotter() {
var txtraw = document.getElementById('input').value;
f = board.jc.snippet(txtraw, true, 'x', true);
curve = board.create('functiongraph',[f,
function(){
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board);
return c.usrCoords[1];
},
function(){
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board);
return c.usrCoords[1];
}
],{name:txtraw, withLabel:true});
var q = board.create('glider', [2, 1, curve], {withLabel:false});
var t = board.create('text', [
function(){ return q.X()+0.1; },
function(){ return q.Y()+0.1; },
function(){ return "The slope of the function f(x)=" + txtraw + "<br>at x=" + q.X().toFixed(2) + " is equal to " + (JXG.Math.Numerics.D(f))(q.X()).toFixed(2); }
],
{fontSize:15});
}
function clearAll() {
JXG.JSXGraph.freeBoard(board);
board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
f = null;
curve = null;
}
function addSlopeInfo() {
if (JXG.isFunction(f)) {
var txtraw = document.getElementById('input').value;
board.suspendUpdate();
var q = board.create('glider', [2, 1, curve], {withLabel:false});
var t = board.create('text', [
function(){ return q.X()+0.1; },
function(){ return q.Y()+0.1; },
function(){ return "The slope of the function f(x)=" + txtraw + "<br>at x=" + q.X().toFixed(2) + " is equal to " + (JXG.Math.Numerics.D(f))(q.X()).toFixed(2); }
],
{fontSize:15});
board.unsuspendUpdate();
}
}
function addTangent() {
if (JXG.isFunction(f)) {
board.suspendUpdate();
var p = board.create('glider',[1,0,curve], {name:'drag me'});
board.create('tangent',[p], {name:'drag me'});
board.unsuspendUpdate();
}
}
function addDerivative() {
if (JXG.isFunction(f)) {
board.create('functiongraph',[JXG.Math.Numerics.D(f),
function(){
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board);
return c.usrCoords[1];
},
function(){
var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board);
return c.usrCoords[1];
}], {dash:2});
}
}