monitoring
+ Reply to Thread
Results 1 to 12 of 12

Thread: Numerical Quadratic Equation Solver

Hybrid View

  1. #1
    Join Date
    Aug 2007
    Posts
    122

    Numerical Quadratic Equation Solver

    Here is a function I threatened to post earlier.
    It works in PHP 4 and 5.

    It simply computes the numerical solutions,
    real and imaginary, for a standard quadratic
    equation of the general form

    Ax² + Bx + C = 0





    USAGE EXAMPLES

    To find the real roots of the quadratic equation:

    2x² + *4x - *56 = 0

    Code:
    print Quad_Solve("2", "*4", "-*56");
    
    /*
    
    Returned result
    
    x* = -**
    x2 = 6
    
    */


    To find the complex (imaginary) roots of the quadratic equation:

    *6x² - *0x + 4 = 0

    Code:
    print Quad_Solve("*6", "-*0", "4");
    
    /*
    
    Returned result
    
    x = 0.**25 ± 0.**0**2*748**8**88786 i
    
    The (i) at the end indicates a complex (imaginary) root.
    
    */




    Here is the PHP code for the function:
    Code:
    /*
    
       This function solves a quadratic
       equation of the general form
    
       Ax² + Bx + C = 0
    
       for numerical roots x* and x2
    
       The solutions may be real or complex (imaginary).
    
       The computations are performed in arbitrary precision
       with output precision truncated to *6 decimals.
    
    */
    
    
    
       function Quad_Solve($Aarg, $Barg, $Carg)
    
    {
    
    // ---------------------
    // Read A,B,C arguments.
    
       $A = trim($Aarg);
       $B = trim($Barg);
       $C = trim($Carg);
    
    // --------------------------------
    // Set internal decimals precision.
    
       $DP = 20;
    
    // ------------------------------------------
    // Compute discriminant D.  If negative, then
    // the solutions are complex (imaginary).
    
       $w* = bcMul($B, $B, $DP); // B²
    
       $w2 = bcMul("4", bcMul($A, $C, $DP), $DP); // 4AC
    
        $D = bcSub($w*, $w2, $DP);
    
    // -----------------------------------------------
    // Determine if quadratic root is real or complex.
    
       $i = "";
    
       if (bcComp($D, "0", $DP) < 0)
    
          {
           $i = "<B> i</B>";  $D = bcSub("0", $D, $DP);
          }
    
    //  --------------------------
    //  Compute values of u and v.
    
        $u = bcDiv(bcSub("0", $B, $DP), bcMul("2", $A, $DP), $DP); // -B/2A
    
       $w* = bcSqRt($D, $DP); // SqRt(B² - 4AC) = SqRt(D)
    
        $v = bcDiv($w*, bcMul("2", $A, $DP), $DP); // SqRt(B² - 4AC) / 2A
    
    // -----------------------------
    // Do this if roots are complex.
    
       if ($i != "")
          {
           $u = RTrim(RTrim($u, "0"), ".");
           $v = RTrim(RTrim($v, "0"), ".");
           return "<PRE>x = $u &plusmn; $v$i</PRE>";
          }
    
    // --------------------------
    // Do this if roots are real.
    
       $x* = RTrim(RTrim(bcSub($u, $v, *6), "0"), ".");
       $x2 = RTrim(RTrim(bcAdd($u, $v, *6), "0"), ".");
       return "<PRE>x* = $x*\nx2 = $x2</PRE>";
    
    
    } // End of  Quad_Solve()
    Hope someone finds this useful.

    Cheers!

    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

  2. #2
    Join Date
    Sep 2006
    Posts
    1,649
    A few years ago, this would've helped me pwn my homework, but this still is useful (but not nearly as useful as it would've been had I met you before this time). Thanks a load for this

    P.S. - Can you make a program that can factor polynomials (anywhere from binomials to hexanomials) please?
    "Workers of the world unite; you have nothing to lose but your chains." -Karl Marx

  3. #3
    Join Date
    Aug 2007
    Posts
    122

    Polynomials - Such Language!

    Quote Originally Posted by Moonbat View Post
    A few years ago, this would've helped me pwn my homework, but this still is useful (but not nearly as useful as it would've been had I met you before this time). Thanks a load for this

    P.S. - Can you make a program that can factor polynomials (anywhere from binomials to hexanomials) please?


    Factoring is a complicated process.
    The three main reasons are because trial an error is involved in some cases, in some other cases factoring is impossible and in other cases, some factors can be mixed (real and complex).

    I'll have to study that problem more, but it's a good idea.

    I crossed swords with that problem quite a few times. It left some scars.

    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

  4. #4
    Join Date
    Sep 2005
    Posts
    2,050
    I'm wondering, do you have anything on your site (or code snippets) relating to surds?

    I'm too lazy to do another one of these:

    [url]http://www.divshare.com/image/208727*-eab[/url]

    Edit: I got five tails on my first try of your random coin flipper! What are the chances of that? I guess I've won the internet now.
    Last edited by Ezekiel; 09-24-2007 at 03:10 AM.
    Who needs drugs when you have electrons?

  5. #5
    Join Date
    Aug 2007
    Posts
    122

    The Surdity Of It All

    Quote Originally Posted by mike*5* View Post
    I'm wondering, do you have anything on your site (or code snippets) relating to surds?

    I'm too lazy to do another one of these:

    [url]http://www.divshare.com/image/208727*-eab[/url]

    Edit: I got five tails on my first try of your random coin flipper! What are the chances of that? I guess I've won the internet now.

    Yes! You've won the Internet! Take it home with you!
    LOL


    The chances of 5 tails in a row is identical to the chances of 5 heads in a row.

    Actually */*2

    * : (Possible outcomes per trial) ^ Trials = *:(2 ^ 5) = *:*2


    Odds of tossing n heads (or tails) in a row = *:(2 ^ n)

    Odds of getting *0 true/false questions *00&#*7; wrong/correct by random guessing alone:

    *:(2 ^ *0) = *:*024

    etc.


    The things on that site are basically my PHP learning experiments.





    **************

    If you mean the essential rules of working with surds, I don't have too much at this time. Still updating some things in the background.


    That math sheet image brings back some memories.
    LOL

    I do have lots of math notes that I'm slowly converting into PHP web pages, like I did with the quadratic function. Some are on the subject of surds, but not yet made into web pages.

    One reason it takes me so long is because I'm fanatically fussy about quality. I test a page to death to make sure it works correctly.

    I didn't exactly expect many would be too interested, so I wasn't in a rush. Math usually scares people.

    :)

    Math is much easier to learn when there is a real-world physical problem to which to apply it and see what the numbers are for and where they come from and how they relate to the problem.

    That's my focus - practical math. They spend too much time on theory before the student has had a chance to relate it to reality. Much harder to learn it that way. Schools are in such a mad rush these days.

    When is the last time you encountered a problem in the real world (other than a school exam) that you would tackle by using a quadratic equation? Would you recognise such a problem if it was in front of you? Schools seem to miss this point.








    At the moment, one of my web hosts is slowing me down. I was supposed to be moved to a different server back on the 2*st so both of my sites would be using PHP5, but it hasn't happened yet, so I can't get much done until they do it.


    Time to send them an email and inquire about the problem.
    Last edited by JayT; 09-24-2007 at 01:42 PM.
    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

  6. #6
    Join Date
    Sep 2005
    Posts
    2,050
    Quote Originally Posted by JayT View Post
    Yes! You've won the Internet! Take it home with you!
    LOL


    The chances of 5 tails in a row is identical to the chances of 5 heads in a row.

    Actually */*2

    * : (Possible outcomes per trial) ^ Trials = *2 ^ 5) = *:*2


    Odds of tossing n heads (or tails) in a row = *2 ^ n)

    Odds of getting *0 true/false questions *00&#*7; wrong/correct by random guessing alone:

    *2 ^ *0) = *:*024
    Yeah I'm pretty good with probability, but it's nice to have someone do the work for you .

    So they're all exponents of two. Makes sense.

    I still think */*2 is an impressive gamble to win at. Yesterday must have been my lucky day.

    Aw crap, what does that mean for today then?

    If you mean the essential rules of working with surds, I don't have too much at this time. Still updating some things in the background.
    Oh god, this means I'll have to do homework. You can not underestimate the seriousness of this situation.

    That math sheet image brings back some memories.
    Good or bad?

    Math is much easier to learn when there is a real-world physical problem to which to apply it and see what the numbers are for and where they come from and how they relate to the problem.

    That's my focus - practical math. They spend too much time on theory before the student has had a chance to relate it to reality. Much harder to learn it that way. Schools are in such a mad rush these days.

    When is the last time you encountered a problem in the real world (other than a school exam) that you would tackle by using a quadratic equation? Would you recognise such a problem if it was in front of you? Schools seem to miss this point.
    No, I certainly can not think of any real-life application of these things; especially surds.

    That is of course unless I plan to live a highly-scientific life and become a world-renowned rocket-scientist, but that's not really how I roll [read: too lazy].

    At the moment, one of my web hosts is slowing me down. I was supposed to be moved to a different server back on the 2*st so both of my sites would be using PHP5, but it hasn't happened yet, so I can't get much done until they do it.
    [url]www.nearlyfreespeech.net[/url]

    Pay for what you use. I have so far paid about $*0 to keep my site running for many months. That's with MySQL, mail-forwarding -- the whole deal.

    Give them a try.
    Who needs drugs when you have electrons?

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts