SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculus: Integration possible with Javascript?

    I'm writing a script for a mathematics statistics class that will show students how the normal distribution works (using z tables). In order to get the script to work I either need to code the z tables manually (big pain) or find some javascript tool that will integrate a function (by integration, I mean the integration one does in calculus).

    Does anyone know if Javascript can do integration? If so could you point me in the right direction.

    Thanks!
    Stephanie

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at

    http://math2.org/math/stat/distributions/z-dist.htm

    They are doing some kind of calculations on that page

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...this was just what I needed. The code on that page was a little wonky so I refactored it a bit and here's what I came up with:

    Code:
    function GetZPercent(z) 
      {
        //z == number of standard deviations from the mean
        
        //if z is greater than 6.5 standard deviations from the mean
        //the number of significant digits will be outside of a reasonable 
        //range
        if ( z < -6.5)
          return 0;
        if( z > 6.5) 
          return 1;
    
        var factK = 1;
        var sum = 0;
        var term = 1;
        var k = 0;
        var loopStop = Math.exp(-23);
        while(Math.abs(term) > loopStop) 
        {
          term = .3989422804 * Math.pow(-1,k) * Math.pow(z,k) / (2 * k + 1) / Math.pow(2,k) * Math.pow(z,k+1) / factK;
          sum += term;
          k++;
          factK *= k;
    
        }
        sum += 0.5;
    
        return sum * 100;
      }

  4. #4


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •