Search code examples
javascriptmathjsxgraphmath.js

How to set initial position of draggable points in JSXGraph in an equation graph?


I'm developing a web software that plots an interactive polynomial equation with 2 variables on JSXGraph, I need that every point where the derivative of the equation is 0 to have a draggable point, and that when I drag one point, only the interval between 1 point after and 1 before the dragged point of the curve goes together does anyone knows how I could do that?


Solution

  • Sorry for the delay. I guess, what you need is Hermite interpolation. I gave an example recently in Google groups (https://groups.google.com/forum/#!topic/jsxgraph/OHls2NTOmUA). Hermite interpolation is not yet integrated in JSXGraph, but there seem to be a demand for it, so we may think about it.

    Here is the code:

    JXG.Math.Numerics.hermitePolynomial = function (p, derivatives) {
            var w = [],
                Lii = [],
    
                /** @ignore */
                fct = function (x, suspendedUpdate) {
                    var i, k, xi, s,
                        L, Hi, Hi_hat,
                        len = p.length,
                        num = 0,
                        denom = 0;
    
                    if (!suspendedUpdate) {
                        // Compute denominator
                        for (i = 0; i < len; i++) {
                            w[i] = 1.0;
                            Lii[i] = 0.0;
                            xi = p[i].X();
    
                            for (k = 0; k < len; k++) {
                                if (k !== i) {
                                    w[i] *= (xi - p[k].X());
                                    Lii[i] += 1 / (xi - p[k].X());
                                }
                            }
    
                            w[i] = 1 / w[i];
                        }
                    }
    
                    s = 0.0;
                    for (i = 0; i < len; i++) {
                        xi = p[i].X();
                        L = w[i];
                        for (k = 0; k < len; k++) {
                            if (k !== i) {
                                L *= (x - p[k].X());
                            }
                        }
    
                        Hi = (1.0 - 2 * (x - xi) * Lii[i]) * L * L;
                        Hi_hat = (x -xi) * L * L;
                        s += p[i].Y() * Hi + derivatives[i] * Hi_hat;
                    }
                    return s;
                };
    
            return fct;
        };
    
    var board = JXG.JSXGraph.initBoard("jxgbox", {
                boundingbox: [-5, 6, 6, -5],
                axis: true
    });
    
    var p = [];
    // Define the points
    p[0] = board.create('point', [-1,2], {size:4});
    p[1] = board.create('point', [1,-2], {size:4});
    p[2] = board.create('point', [3,-1], {size:4});
    
    // The derivates at this points are all zero.
    var derivatives = [0, 0, 0];
    
    var f = JXG.Math.Numerics.hermitePolynomial(p, derivatives);
    var graph = board.create('functiongraph', [f, -10, 10], {strokeWidth:2});
    

    A live demo is at https://jsfiddle.net/vcL7aepo/278/

    Best wishes, Alfred