Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Algebra

  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Algebra

    Is there a way to get php to solve for a variable?

    Ex,
    1.1x=110
    output to screen would be what x is
    Last edited by PureMisery; 01-03-2006 at 03:05 PM.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Brute force? I'm pretty sure that's how the fancy calculators do it.

    PHP Code:
        function solveForX($equation$answer$min null$max null)
        {
            
    $min = ($min == null) ? -mt_getrandmax() : $min;
            
    $max = ($max == null) ? mt_getrandmax() : $max;
            
    $last['guess'] = null;
           
            while (
    true)
            {
                eval(
    '$attempt = ' str_replace('x', ($guess mt_rand($min$max)), $equation) . ';');
                if (
    $attempt == $answer)
                {
                    return(
    $guess);
                }
                if (
    $last['guess'] != null)
                {
                    
    $diff abs($answer $attempt);
                    if (
    $diff $last['diff'])
                    {
                        if (
    $guess $last['guess'])
                        {
                            
    $max $guess 1;
                        }
                        else
                        {
                            
    $min $guess 1;
                        }
                    }
                }
                
    $last['guess'] = $guess;
                
    $last['diff'] = abs($answer $attempt);
            }
        }
        
        echo 
    solveForX('x+(x*.1)'110); 
    Note this is oversimplified and will fall into infinite loops on discontinuous functions and functions with noninteger answers. The latter problem is pretty easily resolvable, the former maybe not so much.

    Also, it'd probably be neater if it was refactored to use recursion, another exercise I'll leave to the reader.

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    There's some good classes that help handle this.

    http://www.phpclasses.org/browse/class/65.html

    Check there for some.

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kurashu
    There's some good classes that help handle this.

    http://www.phpclasses.org/browse/class/65.html

    Check there for some.
    Thanks, I'll go look around there now.


    <---edit--->
    I found one that I think will do what I need. But how do use the files?
    Last edited by PureMisery; 01-03-2006 at 05:19 AM.


  •  

    Posting Permissions

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