Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

[Single variable Talyor Series] Sept 18 2015

Brosnan Yuen edited this page Sep 22, 2015 · 2 revisions

Single variable Taylor series expansion is a straight forward method.

f(a)+\frac {f'(a)}{1!} (x-a)+ \frac{f''(a)}{2!} (x-a)^2+\frac{f'''(a)}{3!}(x-a)^3+ \cdots.

The code:

/*
* Single variable taylor series
* Specification : http://mathworld.wolfram.com/TaylorSeries.html
* @param expression symbols to tranform
* @param varin Input variable name
* @param point Point to evalute taylor series at
* @param iterations Number of terms to generate
*/
var staylor =  function(expression,varin,point,iterations) {
    if (!isSingleVariable(varin))
    {
        throw new Error('Must be single symbol');
    }

    if (iterations.group !== N)
    {
        throw new Error('Must be number');
    }

    if (iterations.multiplier <= 1)
    {
        throw new Error('Must be number > 1');
    }

    var subs = {};
    subs[varin.text()] = point.copy();
    //Generate first term
    var terms = [ _.parse(expression.copy(), subs)];
    //Calculate each term
    var fac = 1;
    for (var n = 1;n < (iterations.valueOf()) ;++n)
    {
        expression = core.Calculus.diff(expression.copy(),varin.copy(),new Symbol('1'));
        var expeval = _.parse(expression.copy(), subs);
        var term = _.multiply( _.divide( expeval ,new Symbol(fac.toString())) , _.pow(  _.subtract( varin.copy(),point.copy() ) , new Symbol(n.toString())  )  );
        terms.push( term );
        fac = fac*(n+1);
    }

    return joinaddsymbols(terms);
};

Starting at 1. It takes a function f and takes the derivative and evaluates it at point a. Then it divides it by a factorial. Then it moves on to the next term and so on...

The applications:

Approximating solution of hard integrals:

Solving ODEs

--Brosnan Yuen