Ejemplo n.º 1
0
  public TaylorApproximation(ParseTree pt) {
    super(pt);

    SymbolTable st = new SymbolTable();
    ValueTable vt = new ValueTable();

    try {
      f = FunctionFactory.getFunction(getArg(0));
      x0 = FunctionFactory.getFunction(getArg(1)).evaluate(st, vt);
      order = (int) FunctionFactory.getFunction(getArg(2)).evaluate();
    } catch (ArrayIndexOutOfBoundsException e) {
      e.printStackTrace(System.out);
      return;
    }

    Function D = f;
    taylorCoeff = new double[order + 1];

    // TODO this is a problem!  Make the taylor approximation not always over x.
    vt.setValue(VariableToken.X_VAR, x0);

    for (int i = 0; i <= order; i++) {
      taylorCoeff[i] = D.evaluate(st, vt);

      for (int j = 2; j <= i; j++) taylorCoeff[i] = taylorCoeff[i] / j;

      D = D.derivative(VariableToken.X_VAR);
    }
  }
Ejemplo n.º 2
0
 public double evaluate(SymbolTable st, ValueTable vt) {
   // So called Horner's Rule which evaluates a polynomial of degree n
   // in n multiplies and n adds.
   double x = vt.getValue(VariableToken.X_VAR);
   double val = 0;
   for (int i = taylorCoeff.length - 1; i >= 0; i--) {
     val = val * (x - x0) + taylorCoeff[i];
   }
   return val;
 }