Pages

Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

Monday, December 27, 2010

Polinomial Differentiation Using JS

/**
 * This function will do polinomial differentioation.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 28, 2010
 * @version : 1.0
 */




function Polinomial(){
    var x = new Array();
    x[0] = 1;
    var dx = 0.1;
    var y = new Array();
    var fx = 2*x[0];
    y[0] = 1;
    for(i=1;i<12;i++){
        x[i] = x[i-1]+dx;                   //1.1 - 1.2 - 1.3
        y[i]=y[i-1]+dx-fx;
       fx = 2*x[i];
    }
}
                           

Sunday, December 26, 2010

Factorial

/**
 * This function provide factorial service
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 26, 2010
 * @version : 1.0
 *
 * @param n number a factor of the factorial
 * @return sum total summing of the factor
 */

function factorial(n){
    if (n==0) return 1;
    else if (n<0) return 'invalid factor';
    var sum = 1;
    for(i=n;i>0;i--){
        sum = sum * i;
    }
    return sum;
}

Fibonaci Series

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 26, 2010
 * @version : 1.0
 */

/**
 * This function provide fibonaci series
 * @param n number how many term expected
 * @return data an array of the series
 */
function fibo(n){
    var data = new Array();
    data[0]=1;
    data[1]=1;
    for(i=2;i<n;i++){
        data[i] = data[i-1]+data[i-2];
    }
    return data;
}

Saturday, December 25, 2010

Linear Equation Algorithm

/**
 * Class for solving linear equation
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.0
 */


/**
 * Constructor service.
 * The equation supplied must be in the form:
 * ax+bx+c
 * This class is very limited in the for of the simple equation
 *
 * @param fa string the first linear equation
 * @param fb string the second linear equation
 */
function LinearEquation(fa,fb){
    this.fa = fa;
    this.fb = fb;

    var terma = this.fa.split(/[-+]/g);
    var a1 = terma[0].split(/x/)[0];
    var b1 = terma[1].split(/y/)[0];
    var c1 = terma[2];
    a1 = eval(a1);
    b1 = eval(b1);
    c1 = eval(c1);

    var termb = this.fb.split(/[+-]/g);
    var a2 = termb[0].split(/x/)[0];
    var b2 = termb[1].split(/y/)[0];
    var c2 = termb[2];
    a2 = eval(a2);
    b2 = eval(b2);
    c2 = eval(c2);
  
    this.x = ((b2*c1)-(c2*b1)) / ((a2*b1)-(b2*a1));
    this.y = (-c1 - a1*this.x) / b1;
}
/**
 * method to get the x,y value in array
 * @return array x,y value
 */
LinearEquation.prototype.getXY = function(){
    var data = new Array();
    data[0] = this.x;
    data[1] = this.y;
    return data;
};
/**
 * method to get x value
 * @return number x value
 */
LinearEquation.prototype.getX = function(){
    return this.x;
};
/**
 * method to get y value
 * @return number y value
 */
LinearEquation.prototype.getY = function(){
    return this.y;
};
           

Arithmetics Series

/**
 * Class to provide arithmatics services using Javascript
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 2.0 
 */


/**
 * Constructor service
 * @param start the first value of the series
 * @param diff the difference between the values in the series
 * @n how many value in the series
 */
function ArithmeticSeries(start,diff,n){
    this.data = new Array();
    this.start = start;
    this.diff = diff;
    this.n = n;
}


/**
 * method to get the series
 * @return array of the series
 */
ArithmeticSeries.prototype.getSeries = function(){
    for(i=0;i<this.n;i++){
        this.data[i] = this.start + (this.diff*i);
    }
    return this.data;
}


/**
 * method to get the total sum of the individual value of
 * the series.
 * @return sum total summation of the series.
 */
ArithmeticSeries.prototype.getSum = function(){
    var sum = 0;
    for(i=0;i<this.n;i++){
        sum = sum+(this.start+(this.diff*i));
    }
    return sum;
}


/**
 * method to get the last item of the series
 * @return the last item
 */
ArithmeticSeries.prototype.getLast = function(){
    return this.getSeries()[this.getSeries().length-1];
}


/**
 * method to get the first item in the series
 * @return the first item in the series
 */
ArithmeticSeries.prototype.getFirst = function(){
    return this.getSeries()[0];
}


/**
 * method to get the n-th item of the series 
 * @return the n-th item of the series
 */
ArithmeticSeries.prototype.getNth = function(n){
    var bound = this.getSeries().length;

    if(typeof n=='undefined')
        return 'please supply an argument';

    else if(n<=0)
        return 'index must be positive';
   
    else if(n>bound)
        return 'index out of bounds';

    else
        return this.getSeries()[n-1];
}

          

Aritmathics Series

/**
 * Class to provide aritmathics services using Javascript
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.0 
 */


/**
 * Constructor service
 * @param start the first value of the series
 * @param diff the difference between the values in the series
 * @n how many value in the series
 */
function AritmathicSeries(start,diff,n){
    this.data = new Array();
    this.start = start;
    this.diff = diff;
    this.n = n;
}


/**
 * method to get the series
 * @return array of the series
 */
AritmathicSeries.prototype.getSeries = function(){
    for(i=0;i<this.n;i++){
        this.data[i] = this.start + (this.diff*i);
    }
    return this.data;
}


/**
 * method to get the total sum of the individual value of
 * the series.
 * @return sum total summation of the series.
 */
AritmathicSeries.prototype.getSum = function(){
    var sum = 0;
    for(i=0;i<this.n;i++){
        sum = sum+(this.start+(this.diff*i));
    }
    return sum;
}
          

Number Generator

/**
 * This function generate number between seeds.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.0
 */


/**
 * Constructor service
 * @param a the start number desired
 * @param b the end number desired
 */
function Number(a,b){
     this.start = a;
     this.end = b;
     this.data = new Array();
     this.msg = 'Error: 1st argument must less than 2nd';
     this.condition = this.start < this.end;
}

/**
 * method to get the odd numbers.
 * @return array of the odd numbers
 */
Number.prototype.getOdd = function(){
     if(!this.condition) return this.msg;
     var k = 0;
     for(i=this.start;i<=this.end;i++){
         if(i%2!=0){
             this.data[k] = i;
             k++;
         }
     }
     return this.data;
}


/**
 * method to get the even numbers
 * @return array of the even numbers
 */
Number.prototype.getEven = function(){
    if(!this.condition) return this.msg;
    var k = 0;
    for(i=this.start;i<=this.end;i++){
        if(i%2==0){
            this.data[k] = i;
            k++;
        }
    }
    return this.data;
}