php/Math   
Recreational Mathematics   
   home  |  library  |  contact
 Math Notes
 Math Programming [25]
 Regression [3]
 Data Mining [17]
 Notation [6]
 Linear Algebra [9]
 Stats & Prob [15]
 Math Cognition [5]
 Space & Physics [6]
 Formulas [5]
 Fun & Games [2]
 Haskell [1]
 Bayes Theory [1]
 Site News [0]
 Math Projects [5]
 Polynomials [1]
 Calculus [9]
 Number Theory [3]
 Optimization [2]
 Financial [1]

 Math Links
 PHP/ir
 Andrew Gelman
 Chance Wiki
 Daniel Lemire
 KD Knuggets
 Social Stats
 MySQL Performance
 Hunch.net
 Matthew Hurst
 JMLR
 JSS
 Hal Daume III
 Math Notes >> Math Projects

Build your own tetrahedron [Math Projects
Posted on October 22, 2012 @ 04:30:35 PM by Paul Meagher


Credits: http://geoite.com/msu/albums/userpics/10002/Tetrahedron-cutout.jpg

To render a 3D tetrahedron, you can download the latest version of Processing (2.0 beta 4 as of this writing) and load this code into it:

# Jennie Meyer
# http://faculty.purchase.edu/jeanine.meyer/processing/tetrahedron/applet/tetrahedron.pde

float side = 100;
float rad60=60*PI/180;

float rotx = PI/4;
float roty = PI/4;

void setup() {
  size(800,600,P3D);
}
void draw() {
  background(0);
  translate(width/2.0, height/2.0, -100);
  rotateX(rotx);
  rotateY(roty);
  
  drawtetrahedron();
   
}
void drawtetrahedron() {
   beginShape(TRIANGLES);
   vertex(-side/2,0,0);
   vertex(0,sin(rad60)*(-side),0);
   vertex(side/2,0,0);    
  endShape();
  beginShape(TRIANGLES);
   vertex(-side/2,0,0);
   vertex(0,sin(rad60)*(-side)*.5,sin(rad60)*(side));
   vertex(side/2,0,0);    
  endShape();
   beginShape(TRIANGLES);
   vertex(-side/2,0,0);
   vertex(0,sin(rad60)*(-side)*.5,sin(rad60)*(side));
   vertex(0,sin(rad60)*(-side),0);    
  endShape();
    beginShape(TRIANGLES);
     vertex(0,sin(rad60)*(-side),0);
     vertex(0,sin(rad60)*(-side)*.5,sin(rad60)*(side));
       vertex(side/2,0,0);
  endShape();
  
}
void mouseDragged() {
  float rate = 0.01;
  rotx += (pmouseY-mouseY) * rate;
  roty += (mouseX-pmouseX) * rate;
  //println("rot x is "+rotx*180/PI+"degrees and roty is "+roty*180/PI);
}

There is a javascript-based implementation of the Processing language started by John Resig (JQuery founder) and now carried by other developers. The Java version is ahead of the Javascript version of the language in terms of featureset right now. Would be interesting to see the javascript version move more towards parity with the Java version.

My inspiration for learning more about tetrahedrons was a picture of Alexander Graham Bell reclining in an outdoor shack in the shape of a tetrahedron, a shape he considered to be structural perfection.


Credits: Maria McGowan, http://therightcoastnovascotia.blogspot.ca/2009/08/visit-to-alexander-graham-bell-national.html

Permalink 

PHP Excel Classes 2007 [Math Projects
Posted on April 1, 2008 @ 10:10:35 AM by Paul Meagher

The opensource PHP Excel Classes 2007 Project aims to provide "a set of classes for the PHP programming language, which allow you to write to Excel 2007 files and read from Excel 2007 files. This project is built around Microsoft's OpenXML standard and PHP". What impresses me about this project is the inclusion of a formula parser and calculation engine that mimics much of Excel's functionality. I can see a use for the formula parser and the calculation engine in other projects that don't involve Excel.

Much of the functionality associated with the calculation engine is located in the PHPExcel_Calculation_Functions.php class which includes full implementations of the following 352 math functions:

  • ABS
  • ACCRINT
  • ACCRINTM
  • ACOS
  • ACOSH
  • ADDRESS
  • AMORDEGRC
  • AMORLINC
  • AND
  • AREAS
  • ASC
  • ASIN
  • ASINH
  • ATAN
  • ATAN2
  • ATANH
  • AVEDEV
  • AVERAGE
  • AVERAGEA
  • AVERAGEIF
  • AVERAGEIFS
  • BAHTTEXT
  • BESSELI
  • BESSELJ
  • BESSELK
  • BESSELY
  • BETADIST
  • BETAINV
  • BIN2DEC
  • BIN2HEX
  • BIN2OCT
  • BINOMDIST
  • CEILING
  • CELL
  • CHAR
  • CHIDIST
  • CHIINV
  • CHITEST
  • CHOOSE
  • CLEAN
  • CODE
  • COLUMN
  • COLUMNS
  • COMBIN
  • COMPLEX
  • CONCATENATE
  • CONFIDENCE
  • CONVERT
  • CORREL
  • COS
  • COSH
  • COUNT
  • COUNTA
  • COUNTBLANK
  • COUNTIF
  • COUNTIFS
  • COUPDAYBS
  • COUPDAYSNC
  • COUPNCD
  • COUPNUM
  • COUPPCD
  • COVAR
  • CRITBINOM
  • CUBEKPIMEMBER
  • CUBEMEMBER
  • CUBEMEMBERPROPER
  • CUBERANKEDMEMBER
  • CUBESET
  • CUBESETCOUNT
  • CUBEVALUE
  • CUMIPMT
  • CUMPRINC
  • DATE
  • DATEDIF
  • DATEVALUE
  • DAVERAGE
  • DAY
  • DAYS360
  • DB
  • DCOUNT
  • DCOUNTA
  • DDB
  • DEC2BIN
  • DEC2HEX
  • DEC2OCT
  • DEGREES
  • DELTA
  • DEVSQ
  • DGET
  • DISC
  • DMAX
  • DMIN
  • DOLLAR
  • DOLLARDE
  • DOLLARFR
  • DPRODUCT
  • DSTDEV
  • DSTDEVP
  • DSUM
  • DURATION
  • DVAR
  • DVARP
  • EDATE
  • EFFECT
  • EOMONTH
  • ERF
  • ERFC
  • ERROR.TYPE
  • EVEN
  • EXACT
  • EXP
  • EXPONDIST
  • FACT
  • FACTDOUBLE
  • FALSE
  • FDIST
  • FIND
  • FINDB
  • FINV
  • FISHER
  • FISHERINV
  • FIXED
  • FLOOR
  • FORECAST
  • FREQUENCY
  • FTEST
  • FV
  • FVSCHEDULE
  • GAMMADIST
  • GAMMAINV
  • GAMMALN
  • GCD
  • GEOMEAN
  • GESTEP
  • GETPIVOTDATA
  • GROWTH
  • HARMEAN
  • HEX2BIN
  • HEX2DEC
  • HEX2OCT
  • HLOOKUP
  • HOUR
  • HYPERLINK
  • HYPGEOMDIST
  • IF
  • IFERROR
  • IMABS
  • IMAGINARY
  • IMARGUMENT
  • IMCONJUGATE
  • IMCOS
  • IMDIV
  • IMEXP
  • IMLN
  • IMLOG10
  • IMLOG2
  • IMPOWER
  • IMPRODUCT
  • IMREAL
  • IMSIN
  • IMSQRT
  • IMSUB
  • IMSUM
  • INDEX
  • INDIRECT
  • INFO
  • INT
  • INTERCEPT
  • INTRATE
  • IPMT
  • IRR
  • ISBLANK
  • ISERR
  • ISERROR
  • ISEVEN
  • ISLOGICAL
  • ISNA
  • ISNONTEXT
  • ISNUMBER
  • ISODD
  • ISPMT
  • ISREF
  • ISTEXT
  • JIS
  • KURT
  • LARGE
  • LCM
  • LEFT
  • LEFTB
  • LEN
  • LENB
  • LINEST
  • LN
  • LOG
  • LOG10
  • LOGEST
  • LOGINV
  • LOGNORMDIST
  • LOOKUP
  • LOWER
  • MATCH
  • MAX
  • MAXA
  • MDETERM
  • MDURATION
  • MEDIAN
  • MID
  • MIDB
  • MIN
  • MINA
  • MINUTE
  • MINVERSE
  • MIRR
  • MMULT
  • MOD
  • MODE
  • MONTH
  • MROUND
  • MULTINOMIAL
  • N
  • NA
  • NEGBINOMDIST
  • NETWORKDAYS
  • NOMINAL
  • NORMDIST
  • NORMINV
  • NORMSDIST
  • NORMSINV
  • NOT
  • NOW
  • NPER
  • NPV
  • OCT2BIN
  • OCT2DEC
  • OCT2HEX
  • ODD
  • ODDFPRICE
  • ODDFYIELD
  • ODDLPRICE
  • ODDLYIELD
  • OFFSET
  • OR
  • PEARSON
  • PERCENTILE
  • PERCENTRANK
  • PERMUT
  • PHONETIC
  • PI
  • PMT
  • POISSON
  • POWER
  • PPMT
  • PRICE
  • ORICEDISC
  • PRICEMAT
  • PROB
  • PRODUCT
  • PROPER
  • PV
  • QUARTILE
  • QUOTIENT
  • RADIANS
  • RAND
  • RANDBETWEEN
  • RANK
  • RATE
  • RECEIVED
  • REPLACE
  • REPLACEB
  • REPT
  • RIGHT
  • RIGHTB
  • ROMAN
  • ROUND
  • ROUNDDOWN
  • ROUNDUP
  • ROW
  • ROWS
  • RSQ
  • RTD
  • SEARCH
  • SEARCHB
  • SECOND
  • SERIESSUM
  • SIGN
  • SIN
  • SINH
  • SKEW
  • SLN
  • SLOPE
  • SMALL
  • SQRT
  • SQRTPI
  • STANDARDIZE
  • STDEV
  • STDEVA
  • STDEVP
  • STDEVPA
  • STEYX
  • SUBSTITUTE
  • SUBTOTAL
  • SUM
  • SUMIF
  • SUMIFS
  • SUMPRODUCT
  • SUMSQ
  • SUMX2MY2
  • SUMX2PY2
  • SUMXMY2
  • SYD
  • T
  • TAN
  • TANH
  • TBILLEQ
  • TBILLPRICE
  • TBILLYIELD
  • TDIST
  • TEXT
  • TIME
  • TIMEVALUE
  • TINV
  • TODAY
  • TRANSPOSE
  • TREND
  • TRIM
  • TRIMMEAN
  • TRUE
  • TRUNC
  • TTEST
  • TYPE
  • UPPER
  • USDOLLAR
  • VALUE
  • VAR
  • VARA
  • VARP
  • VARPA
  • VDB
  • VERSION
  • VLOOKUP
  • WEEKDAY
  • WEEKNUM
  • WEIBULL
  • WORKDAY
  • XIRR
  • XNPV
  • YEAR
  • YEARFRAC
  • YIELD
  • YIELDDISC
  • YIELDMAT
  • ZTEST

If you are interested in the intersection of PHP and Math I encourage you to test drive the PHPExcel Project. The code is licensed under the GPL v2.

Permalink 

Estimating Water Flow: Recreational Exercises [Math Projects
Posted on July 12, 2007 @ 01:28:03 PM by Paul Meagher

  1. If you do not make your depth measurements from the waterline, but say 2 feet above the waterline, how would you accomodate this difference?
  2. Make a plot with "Water Line Distance (in)" on your x axis and "Water Depth (in)" measurements on your y axis. Might be interesting to syncronize the orientation of the plot to the orientation of your measurement (e.g., make your depth measurements a negative value so they plot downwards).
  3. Develop a water flow estimation calculator.
  4. How might we compute an error margin for our estimate?
  5. Consider whether the manner in which water flows is an embedded metaphor used to structure our thinking in other domains. For example, water flows and investment flows (a.k.a., revenue streams) might have similar volumetric influences and parametric controls.

Permalink 

Estimating Water Flow: Discrete Integration [Math Projects
Posted on July 11, 2007 @ 10:58:13 PM by Paul Meagher

The traditional Trapezoid Rule that computes the area under a specified function between the limits $a and $b:

<?php
/**
* @author Paul Meagher
* @modified July 11/2007
*
* Port of TrapezoidRule.java
*
* @see http://www.cs.princeton.edu/introcs/93integration/TrapezoidRule.java.html
*/

/*************************************************************************
 *  Compilation:  javac TrapezoidRule.java
 *  Execution:    java TrapezoidRule a b
 *  
 *  Numerically integrate the function in the interval [a, b].
 *
 *  % java TrapezoidRule -3 3
 *  0.9973002031388447                 // true answer = 0.9973002040...
 *
 *  Observation: this says that 99.7% of time a standard normal random
 *  variable is within 3 standard deviation of its mean.
 *
 *  %  java TrapezoidRule 0 100000
 *  1.9949108930964732                 // true answer = 1/2
 *
 *  Caveat: this is not the best way to integrate the normal density
 *  function. See what happens if you make b very big.
 *
 *************************************************************************/

class TrapezoidRule {

   
/**********************************************************************
   * Standard normal distribution density function.
   * Replace with any sufficiently smooth function.
   *
   * @param double $x
   * @return double 
   **********************************************************************/
   
public function f($x) {
      return 
exp(-$x $x 2)  / sqrt(M_PI);
   }

   
/**********************************************************************
   * Integrate f from a to b using trapezoid rule.
   * Increase N for more precision.
   *
   * @param double $a
   * @param double $b
   * @param int $N
   * @return double
   **********************************************************************/
   
public function integrate($a$b$N) {
      
$h = ($b $a) / $N;                         // step size
      
$sum 0.5 * ($this->f($a) + $this->f($b));  // area
      
for ($i 1$i $N$i++) {
         
$x $a $h $i;
         
$sum $sum $this->f($x);
      }
      return 
$sum $h;
   }   

}

$a = -3;
$b 3;
$N 1000;

$trap = new TrapezoidRule;

echo 
$trap->integrate($a$b$N);         

?>

Below is a version of the Trapezoid Rule that can be used to integrate tabulated data. We will apply the DiscreteTrapezoidRule to x and y data collected from our stream depth measurement. The x values correspond to each mark on the water-spanning line and run from 0 to 13 inches with depth reading marks at 1 inch intervals. The corresponding y values are the vertical depth measurements at each line mark.

<?php
/**
* Implements Discrete Trapezoid Rule for use with tabulated data.
*
* Works with regular or irregular spacing between x values.
*
* @see http://www.cs.princeton.edu/introcs/93integration/TrapezoidRule.java.html
*
* @author Paul Meagher
* @modified July 12/2007
*/
class DiscreteTrapezoidRule {
     
  
/**
  * @var boolean $debug
  */ 
  
public $debug false;

   
/**********************************************************************
   * Integrate tabulated x and y values using trapezoid rule.
   * 
   * @param array[] $X vector of x measurements.
   * @param array[] $Y vector of corresponding y measurements.
   * @return double sum of trapezoids defined by pair of tabulated values.
   **********************************************************************/
   
public function integrate($X$Y) {
     
     
$n   count($X);       
     
     
// Initialize sum to 0.0 float value.
     
$sum 0.0;    
     
     
// Iterate to less than $n-1 because routine uses $i+1 
     // element in an array for computing average heights.
     
for ($i 0$i $n-1$i++) {
     
       
// Width of trapezoid. Can be irregular widths.
       
$width  abs($X[$i] - $X[$i+1]);
     
       
// Average height of trapezoid.
       
$height 0.5 * ($Y[$i] + $Y[$i+1]);
     
       
// Trapezoid area = width times average height.
       
$area   $width $height;

       
// View left hand side.
       
if ($this->debug) echo "iter ".($i+1).": $sum + ($width * $height) = ";
     
       
// Sum the areas.
       
$sum    += $area
       
       
// View right hand side.
       
if ($this->debug) echo "$sum <br />";       
     
      }
     
      return 
$sum;    

   }   

}


$X = array(012345678910111213);
$Y = array(0491188121315121011102);

$trap = new DiscreteTrapezoidRule;

// Set debug to true to see intermediate computations.
$trap->debug true;

// Integrate tabulated values to compute cross section in square inches.
$cross_section_sq_in $trap->integrate($X$Y);   
    
// Use foot unit instead of inches unit.    
$cross_section_sq_ft $cross_section_sq_in 12;

// How far a marker travels in one minute.
$flow_rate_ft_min 88;

// Correction factor.
$flow_rate_correction 0.83;

// Compute cubic flow.
$flow_rate = ($cross_section_sq_ft $flow_rate_ft_min) * $flow_rate_correction;

// Display results.      
?>  
           
<p>
The water flow estimate is <?php echo round($flow_rate?> cubic feet per minute.
</p>

The output of this script looks like this:

iter 1: 0 + (1 * 2) = 2
iter 2: 2 + (1 * 6.5) = 8.5
iter 3: 8.5 + (1 * 10) = 18.5
iter 4: 18.5 + (1 * 9.5) = 28
iter 5: 28 + (1 * 8) = 36
iter 6: 36 + (1 * 10) = 46
iter 7: 46 + (1 * 12.5) = 58.5
iter 8: 58.5 + (1 * 14) = 72.5
iter 9: 72.5 + (1 * 13.5) = 86
iter 10: 86 + (1 * 11) = 97
iter 11: 97 + (1 * 10.5) = 107.5
iter 12: 107.5 + (1 * 10.5) = 118
iter 13: 118 + (1 * 6) = 124

The water flow estimate is 755 cubic feet per minute. 

At this stage you might try imagining a container large enough to house 775 cubic feet of volume. A 10 ft by 10 ft by 10 ft box-shaped container has a capacity of 1000 cubic feet. The stream you are measuring would fill such a container to the 3/4 mark in one minute.

Permalink 

Estimating Water Flow: Introduction [Math Projects
Posted on July 11, 2007 @ 04:17:49 AM by Paul Meagher

A nice project for applying integral calculus and some statistical thinking is estimating water flow.

One way to estimate water flow is to extend a physical line from bank to bank. This line should be placed level with the waterline.

Once you have setup the line, mark intervals along the line where you will be measuring the depth of the water flow. The marks along the line are your x-axis values, and the depth measurements are your y-axis values.

To compute a cross sectional area, multiply the horizontal width of a given measurement interval by an average of the vertical measurements for that interval. Sum these sectional areas to compute an estimate of the total cross sectional area of the waterflow under the waterline.

To estimate water flow you also need to take into account the average speed of the current. To estimate this speed you can throw a floating marker (e.g., plastic bottle) into the current and measure how far it goes in one minute. Better yet, do this a few times and average your measurements. If you measured your cross sectional area in square feet and the current in feet per minute, you can multiply these values together to get a water flow estimate in terms of cubic feet per minute. You can also express your results as cubic meters per minute.

You need to apply a final correction to your estimate to take into account the fact that water flows faster at the surface than below. A multiplier value of 0.83 or 5/6 is recommended by Darrell Huff 1 who discussed the water flow measurement procedure being advocated here.

1 Darrell Huff (1996) The Complete How to Figure it. W.W Norton & Company, New York. p. 150

Permalink 

 Archive 
 


php/Math Project
© 2011. All rights reserved.