Page 1 of 1

same code, BASIC and PHP, PHP version doesn't work. (solved)

Posted: Wed Jun 09, 2010 8:33 am
by nskeim
This is my first time posting, so I hope I'm in the right place...

I found a function somewhere on the web, which I can't remember (or even find anymore) exactly where. The function was written in BASIC. I have this function integrated into a Microsoft Excel spreadsheet and it works exactly as I had hoped, intended, etc. Point is, the BASIC code works perfectly.

Now, I want to integrate this function into a PHP application that I am throwing together and I have translated, I believe, the BASIC function into PHP correctly. Problem is that the PHP function does not work. I say it does not work because whereas the BASIC function will output the correct Mach No. (this function calculates the mach number at the exit of a delaval nozzle) the PHP function throws errors that ($macho - $machn) = 0 and that $deriv = 0 (cannot divide by zero). I have tried breaks in the function to escape the loop if these two values approach 0, thinking that if $macho and $machn are the same then obviously I must have calculated the correct solution, WRONG! If the breaks are in place then the solution is always Mach = 2.25, which is incorrect.

For the purposes of testing I fixed the inputs of the Nozzle AreaRatio and the ratio of specific heats of the fluid (gamma) to be the same in both functions all the time. Here are the two functions, BASIC first, then PHP.

BASIC FUNCTION
Function getMachArat(AreaRatio, gamma)
AreaRatio = 36.9
gamma = 1.22
gm1 = gamma - 1
gp1 = gamma + 1
fac1 = gp1 / (2 * gm1)

aro = 2
macho = 2.2
machn = macho + 0.05

While (Abs(AreaRatio - aro) > 0.0001)
fac = 1 + 0.5 * gm1 * machn * machn
arn = 1 / (machn * (fac ^ (-fac1)) * ((gp1 / 2) ^ fac1))
deriv = (arn - aro) / (machn - macho)
aro = arn
macho = machn
machn = macho + (AreaRatio - aro) / deriv
Wend
getMachArat = macho

PHP FUNCTION

Code: Select all

function getMachArat($AreaRatio, $gamma) {
      $AreaRatio = 36.9;
      $gamma = 1.22;
      $gm1 = $gamma - 1;
      $gp1 = $gamma + 1;
      $fac1 = $gp1 / (2 * $gm1);

      $aro = 2.0;
      $macho = 2.2;
      $machn = $macho + 0.05;

      While ((abs($AreaRatio - $aro)) > 0.0001) {
        $fac = 1 + 0.5 * $gm1 * $machn * $machn;
        $arn = 1 / ($machn * ($fac ^ ($fac1*(-1))) * (($gp1 / 2) ^ $fac1));
		//if ((abs ($machn - $macho)) < 0.000001) {
		//	break; // to avoid divide by zero errors, at this point the area ratio and mach number should be calculated
		//           }
        $deriv = ($arn - $aro) / ($machn - $macho);
		//if ((abs ($deriv)) < 0.000001) {
		//	break; // to avoid divide by zero errors, at this point the area ratio and mach number should be calculated
		//	}
        $aro = $arn;
        $macho = $machn;
        $machn = $macho + ($AreaRatio - $aro) / $deriv;
        }
      return($macho);
      }
Can anyone please help me solve this problem? Does Excel do something "smart" that is making the BASIC function work whereas the PHP function does not. Or am I missing something that I've done stupidly when translating from BASIC to PHP?

If it matters I am using PHP 5.2.8 as shipped in the XAMPPlite distribution version 1.7.0

Any thoughts on this matter are EXTREMELY appreciated.
-Nick

Re: same code, BASIC and PHP, PHP version doesn't work. why?

Posted: Wed Jun 09, 2010 9:26 am
by nskeim
wow, I've been banging my head against the wall for days, and I finally figured it out, I guess the threat of public humiliation was a good motivator...

as I am sure you all know (can't believe I didn't), the exponent operator ^ is not actually the exponent operator in PHP!
I replaced all my exponents with the pow function and it works! duh!

code below in case anyone wants a function that calculated the mach number of the flow at the exhaust of a nozzle given the nozzle area ratio and the ratio of specific heat of the fluid.

Code: Select all

	function getMachArat($AreaRatio, $gamma) {
	  $gm1 = $gamma - 1;
      $gp1 = $gamma + 1;
      $fac1 = $gp1 / (2 * $gm1);
      $aro = 2.0;
      $macho = 2.2;
      $machn = $macho + 0.05;
      While ((abs($AreaRatio - $aro)) > 0.0001) {
        $fac = 1 + 0.5 * $gm1 * $machn * $machn;
        $arn = 1 / ($machn * pow($fac,(-$fac1)) * (pow(($gp1 / 2),$fac1)));
		if ((abs ($machn - $macho)) < 0.000001) {
			break; // to avoid divide by zero errors, at this point the area ratio and mach number should be calculated
			}
        $deriv = ($arn - $aro) / ($machn - $macho);
		if ((abs ($deriv)) < 0.000001) {
			break; // to avoid divide by zero errors, at this point the area ratio and mach number should be calculated
			}
        $aro = $arn;
        $macho = $machn;
        $machn = $macho + ($AreaRatio - $aro) / $deriv;
		}
      return($macho);
	}

Re: same code, BASIC and PHP, PHP version doesn't work. (sol

Posted: Wed Jun 09, 2010 9:56 am
by AbraCadaver
Glad you found it. I don't know your formula but I was wondering why the XORs :-)