How to determine the value of X using JavaScript

-2

How can I find the value of X taking into account that the base formula is this? And using just JavaScript?

1056261433 * X² + 431977909 * X - 2.022 = 281860832

There are functions that do this or will need to create, could you guide me please? The intention is to create a function in JS that gives the value of X.

    
asked by anonymous 17.04.2015 / 02:17

2 answers

2

To solve a high school equation like this one there you you can use the Baskara formula Baskara formula . / p>

If the factors that multiply X and X 2 are always those of the question, you do not need to use Javascript. You can make the account in the calculator and get the two desired roots in hand. Gives X=-0.760057 or X=0.351089 , according to wolfram alpha

If the equation parameters can change then you can program the formula using Javascript. The only special function you would need is Math.sqrt to get the square root.

    
17.04.2015 / 03:21
1

As already hugomg , this is a second-degree equation, and they all have the following form :

  

ax 2 + bx + c = 0

In your case:

  

a = 1056261433
  b = 431977909
  c = -281860832 - 2.022 = -281860834.022

These equations can be solved by the Bhaskara formula:


#

In JavaScript, a simple implementation takes the values of a , b and c and returns the two possible results:

var a = 1056261433; 
var b = 431977909;
var c = -281860834.022;

function bhaskara(a, b, c) {
  
   var ret = [];
   var d = delta(a, b, c);
   ret[0] = ((b * -1) - Math.sqrt(d)) / (2 * a);
   ret[1] = ((b * -1) + Math.sqrt(d)) / (2 * a);
   return ret;
  
   // calcula o delta separadamente
   function delta(a, b, c) {
       return Math.pow(b, 2) - (4 * a * c); 
   }
  
}

document.body.innerHTML = bhaskara(a, b, c).join(', ');

The limitation of this code is that it does not handle complex numbers , if the delta of the equation is negative. If you need to deal with this type of value, you would need to create a representation of the complexes as arrays or objects, or use a library like math.js .

    
17.04.2015 / 15:13