Back to index

wims  3.65+svn20090927
Secant.java
Go to the documentation of this file.
00001 /*
00002  * Created on 05.11.2005
00003  *
00004  */
00005 package rene.zirkel.expression;
00006 
00007 import rene.zirkel.construction.ConstructionException;
00008 import rene.zirkel.objects.FunctionObject;
00009 
00010 public class Secant
00011 {      public static double compute (FunctionObject F,
00012               double a, double b, double eps)
00013        throws ConstructionException
00014        {      double ay=F.evaluateF(a),by=F.evaluateF(b);
00015               double c=(a+b)/2;
00016               if (ay*by>eps)
00017                      throw new ConstructionException("");
00018               while (Math.abs(b-a)>eps)
00019               {      double cy=F.evaluateF(c);
00020                      if (Math.abs(cy)<eps) return c;
00021                      if (cy*ay>0)
00022                      {      if (Math.abs(ay-cy)<eps)
00023                             {      a=c; ay=cy; c=(a+b)/2;
00024                             }
00025                             else
00026                             {      double d=a-ay*(a-c)/(ay-cy);
00027                                    if (d>b || d<a)
00028                                    {      a=c; ay=cy; c=(a+b)/2;
00029                                    }
00030                                    else
00031                                    {      a=c; ay=cy; c=d;
00032                                    }
00033                             }
00034                      }
00035                      else
00036                      {      double d=a-ay*(a-c)/(ay-cy);
00037                             b=c; by=cy; c=d;
00038                      }
00039               }
00040               return c;
00041        }
00042 }
00043