Home > OS >  javascript Solving equation with subsitution of variable value
javascript Solving equation with subsitution of variable value

Time:01-30

I am trying to use the nerdamer library to solve equation. Following is example which works: //multiple roots

var y = nerdamer.solve('x^6 41*x^5 652*x^4 5102*x^3 20581*x^2 40361*x 30030', 'x');
console.log(x4.toString());

But I want to solve with expression containing constant variable calculated already. For example:

var y= nerdamer.solve('x^6 a*x^5 652*x^4 b*x^3 20581*x^2 40361*x 30030', 'x');

where a and b have already been calculated in javascript code above this expression.

How can I do it? is it possible to convert terms into strings and concatenate?

thank you

CodePudding user response:

There are many ways to do this, you could use string concatenation:

var y= nerdamer.solve('x^6 '   a   '*x^5 652*x^4 '   b   '*x^3 20581*x^2 40361*x 30030', 'x');

Or Template literals to allow for variable substitution:

var y= nerdamer.solve(`x^6 ${a}*x^5 652*x^4 ${b}*x^3 20581*x^2 40361*x 30030`, 'x');
  •  Tags:  
  • Related