Back to index

wims  3.65+svn20090927
ConvexMin.java
Go to the documentation of this file.
00001 /*
00002  * Created on 13.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 ConvexMin
00011 {      
00012        public static double computeMin (
00013                      FunctionObject F, double a, double b,
00014                      double eps)
00015               throws ConstructionException
00016        {      double lambda=(Math.sqrt(5)-1)/2;
00017               double x2=lambda*a+(1-lambda)*b;
00018               double y2=F.evaluateF(x2);
00019               double x3=(1-lambda)*a+lambda*b;
00020               double y3=F.evaluateF(x3);
00021               while (b-a>eps)
00022               {      if (y2<y3)
00023                      {      b=x3; x3=x2; y3=y2;
00024                             x2=lambda*a+(1-lambda)*b; y2=F.evaluateF(x2);
00025                      }
00026                      else
00027                      {      a=x2; x2=x3; y2=y3;
00028                             x3=(1-lambda)*a+lambda*b; y3=F.evaluateF(x3);
00029                      }
00030               }
00031               return (a+b)/2;
00032        }
00033               
00034        public static double computeMax (
00035                      FunctionObject F, double a, double b,
00036                      double eps)
00037               throws ConstructionException
00038        {      double lambda=(Math.sqrt(5)-1)/2;
00039               double x2=lambda*a+(1-lambda)*b;
00040               double y2=F.evaluateF(x2);
00041               double x3=(1-lambda)*a+lambda*b;
00042               double y3=F.evaluateF(x3);
00043               while (b-a>eps)
00044               {      if (y2>y3)
00045                      {      b=x3; x3=x2; y3=y2;
00046                             x2=lambda*a+(1-lambda)*b; y2=F.evaluateF(x2);
00047                      }
00048                      else
00049                      {      a=x2; x2=x3; y2=y3;
00050                             x3=(1-lambda)*a+lambda*b; y3=F.evaluateF(x3);
00051                      }
00052               }
00053               return (a+b)/2;
00054        }
00055 }
00056 
00057 
00058