Location: PHPKode > scripts > GG-Tracker GPS and GSM Tracking > gg-tracker-gps-and-gsm-tracking/math_functions.php
<?php 

//***************************************
//*** Various mathematical functions ****
//***************************************

/* Function to calculate the variance, it's needed to asses the quality of data
   Source: http://www.ajdesigner.com/php_code_statistics/variance_sample.php
*/
function variance ($a)
{
  //variable and initializations
  $the_variance = 0.0;
  $the_mean = 0.0;
  $the_array_sum = array_sum($a); //sum the elements
  $number_elements = count($a); //count the number of elements

  //calculate the mean
  $the_mean = $the_array_sum / $number_elements;

  //calculate the variance
  for ($i = 0; $i < $number_elements; $i++)
  {
    //sum the array
    $the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
  }

  $the_variance = sqrt($the_variance / ($number_elements - 1.0));

  //return the variance
  return $the_variance;
}

/* Adapted from the code by Sklar David, "Calculate the great circle distance between two latitude/longitudes"
http://www.weberdev.com/get_example.php3?count=357&mode=text
*/
function great_circle_distance($lat1, $lon1, $lat2, $lon2)
{
	/*  assume  your  points,  in  decimal,  are  in  $lon1,$lat1  and  $lon2,$lat2 
	 */ 

	$pi  =  3.1415926; 
	$rad  =  floatval($pi/180.0); 


	$lon1  =  floatval($lon1)*$rad;  $lat1  =  floatval($lat1)*$rad; 
	$lon2  =  floatval($lon2)*$rad;  $lat2  =  floatval($lat2)*$rad; 

	$theta  =  $lon2  -  $lon1; 
	$dist  =  acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta)); 
	if ($dist < 0) {
		$dist += $pi;
	} 
	$dist  =  round($dist  *  6371.2, 3); // kilometers
	/*
	$miles  =  floatval($dist  *  0.621); 
	$inches  =  floatval($miles*63360);
	*/
	return $dist;
}


?>
Return current item: GG-Tracker GPS and GSM Tracking