• Resolved hheyhey568

    (@hheyhey568)


    Hello team CFF,
    Is there a code for cubic equation to find it’s root like following equation,

    x3-4×2-9x+36=0

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @hheyhey568

    Thank you very much for using our plugin. Please, use the POW operation. POW(x, y) is equivalent to x^y

    Assuming that x is the number field fieldname1, the equation would be:

    POW(fieldname1, 3)-4*POW(fieldname1, 2)-9*fieldname1+36

    Best regards.

    Thread Starter hheyhey568

    (@hheyhey568)

    Thank you , however I mean to say that we need to find the value of X from the following Cubic equations.. Sometimes it has 3 roots ( means answer) or 1 root.

    x3-4×2-9x+36=0

    Following page gives the code in JavaScript.
    https://gist.github.com/weepy/6009631

    If we use calculator , it has function to calculate the roots of the cubic equation.

    • This reply was modified 4 years, 2 months ago by hheyhey568.
    Plugin Author codepeople

    (@codepeople)

    Hello @hheyhey568

    In this case, you can insert an “HTML Content” field in the form, and enter the function as its content:

    <script>function CubicSolve(a, b, c, d){
    
      b /= a;
      c /= a;
      d /= a;
    
      var discrim, q, r, dum1, s, t, term1, r13;
    
      q = (3.0*c - (b*b))/9.0;
      r = -(27.0*d) + b*(9.0*c - 2.0*(b*b));
      r /= 54.0;
    
      discrim = q*q*q + r*r;
      
      var roots = [ {real: 0, i: 0}, {real: 0, i: 0}, {real: 0, i: 0} ]
      
      term1 = (b/3.0);
    
      if (discrim > 0) { 
       s = r + Math.sqrt(discrim);
       s = ((s < 0) ? -Math.pow(-s, (1.0/3.0)) : Math.pow(s, (1.0/3.0)));
       t = r - Math.sqrt(discrim);
       t = ((t < 0) ? -Math.pow(-t, (1.0/3.0)) : Math.pow(t, (1.0/3.0)));
       
       roots[0].real = -term1 + s + t;
       term1 += (s + t)/2.0;
       roots[1].real = roots[2].real = -term1;
       term1 = Math.sqrt(3.0)*(-t + s)/2;
       
       roots[1].i = term1;
       roots[2].i = -term1;
       return roots;
      } 
    
      if (discrim == 0){
       r13 = ((r < 0) ? -Math.pow(-r,(1.0/3.0)) : Math.pow(r,(1.0/3.0)));
       roots[0].real = -term1 + 2.0*r13;
       roots[2].real = roots[1].real = -(r13 + term1);
       return roots;
      }
    
      q = -q;
      dum1 = q*q*q;
      dum1 = Math.acos(r/Math.sqrt(dum1));
      r13 = 2.0*Math.sqrt(q);
      
      roots[0].real = -term1 + r13*Math.cos(dum1/3.0);
      roots[1].real = -term1 + r13*Math.cos((dum1 + 2.0*Math.PI)/3.0);
      roots[2].real = -term1 + r13*Math.cos((dum1 + 4.0*Math.PI)/3.0);
      
      return roots;
    }</script>

    And now, the new function can be called from the equations in the calculated fields like the plugin operations:

    (function(){
    var result = CubicSolve(1,-4,-9,36);
    return 'x = '+result[0]['real']+', x = '+result[1]['real']+', x = '+result[2]['real'];
    })()

    Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Cubic equation’ is closed to new replies.