Rational functions: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 26: Line 26:
         p[0] = board.createElement('point', [-1,2], {style:6});
         p[0] = board.createElement('point', [-1,2], {style:6});
         p[1] = board.createElement('point', [3,-1], {style:6});
         p[1] = board.createElement('point', [3,-1], {style:6});
         var polynomial = function(x) {
         var f = board.lagrangePolyomial(p);
                var i;
         graph = board.createElement('curve', ['x', f, 'x', -10, 10], {curveType:'graph'});
                var y = 0.0;
                var xc = [];
                for (i=0;i<p.length;i++) {
                  xc[i] = p[i].X();
                }
                for (i=0;i<p.length;i++) {
                  var t = p[i].Y();
                  for (var k=0;k<p.length;k++) {
                    if (k!=i) {
                      t *= (x-xc[k])/(xc[i]-xc[k]);
                    }
                  }
                  y += t;
                }
                return y;
            };
         graph = board.createElement('curve', ['x', polynomial, 'x', -10, 10], {curveType:'graph'});


         function addPoint1() {
         function addPoint1() {
Line 58: Line 41:
         q[0] = board2.createElement('point', [-1,2], {style:6});
         q[0] = board2.createElement('point', [-1,2], {style:6});
         q[1] = board2.createElement('point', [3,-1], {style:6});
         q[1] = board2.createElement('point', [3,-1], {style:6});
         var polynomial2 = function(x) {
         var f2 = board2.lagrangePolyomial(p);
                var i;
         graph2 = board2.createElement('curve', ['x', f2, 'x', -10, 10], {curveType:'graph'});
                var y = 0.0;
                var xc = [];
                for (i=0;i<q.length;i++) {
                  xc[i] = q[i].X();
                }
                for (i=0;i<q.length;i++) {
                  var t = q[i].Y();
                  for (var k=0;k<q.length;k++) {
                    if (k!=i) {
                      t *= (x-xc[k])/(xc[i]-xc[k]);
                    }
                  }
                  y += t;
                }
                return y;
            };
         graph2 = board2.createElement('curve', ['x', polynomial2, 'x', -10, 10], {curveType:'graph'});


         function addPoint2() {
         function addPoint2() {
Line 87: Line 53:
         b1axisx3 = board3.createElement('axis', [[0,0], [1,0]], {});
         b1axisx3 = board3.createElement('axis', [[0,0], [1,0]], {});
         b1axisy3 = board3.createElement('axis', [[0,0], [0,1]], {});
         b1axisy3 = board3.createElement('axis', [[0,0], [0,1]], {});
         graph3 = board3.createElement('curve', ['x', function(x){return polynomial(x)/polynomial2(x);}, 'x', -10, 10], {curveType:'graph',strokeColor:'#ff0000'});
         graph3 = board3.createElement('curve', ['x', function(x){return f(x)/f2(x);}, 'x', -10, 10], {curveType:'graph',strokeColor:'#ff0000'});
            
            
</script>
</script>
Line 107: Line 73:
         p[0] = board.createElement('point', [-1,2], {style:6});
         p[0] = board.createElement('point', [-1,2], {style:6});
         p[1] = board.createElement('point', [3,-1], {style:6});
         p[1] = board.createElement('point', [3,-1], {style:6});
         var polynomial = function(x) {
         var f = board.lagrangePolyomial(p);
                var i;
         graph = board.createElement('curve', ['x', f, 'x', -10, 10], {curveType:'graph'});
                var y = 0.0;
                var xc = [];
                for (i=0;i<p.length;i++) {
                  xc[i] = p[i].X();
                }
                for (i=0;i<p.length;i++) {
                  var t = p[i].Y();
                  for (var k=0;k<p.length;k++) {
                    if (k!=i) {
                      t *= (x-xc[k])/(xc[i]-xc[k]);
                    }
                  }
                  y += t;
                }
                return y;
            };
         graph = board.createElement('curve', ['x', polynomial, 'x', -10, 10], {curveType:'graph'});


         function addPoint1() {
         function addPoint1() {
Line 139: Line 88:
         q[0] = board2.createElement('point', [-1,2], {style:6});
         q[0] = board2.createElement('point', [-1,2], {style:6});
         q[1] = board2.createElement('point', [3,-1], {style:6});
         q[1] = board2.createElement('point', [3,-1], {style:6});
         var polynomial2 = function(x) {
         var f2 = board2.lagrangePolyomial(p);
                var i;
         graph2 = board2.createElement('curve', ['x', f2, 'x', -10, 10], {curveType:'graph'});
                var y = 0.0;
                var xc = [];
                for (i=0;i<q.length;i++) {
                  xc[i] = q[i].X();
                }
                for (i=0;i<q.length;i++) {
                  var t = q[i].Y();
                  for (var k=0;k<q.length;k++) {
                    if (k!=i) {
                      t *= (x-xc[k])/(xc[i]-xc[k]);
                    }
                  }
                  y += t;
                }
                return y;
            };
         graph2 = board2.createElement('curve', ['x', polynomial2, 'x', -10, 10], {curveType:'graph'});


         function addPoint2() {
         function addPoint2() {
Line 168: Line 100:
         b1axisx3 = board3.createElement('axis', [[0,0], [1,0]], {});
         b1axisx3 = board3.createElement('axis', [[0,0], [1,0]], {});
         b1axisy3 = board3.createElement('axis', [[0,0], [0,1]], {});
         b1axisy3 = board3.createElement('axis', [[0,0], [0,1]], {});
         graph3 = board3.createElement('curve', ['x', function(x){return polynomial(x)/polynomial2(x);}, 'x', -10, 10], {curveType:'graph',strokeColor:'#ff0000'});
         graph3 = board3.createElement('curve', ['x', function(x){return f(x)/f2(x);}, 'x', -10, 10], {curveType:'graph',strokeColor:'#ff0000'});
</source>
</source>


[[Category:Examples]]
[[Category:Examples]]
[[Category:Calculus]]
[[Category:Calculus]]

Revision as of 19:08, 4 February 2009

Polynomial [math]\displaystyle{ f }[/math]:

Polynomial [math]\displaystyle{ g }[/math]:

Rational function [math]\displaystyle{ \frac{f}{g} }[/math]:

References

The underlying JavaScript source code

        board = JXG.JSXGraph.initBoard('box1', {originX: 300, originY: 100, unitX: 30, unitY: 25});
        // Axes
        b1axisx = board.createElement('axis', [[0,0], [1,0]], {});
        b1axisy = board.createElement('axis', [[0,0], [0,1]], {});

        var p = [];
        var q = [];
        p[0] = board.createElement('point', [-1,2], {style:6});
        p[1] = board.createElement('point', [3,-1], {style:6});
        var f = board.lagrangePolyomial(p);
        graph = board.createElement('curve', ['x', f, 'x', -10, 10], {curveType:'graph'});

        function addPoint1() {
          p.push(board.createElement('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{style:6}));
          board.update();
        }

        board2 = JXG.JSXGraph.initBoard('box2', {originX: 300, originY: 100, unitX: 30, unitY: 25});
        // Axes
        b1axisx2 = board2.createElement('axis', [[0,0], [1,0]], {});
        b1axisy2 = board2.createElement('axis', [[0,0], [0,1]], {});

        q[0] = board2.createElement('point', [-1,2], {style:6});
        q[1] = board2.createElement('point', [3,-1], {style:6});
        var f2 = board2.lagrangePolyomial(p);
        graph2 = board2.createElement('curve', ['x', f2, 'x', -10, 10], {curveType:'graph'});

        function addPoint2() {
          q.push(board2.createElement('point',[(Math.random()-0.5)*10,(Math.random()-0.5)*3],{style:6}));
          board2.update();
        }

        board3 = JXG.JSXGraph.initBoard('box3', {originX: 300, originY: 200, unitX: 30, unitY: 25});
        // Axes
        b1axisx3 = board3.createElement('axis', [[0,0], [1,0]], {});
        b1axisy3 = board3.createElement('axis', [[0,0], [0,1]], {});
        graph3 = board3.createElement('curve', ['x', function(x){return f(x)/f2(x);}, 'x', -10, 10], {curveType:'graph',strokeColor:'#ff0000'});