Logic help (a trial and error script)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Logic help (a trial and error script)

Post by Chris Corbyn »

Hi,

OK my situation is a lot more complex so I'll probably pick a very bad example but it's the logic I can't figure.

Lets say for example I have a function:

Code: Select all

function parabola($x) {
    $y = -1 * $x * $x + 10;
    return $y;
}
Now *imagine* you don't know what the operation is in that function.... all you know is that it outputs a parabola which intercepts the X axis (but we don't know the values).

Image

I want to use trial and error to find what value(s) I input to be within 0.01 of 0.

With me so far?

I need to work out the logic for getting PHP to do the trial and error for me.

I also *know* that these values lies somewhere between -100 and +100 and we also know that going back as far as -100 we are guarunteed to be below the X axis.

I can use two loops if necessary (one that goes forwards and one which goes backwards) to get each point.

I want to avoid writing a whole function to do this. It's going to be part of a single function which *ideally* I dont want to be dependant upon other functions.

The plan was initially to use a for() loop.

Code: Select all

//This should get the first X intercept to within 0.01
$increment = 1;
$i = false;
$intercepted = false;
for ($x=-100; $x<=100; $x+=$increment) {
    $y = parabola($x);
    if ($i == true) {
        $intercepted = true;
    }
    
    if (($y/abs($y)) == +1) { //Now above the X axis
        $i = true;
        $increment *= -1; //Track backwards again
    }
    
    if ($intercepted == true) {
        if (($y/abs($y)) == +1) { //Above axis
            //Do something to the increment to home in on the value
        } else {
            //Do soemthing else to the increment to home in on the value
        }
    }

    if (abs($y) < 0.01) {
        echo $y;
        break;
    }
}
Anybody ever done anything like this before?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

The only way I can think of is to return the integer closest to it. Simply record $y as $closest and check if the next $y is closer or not.

I don't know why you can't know what goes on inside the function. If you ask me, you should calculate the intercepts within the function and return an array of data.
e.g.
return array($y, $i[1], $i[2], $i[n]);
(where $i[?] is an intercept)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i understand it right:

you have a parabolic function, general function prescription is:
f(x) = ax² + b

you know a and b
you want to find out where f(x) = 0 ?

---------------------------------------------------------------------
this comes down to solving the equation:

in the case where a != 0
0 = ax² + b <=> -b/a = x² <=> x = + square(-b/a) or x = - square(-b/a)

in the case where a = 0
0 = 0 x + b => b = 0 <=> the function is always 0 (if b != 0, you have a false equation)


You also need to calculate where the function becomes 0.1 in the same way.


=> Because a parabol is continuous, you can conclude that all the values between those two pairs are in the interval you want...
Last edited by timvw on Mon May 02, 2005 5:19 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Like I say my situation is far more complex (I wont even burden you with it).

I know it can done be right up to nearest 0.01 or whatever. You just need to keep backtracking and changing the amount you move by until you have the value within the correct range.

Lets call this a "Homing in on a value script" ;-)

EDIT | Gimme a chance to read timvw's post (He beat me to it) ;-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

timvw.... I know how to do algebra :P

My scenario is actually that I do not know the eqaution I am solving (I actually do but it's over 150 lines and it's too technical for me to understand (and probably most other non-physicists)...

I really just want to work out how to get the logic for doing it by trial and error (if even just for curiosity now).

I seriously think if I posted the Mathematics and somebody tried to solve the equation(s) by other means everybody would run away...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Well, i have re-read everything. It appears you only know that the function is a parabol. But you don't know how the a and b from the general f(x) = ax²+b look like.

you can find b easily because: f(0) = a.0 + b <=> b = f(0).
and now you can calculate a too: f(1) = a.1 + b <=> a = f(1) - b


I would suggest to post the maths, if we run away, that's our problem :) (Because i think i'm still not getting the problem...)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I have done this another way (by trial and error but with a different requirement - more specific to my situation).

Thanks anyway ;-)
Post Reply