Pierpaolo Ercoli
Pierpaolo Ercoli

Reputation: 1064

Equation evaluation in math.js

I am new in math js library and I was trying to solve this expression:

var x_res = math.simplify('(x-'+x1+')^2 + ('+y_part+' - '+y1+')^2 - 197.5^2');

With simplify method i simplified it, but how can I do to know the "x" value ?

Thanks in advance.

Upvotes: 1

Views: 2056

Answers (1)

elpddev
elpddev

Reputation: 4484

I'm not sure what you mean by know the x value but you get an expression with one variable - x.

x1 = 2
y_part = 3
y1 = 4

var x_res = math.simplify('(x - '+x1 + ')^2 + (' + y_part + ' - ' + y1 + ')^2 - 197.5^2');

x_res.toString()
// "-156021 / 4 + (x - 2) ^ 2"

If you want then to evaluate the expression against a defined x, you can:

x_res.eval({ x: 1 })
// -39004.25

x_res.eval({ x: 2 })
// -39005.25

x_res.eval({ x: 1000 })
// 956998.75

Not regarding mathjs but, If you want to find what x will be equals to when the all equation equals some value you can use AlgebraJs

var expr = new Expression("x");
expr = expr.subtract(3);
expr = expr.add("x");

console.log(expr.toString());
2x - 3
var eq = new Equation(expr, 4);

console.log(eq.toString());
2x - 3 = 4
var x = eq.solveFor("x");

console.log("x = " + x.toString());
x = 7/2

Upvotes: 1

Related Questions