1

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.

Pierpaolo Ercoli
  • 1,028
  • 2
  • 11
  • 32

1 Answers1

1

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
elpddev
  • 4,314
  • 4
  • 26
  • 47
  • First of all, thanks for your answer. I mean that, I can't evaluate the function with a defined value for x variable. I just wanna know the value of the x variable because it's an expression with only one unknown value. Just this.. I hope that I have been clear – Pierpaolo Ercoli Apr 19 '17 at 14:04
  • Sorry :) Not sure I understand. Are you trying to extract what `x` will be equal to when yo do something like `-156021 / 4 + (x - 2) ^ 2 = 1000` ? – elpddev Apr 19 '17 at 14:08
  • Yes for example I need it give me x = 3 .. something similar to this – Pierpaolo Ercoli Apr 19 '17 at 14:21
  • 1
    Not an expert on mathjs, but I did stumble upon other library for that kind of calculations - AlgebraJs – elpddev Apr 19 '17 at 14:25
  • thank you ! Using AlgebraJs I solved the system of equations. Thanks for the hint. – Pierpaolo Ercoli Apr 20 '17 at 07:30