Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Reached's avatar
Level 11

Equation with unknown in PHP

Hi guys,

I am trying to solve an equation, where I have all the results given to me in Excel, but I don't know how to get to the same results in PHP since $unknownVariable essentially is part of the calculation.

$unknownVariable = 1 / pow(2 * log(2.51 / (28294.21211 * sqrt($unknownVariable) + 0.27 * 0.0008)), 2);

// The end result should be: 0,02577574

In Excel the result is given by iterating, but how can I do the same?

Sorry for my lack of correct math terms here :D

0 likes
36 replies
Sinnbeck's avatar

What is the initial value of $unknownVariable ?

Sinnbeck's avatar

And im not getting the excel part? You get the results from excel? But why do you need to calculate it if you have the result already?

Reached's avatar
Level 11

@Sinnbeck It's because I am converting the Excel to a php-app, thus I need to convert the formulas to php :)

Sorry if that was unclear!

Reached's avatar
Level 11

@Sinnbeck Yes :)

So this is a part of the Excel sheet from which I need to calculate stuff, the derived values are calculated in other formulas, however they are not relevant to this I believe.

The formula for calculating λ is the one I pasted in the opening post, but has been substituted with the values that you see in the Excel spreadsheet here:

https://pasteboard.co/lWpgO2URebTS.png

1÷POWER(2×LOG(2,51÷(A21×SQRT(G21))+0,27×B21);2)

Where G21 is the result of the above

Sinnbeck's avatar

That row is probably row 22 as it checks row 21. So scroll to row 1 and see what it has there. It most likely does not have G0. Instead it probably has the starting value

Reached's avatar
Level 11

@Sinnbeck There is nothing there (as far as an initial value goes), but I asked the client about it, so hopefully I will have it soon :)

Reached's avatar
Level 11

The formula for all the cells is the same as posted previously (also for the first cell). But it seems like the initial value is some kind of "guess"

tykus's avatar

I assume you want to use recursion (assuming the $unknownVariable inside the formula is an estimate from a previous iteration)?

function calculate($previousEstimate) {
    $newEstimate = 1 / pow(2 * log(2.51 / (28294.21211 * sqrt($previousEstimate) + 0.27 * 0.0008)), 2);

    // check if the newEstimate is "close enough"; i.e. within N significant digits
    if (abs($newEstimate - $previousEstimate) < 0.0001)
       return $newEstimate;
    }
    return calculate($newEstimate);
}
// use some original estimate to begin your computation
calculate(1);
Reached's avatar
Level 11

@tykus Yes I believe this is very close to what I need actually. I am not sure what my initial value should be though, as it seems like no results are returned when setting it to 1 or even 0.0001

Essentially I need to calculate a whole bunch of different values depending on the user input, and then use those calculations for some other calculations, so this method will be running a loop :)

Thanks so far!

tykus's avatar

@Reached I don't know what a original estimate might be - or how this equation might converge on its solution, so pick a reasonable original estimate.

Ultimately, because the equation needs an initial value for $unknownVariable; you must give it some matching value! How is this actually achieved in Excel; can you post the Excel formula here?

Reached's avatar
Level 11

@tykus I posted a part of the excel sheet further up in a reply to Sinnbeck :)

The Excel sheet iterated to find the value (not something I know too much about unfortunately) :/

The Excel formula is as follows:

1÷POWER(2×LOG(2,51÷(A21×SQRT(G21))+0,27×B21);2)

G21 is the result of the above

tykus's avatar

@Reached

G21 is the result of the above

Ok, so recursion is a viable solution. What does the Excel sheet do in the first row (where there is no value in the previous row) - this will be the initial estimate?

My solution takes no account of the B21 cell value(s) - is this a computed value as well? Note, you would also need to pass this into the recursive function.

Reached's avatar
Level 11

@tykus Awesome, I think we are getting there!

There are no other values of relevance above the input (all cells are empty), and I can't seem to find any other value. I will try to ask the client if they have a number I could use as the initial value

Yes B21 (0.0008) is also a computed value, based on user input. I actually already adapted the recursive function you provided to be able to swap out the relevant user-input

tykus's avatar

@Reached but there must be something in the formula on very first row that is different from subsequent rows - there is no previous value to work with there!

tykus's avatar

Looking at the screenshot again; this looks like a fluid dynamics problem; why is the previous result valid for different set of flow parameters? It is a long time since I was a mechanical engineer...

I wonder if recursion is the correct approach here?

Reached's avatar
Level 11

@tykus This is exactly what it is yes.

It's attempting to calculate the pressure drop in pipes given some different values, and to calculate the pressure drop, I need ƛ :D

I am sorry that I am not of more help, but I am not a mechanical engineer myself :/

I asked them for the initial number, as there is nothing to find in the Excel sheet from what I can tell

tykus's avatar

@Reached is there a formula in the cell where the very first ƛ value appears; can you share it? Just for reference let us know what any cell references refer to.

Reached's avatar
Level 11

@tykus The formula for all the cells is the same as posted previously (also for the first cell). But it seems like the initial value is some kind of "guess"

Reached's avatar
Level 11

Okay, so there is some news:

I was told that the starting value is always under 1, but that it's not given, so I should start with a very small number as the default value

tykus's avatar

@Reached okay, so there is something to work with if you can make an initial guess...

The other consideration will be how you end the recursive behaviour - I suggested a "precision" value which will trigger as the estimates converge. This won't be the case in your Excel example; you will have a finite set of data to work with (different flow rates / conditions); so then iteration rather than recursion would be preferable.

Reached's avatar
Level 11

@tykus Yea, the guess is harder than it looks, hmm.. I just can't seem to find a value that will make it work

Do you mean replacing the recursive method you provided earlier with an iteration instead?

Reached's avatar
Level 11

So here is a very dumbed down version of what I've got so far:

function calculate($estimate, $reynoldsNumber, $kd) {
    return 1 / pow(2 * log(2.51 / ($reynoldsNumber * sqrt($estimate) + 0.27 * $kd)), 2);
}

Route::get('calculate', function() {
    $flows = [10, 15, 21, 27];
    $calculated = [];
    $initialValue = 0.00001;

    foreach($flows as $flow) {
        // Constants
        $viscocity = 0.001; // ϑ /roughness

        // Variables the user can change
        $tubeLength = 20; // m
        $tubeDiameter = 125; // mm
        $kgm3 = 1000; // ƍ - kg/m3
        $kd = $viscocity / $tubeDiameter;

        // Calculations
        $areaM2 = pi() * pow(($tubeDiameter / 2) / 1000, 2);
        $speedMs = ($flow / 3600) / $areaM2;
        $reynoldsNumber = $speedMs * ($tubeDiameter / 1000) * $kgm3 / $viscocity;

        $calculated[] = calculate($initialValue, $reynoldsNumber, $kd);
    }

    return $calculated;

    // $calculated:

   [
        0.01957545318891809,
        0.01578931453909297,
        0.013423221338202658,
        0.011986528471523473
]

But I would expect something like this:

[
    0.02577574,
    0.024054954,
    0.022884916,
    0.022147663
]
Reached's avatar
Level 11

@tykus Another update here after having spoken to my contact person :)

The way it works in excel, is that you choose a very small starting value (basically random), and then it runs the calculation up till a 100 times (depending on your settings) on a number whenever you press "Calculate". They call it "Iterative calculation". You also set a max deviation for the calculations (something like 0.001).

I am not completely sure how to translate this to the php side. My guess is that I should use a foreach loop which runs 100 times, but I would need some way to increment the value inside the loop, otherwise the calculation will always return the same? And I would also need to check it against the deviation (sort of how you did in the recursive function)

I really appreciate your help!

Reached's avatar
Level 11

I rewrote the function a bit so that it iterates instead. However it seems to make no difference whether I use 1 or 100 iterations, so something is obviously not working the way I want it to, can anyone see any obvious mistakes?

function iterate($previousNumber, $reynoldsNumber, $kd) {
    $iterations = 100;
    $threshold = 0.0001;
    $previousValue = $previousNumber;
    $array = [];

    for($i = 0; $i < $iterations; $i++) {
        // Find the next value of the iteration
        if(count($array)) {
            $previousValue = $array[count($array) - 1];
        }

        $array[] = $newEstimate = 1 / pow(2 * log(2.51 / ($reynoldsNumber * sqrt($previousValue) + 0.27 * $kd)), 2);

        // Check if the difference between the previous and current value is less than the threshold
        if(abs($newEstimate - $previousValue) < $threshold) {
            return $newEstimate;
        }
    }
}
// Outputs the following (regardless of number of iterations)
//0.0055242147063832
jlrdw's avatar

@Reached also and just FYI there are python packages that handle this.

jlrdw's avatar

@Reached no not fluid dynamics, but I have written tube bending software. I've seen similar type questions on the freecad forum.

Please or to participate in this conversation.