Difference between revisions of "Cubic spline interpolation"

From JSXGraph Wiki
Jump to navigationJump to search
 
(20 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
Points can be added by clicking on "Add point".
 
Points can be added by clicking on "Add point".
 
<html>
 
<html>
<link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
 
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
 
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
 
 
<form><input type="button" value="Add point" onClick="addPoint()"></form>
 
<form><input type="button" value="Add point" onClick="addPoint()"></form>
<div id="box" class="jxgbox" style="width:600px; height:400px;"></div>
+
</html>
<script language="JavaScript">
+
<jsxgraph width="600" height="400" box="box">
        board = JXG.JSXGraph.initBoard('box', {originX: 250, originY: 250, unitX: 50, unitY: 25});
+
var board = JXG.JSXGraph.initBoard('box', {boundingbox: [-5, 10, 7, -5], axis:true});
        // Axes
+
var p = [];
        b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
+
p[0] = board.create('point', [-1,2], {size: 4, face: 'o'});
        b1axisy = board.createElement('axis', [[0,0], [0,1]], {});
+
p[1] = board.create('point', [0,-1], {size: 4, face: 'o'});
 +
p[2] = board.create('point', [1,0], {size: 4, face: 'o'});
 +
p[3] = board.create('point', [2,1], {size: 4, face: 'o'});
  
        var p = [];
+
var c = board.create('spline', p, {strokeWidth:3});
        p[0] = board.createElement('point', [-1,2], {style:6});
 
        p[1] = board.createElement('point', [0,-1], {style:6});
 
        p[2] = board.createElement('point', [1,0], {style:6});
 
        p[3] = board.createElement('point', [2,1], {style:6});
 
  
        function computeSpline() {
+
var g = board.create('glider', [1.5,0,c], {name:'',style:8});
          var x = [];
+
var t = board.create('tangent', [g], {dash:2,strokeColor:'#aa0000'});
          var y = [];
+
   
          for (var i=0;i<p.length;i++) {
+
function addPoint() {
            x[i] = p[i].X();
+
    p.push(board.create('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{size: 4, face: 'o'}));
            y[i] = p[i].Y();
+
    board.update();
          }
+
}            
          var F = JXG.Math.Numerics.splineDef(x, y);
+
</jsxgraph>
          var px = [];
 
          i = 0;
 
          var delta = (x[x.length-1]-x[0])/board.canvasWidth*1.0;
 
          while(x[0] + i*delta < x[x.length-1]) {
 
            px[i] = x[0] + i*delta;
 
            i++;
 
          }
 
          var py = JXG.Math.Numerics.splineEval(px, x, y, F);
 
           return F;
 
        }         
 
  
        var c = board.createElement('curve', ["x",computeSpline(),"x"], {strokeWidth:3});
+
=== References ===
 +
* [http://en.wikipedia.org/wiki/Spline_interpolation http://en.wikipedia.org/wiki/Spline_interpolation]
  
        function addPoint() {
+
=== The underlying JavaScript code ===
          p.push(board.createElement('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{style:6}));
+
<source lang="javascript">
          board.update();
+
var board = JXG.JSXGraph.initBoard('box', {boundingbox: [-5, 10, 7, -5], axis:true});
        }
+
var p = [];
         
+
p[0] = board.create('point', [-1,2], {size: 4, face: 'o'});
</script>
+
p[1] = board.create('point', [0,-1], {size: 4, face: 'o'});
</html>
+
p[2] = board.create('point', [1,0], {size: 4, face: 'o'});
 +
p[3] = board.create('point', [2,1], {size: 4, face: 'o'});
  
<source lang="javascript">
+
var c = board.create('spline', p, {strokeWidth:3});
        function addPoint() {
 
          p.push(board.createElement('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{style:6}));
 
          board.update();
 
        }
 
  
</source>
+
var g = board.create('glider', [1.5,0,c], {name:'',style:8});
 +
var t = board.create('tangent', [g], {dash:2,strokeColor:'#aa0000'});
 +
   
 +
function addPoint() {
 +
    p.push(board.create('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{size: 4, face: 'o'}));
 +
    board.update();
 +
}</source>
  
  
 
[[Category:Examples]]
 
[[Category:Examples]]
 +
[[Category:Interpolation]]

Latest revision as of 15:09, 7 June 2011

Constructs a cubic spline through given points. Points can be added by clicking on "Add point".

References

The underlying JavaScript code

var board = JXG.JSXGraph.initBoard('box', {boundingbox: [-5, 10, 7, -5], axis:true});
var p = [];
p[0] = board.create('point', [-1,2], {size: 4, face: 'o'});
p[1] = board.create('point', [0,-1], {size: 4, face: 'o'});
p[2] = board.create('point', [1,0], {size: 4, face: 'o'});
p[3] = board.create('point', [2,1], {size: 4, face: 'o'});

var c = board.create('spline', p, {strokeWidth:3});

var g = board.create('glider', [1.5,0,c], {name:'',style:8});
var t = board.create('tangent', [g], {dash:2,strokeColor:'#aa0000'});
     
function addPoint() {
    p.push(board.create('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{size: 4, face: 'o'}));
    board.update();
}