楼主: ReneeBK
3130 11

[读者文库]Java Number Cruncher [推广有奖]

11
ReneeBK 发表于 2015-6-6 09:34:05
  1. Listing 5-1a. The class that implements the bisection algorithm.

  2. Code View: Scroll / Show All
  3. package numbercruncher.mathutils;

  4. /**
  5. * The root finder class that implements the bisection algorithm.
  6. */
  7. public class BisectionRootFinder extends RootFinder
  8. {
  9.     private static final int   MAX_ITERS = 50;
  10.     private static final float TOLERANCE = 100*Epsilon.floatValue();

  11.     /** x-negative value */         private float    xNeg;
  12.     /** x-middle value */           private float    xMid = Float.NaN;
  13.     /** x-positive value */         private float    xPos;
  14.     /** previous x-middle value */  private float    prevXMid;
  15.     /** f(xNeg) */                  private float    fNeg;
  16.     /** f(xMid) */                  private float    fMid;
  17.     /** f(xPos) */                  private float    fPos;

  18.     /**
  19.      * Constructor.
  20.      * @param function the functions whose roots to find
  21.      * @param xMin the initial x-value where the function is negative
  22.      * @param xMax the initial x-value where the function is positive
  23.      * @throws RootFinder.InvalidIntervalException
  24.      */
  25.     public BisectionRootFinder(Function function,
  26.                                float xMin, float xMax)
  27.         throws RootFinder.InvalidIntervalException
  28.     {
  29.         super(function, MAX_ITERS);
  30.         checkInterval(xMin, xMax);

  31.         float yMin = function.at(xMin);
  32.         float yMax = function.at(xMax);

  33.         // Initialize xNeg, fNeg, xPos, and fPos.
  34.         if (yMin < 0) {
  35.             xNeg = xMin; xPos = xMax;
  36.             fNeg = yMin; fPos = yMax;
  37.         }
  38.         else {
  39.             xNeg = xMax; xPos = xMin;
  40.             fNeg = yMax; fPos = yMin;
  41.         }
  42.     }

  43.     //–––––––––//
  44.     // Getters //
  45.     //–––––––––//

  46.     /**
  47.      * Return the current value of x-negative.
  48.      * @return the value
  49.      */
  50.     public float getXNeg() { return xNeg; }

  51.     /**
  52.      * Return the current value of x-middle.
  53.      * @return the value
  54.      */
  55.     public float getXMid() { return xMid; }

  56.     /**
  57.      * Return the current value of x-positive.
  58.      * @return the value
  59.      */
  60.     public float getXPos() { return xPos; }

  61.     /**
  62.      * Return the current value of f(x-negative).
  63.      * @return the value
  64.      */
  65.     public float getFNeg() { return fNeg; }

  66.     /**
  67.      * Return the current value of f(x-middle).
  68.      * @return the value
  69.      */
  70.     public float getFMid() { return fMid; }

  71.     /**
  72.      * Return the current value of f(x-positive).
  73.      * @return the value
  74.      */
  75.     public float getFPos() { return fPos; }

  76.     //-----------------------------//
  77.     // RootFinder method overrides //
  78.     //-----------------------------//

  79.     /**
  80.      * Do the bisection iteration procedure.
  81.      * @param n the iteration count
  82.      */
  83.     protected void doIterationProcedure(int n)
  84.     {
  85.         if (n == 1) return;     // already initialized

  86.         if (fMid < 0) {
  87.             xNeg = xMid;        // the root is in the xPos half
  88.             fNeg = fMid;
  89.         }
  90.         else {
  91.             xPos = xMid;        // the root is in the xNeg half
  92.             fPos = fMid;
  93.         }
  94.     }

  95.     /**
  96.      * Compute the next position of xMid.
  97.      */
  98.     protected void computeNextPosition()
  99.     {
  100.         prevXMid = xMid;
  101.         xMid     = (xNeg + xPos)/2;
  102.         fMid     = function.at(xMid);
  103.     }

  104.     /**
  105.      * Check the position of xMid.
  106.      * @throws PositionUnchangedException
  107.      */
  108.     protected void checkPosition()
  109.         throws RootFinder.PositionUnchangedException
  110.     {
  111.         if (xMid == prevXMid) {
  112.             throw new RootFinder.PositionUnchangedException();
  113.         }
  114.     }

  115.     /**
  116.      * Indicate whether or not the algorithm has converged.
  117.      * @return true if converged, else false
  118.      */
  119.     protected boolean hasConverged()
  120.     {
  121.         return Math.abs(fMid) < TOLERANCE;
  122.     }
  123. }


  124.                                           
复制代码

12
ReneeBK 发表于 2015-6-6 09:34:37
  1. Listing 5-1b. The noninteractive version of Program 5–1 applies the bisection algorithm to the function f(x) = x2 − 4 in the initial interval [−0.25, 3.25].

  2. Code View: Scroll / Show All
  3. package numbercruncher.program5_1;

  4. import numbercruncher.mathutils.Function;
  5. import numbercruncher.mathutils.BisectionRootFinder;
  6. import numbercruncher.mathutils.AlignRight;
  7. import numbercruncher.rootutils.RootFunctions;

  8. /**
  9. * PROGRAM 5–1: Bisection Algorithm
  10. *
  11. * Demonstrate the Bisection Algorithm on a function.
  12. */
  13. public class BisectionAlgorithm
  14. {
  15.     /**
  16.      * Main program.
  17.      * @param args the array of runtime arguments
  18.      */
  19.     public static void main(String args[])
  20.     {
  21.         try {
  22.             BisectionRootFinder finder =
  23.                 new BisectionRootFinder(
  24.                         RootFunctions.function("x^2 - 4"),
  25.                                                -0.25f, 3.25f);

  26.             AlignRight ar = new AlignRight();

  27.             ar.print("n", 2);
  28.             ar.print("xNeg", 10); ar.print("f(xNeg)", 14);
  29.             ar.print("xMid", 10); ar.print("f(xMid)", 14);
  30.             ar.print("xPos", 10); ar.print("f(xPos)", 13);
  31.             ar.underline();

  32.             // Loop until convergence or failure.
  33.             boolean converged;
  34.             do {
  35.                 converged = finder.step();

  36.                 ar.print(finder.getIterationCount(), 2);
  37.                 ar.print(finder.getXNeg(), 10);
  38.                 ar.print(finder.getFNeg(), 14);
  39.                 ar.print(finder.getXMid(), 10);
  40.                 ar.print(finder.getFMid(), 14);
  41.                 ar.print(finder.getXPos(), 10);
  42.                 ar.print(finder.getFPos(), 13);
  43.                 ar.println();
  44.             } while (!converged);
  45.             System.out.println("\nSuccess! Root = " +
  46.                                finder.getXMid());
  47.         }
  48.         catch(Exception ex) {
  49.             System.out.println("***** Error: " + ex);
  50.         }
  51.     }
  52. }


  53.                                           
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-1 12:49