JSXGraph logo
JSXGraph
JSXGraph share

Share

All real roots of a polynomial
QR code
<iframe 
    src="https://www.jsxgraph.org/share/iframe/all-real-roots-of-a-polynomial" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: All real roots of a polynomial" 
    allowfullscreen
></iframe>
This code has to
<div id="board-0-wrapper" class="jxgbox-wrapper " style="width: 100%; ">
   <div id="board-0" class="jxgbox" style="aspect-ratio: 1 / 1; width: 100%;" data-ar="1 / 1"></div>
</div>

<script type = "text/javascript"> 
    /*
    This example is licensed under a 
    Creative Commons Attribution ShareAlike 4.0 International License.
    https://creativecommons.org/licenses/by-sa/4.0/
    
    Please note you have to mention 
    The Center of Mobile Learning with Digital Technology
    in the credits.
    */
    
    const BOARDID = 'board-0';

    const board = JXG.JSXGraph.initBoard(BOARDID, {
        boundingbox: [-9, 9, 9, -9],
        axis: true
    });
    
    var a, b, c, d,
        f, func,
        realRoots;
    
    a = board.create('slider', [[-8, 8], [-2.5, 8], [-10, 0, 10]], { name: 'a' });
    b = board.create('slider', [[-8, 7], [-2.5, 7], [-10, -5, 10]], { name: 'b' });
    c = board.create('slider', [[-8, 6], [-2.5, 6], [-10, 2, 10]], { name: 'c' });
    d = board.create('slider', [[-8, 5], [-2.5, 5], [-10, 1, 10]], { name: 'd' });
    
    f = (x) => x ** 4 + a.Value() * x ** 3 + b.Value() * x ** 2 + c.Value() * x + d.Value();
    func = board.create('functiongraph', [f], { strokeWidth: 2 });
    
    // Return all real roots of polynomial with coefficients `coeffs`
    realRoots = function(coeffs) {
        return JXG.Math.Numerics.polzeros(coeffs) // Find ALL complex roots of the polynomial
            .filter((z) => Math.abs(z.imaginary) < 1.e-12) // Filter real roots
            .map((z) => z.real); // Convert complex numbers to real numbers
    };
    
    // Construct the roots.
    // If the roots are non-real, they are not shown.
    board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[0], 0]);
    board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[1], 0]);
    board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[2], 0]);
    board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[3], 0]);
 </script> 
/*
This example is licensed under a 
Creative Commons Attribution ShareAlike 4.0 International License.
https://creativecommons.org/licenses/by-sa/4.0/

Please note you have to mention 
The Center of Mobile Learning with Digital Technology
in the credits.
*/

const BOARDID = 'your_div_id'; // Insert your id here!

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-9, 9, 9, -9],
    axis: true
});

var a, b, c, d,
    f, func,
    realRoots;

a = board.create('slider', [[-8, 8], [-2.5, 8], [-10, 0, 10]], { name: 'a' });
b = board.create('slider', [[-8, 7], [-2.5, 7], [-10, -5, 10]], { name: 'b' });
c = board.create('slider', [[-8, 6], [-2.5, 6], [-10, 2, 10]], { name: 'c' });
d = board.create('slider', [[-8, 5], [-2.5, 5], [-10, 1, 10]], { name: 'd' });

f = (x) => x ** 4 + a.Value() * x ** 3 + b.Value() * x ** 2 + c.Value() * x + d.Value();
func = board.create('functiongraph', [f], { strokeWidth: 2 });

// Return all real roots of polynomial with coefficients `coeffs`
realRoots = function(coeffs) {
    return JXG.Math.Numerics.polzeros(coeffs) // Find ALL complex roots of the polynomial
        .filter((z) => Math.abs(z.imaginary) < 1.e-12) // Filter real roots
        .map((z) => z.real); // Convert complex numbers to real numbers
};

// Construct the roots.
// If the roots are non-real, they are not shown.
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[0], 0]);
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[1], 0]);
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[2], 0]);
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[3], 0]);
<jsxgraph width="100%" aspect-ratio="1 / 1" title="All real roots of a polynomial" description="This construction was copied from JSXGraph examples database: BTW HERE SHOULD BE A GENERATED LINKuseGlobalJS="false">
   /*
   This example is licensed under a 
   Creative Commons Attribution ShareAlike 4.0 International License.
   https://creativecommons.org/licenses/by-sa/4.0/
   
   Please note you have to mention 
   The Center of Mobile Learning with Digital Technology
   in the credits.
   */
   
   const board = JXG.JSXGraph.initBoard(BOARDID, {
       boundingbox: [-9, 9, 9, -9],
       axis: true
   });
   
   var a, b, c, d,
       f, func,
       realRoots;
   
   a = board.create('slider', [[-8, 8], [-2.5, 8], [-10, 0, 10]], { name: 'a' });
   b = board.create('slider', [[-8, 7], [-2.5, 7], [-10, -5, 10]], { name: 'b' });
   c = board.create('slider', [[-8, 6], [-2.5, 6], [-10, 2, 10]], { name: 'c' });
   d = board.create('slider', [[-8, 5], [-2.5, 5], [-10, 1, 10]], { name: 'd' });
   
   f = (x) => x ** 4 + a.Value() * x ** 3 + b.Value() * x ** 2 + c.Value() * x + d.Value();
   func = board.create('functiongraph', [f], { strokeWidth: 2 });
   
   // Return all real roots of polynomial with coefficients `coeffs`
   realRoots = function(coeffs) {
       return JXG.Math.Numerics.polzeros(coeffs) // Find ALL complex roots of the polynomial
           .filter((z) => Math.abs(z.imaginary) < 1.e-12) // Filter real roots
           .map((z) => z.real); // Convert complex numbers to real numbers
   };
   
   // Construct the roots.
   // If the roots are non-real, they are not shown.
   board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[0], 0]);
   board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[1], 0]);
   board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[2], 0]);
   board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[3], 0]);
</jsxgraph>

All real roots of a polynomial

The method `JXG.Math.Numerics.polzeros` computes all (complex) roots of a polynomial with real coefficients. We can use this method to filter out the real roots of that polynomial. Here is an example for the quartic polynomial \[ f(x) = x^4 + ax^3 + bx^2 + cx + d \] The values of $a$, $b$, $c$, and $d$ can be adjusted with the sliders.
// Define the id of your board in BOARDID

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-9, 9, 9, -9],
    axis: true
});

var a, b, c, d,
    f, func,
    realRoots;

a = board.create('slider', [[-8, 8], [-2.5, 8], [-10, 0, 10]], { name: 'a' });
b = board.create('slider', [[-8, 7], [-2.5, 7], [-10, -5, 10]], { name: 'b' });
c = board.create('slider', [[-8, 6], [-2.5, 6], [-10, 2, 10]], { name: 'c' });
d = board.create('slider', [[-8, 5], [-2.5, 5], [-10, 1, 10]], { name: 'd' });

f = (x) => x ** 4 + a.Value() * x ** 3 + b.Value() * x ** 2 + c.Value() * x + d.Value();
func = board.create('functiongraph', [f], { strokeWidth: 2 });

// Return all real roots of polynomial with coefficients `coeffs`
realRoots = function(coeffs) {
    return JXG.Math.Numerics.polzeros(coeffs) // Find ALL complex roots of the polynomial
        .filter((z) => Math.abs(z.imaginary) < 1.e-12) // Filter real roots
        .map((z) => z.real); // Convert complex numbers to real numbers
};

// Construct the roots.
// If the roots are non-real, they are not shown.
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[0], 0]);
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[1], 0]);
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[2], 0]);
board.create('point', [() => realRoots([d.Value(), c.Value(), b.Value(), a.Value(), 1])[3], 0]);

license

This example is licensed under a Creative Commons Attribution ShareAlike 4.0 International License.
Please note you have to mention The Center of Mobile Learning with Digital Technology in the credits.