Parser equation for graphical display in Javascript or JQuery

I want the equalizer to be able to solve for both x and y to get an array of points (i.e. x and y) as well.

Example:

Suppose the user enters the expression:

var expression ="x + y = 1";

      

Now let's say that I know the domain and ranges:

var xMin = -10, yMin = -10, xMax = 10 ,yMax = 10;

      

What I want are projection points between these ranges and the domain.

... I want an array or two dimensional array to contain the value of the x and y coordinates.

Please, can anyone suggest me how to get here using any algorithm or program.

Thanks in advance!

+3


source to share


2 answers


After parsing the equation into the standard form, you can substitute different values ​​depending on the number of points required from the given range for one unknown variable, for example, for this example, say x and get the corresponding y values.



+1


source


Try it. "parser.js" can be downloaded here https://github.com/silentmatt/js-expression-eval/tree/master

Take a look at this also, http://silentmatt.com/javascript-function-plotter/ might be helpful



     <html>
<head>
<script src="parser.js"></script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>

<script>


$( document ).ready(function() {
var xMin = -10, yMin = -10, xMax = 10 ,yMax = 10;
var rangeIncrement=1;

var equation="";
var points=[];

$( "#btnDraw" ).click(function() {
points=[];
equation=$("#txtequation").val()
expr = Parser.parse(equation);
i=0;
for(i=xMin  ;i<=xMax ;i+=rangeIncrement)
{
  result=expr.evaluate({ x: i});
    points.push([i, result]);
}
$( "#resultPoints" ).empty();
$.each(points, function( index, value ) {
  $( "#resultPoints" ).append( "<p>X="+value[0] +", Y="+ value[1]+"</p>" );
});

});
});



</script>
</head>
<body>
var xMin = -10, yMin = -10, xMax = 10 ,yMax = 10;
<br>
var rangeIncrement=1;
<br>
Y= <input type="text" id="txtequation"  value="x+1" /><button id="btnDraw">Draw</button>
<br>
<div id="resultPoints"></div>
</body>
</html>

      

+1


source







All Articles