same code, BASIC and PHP, PHP version doesn't work. (solved)
Posted: Wed Jun 09, 2010 8:33 am
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
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
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);
}
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