title: Newton-Raphson method using the Math.Commons library
body: <p>I made a test program to try the <a href="http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolver.html" rel="nofollow">NewtonRaphsonSolver</a> class through the <a href="http://commons.apache.org/proper/commons-math/download_math.cgi" rel="nofollow">Apache Commons Math</a> library. Newton's method is used to find roots for a given function.</p>
  
  <p>The test program that I wrote references the cos(x) function (I have a more difficult function to analyze and am looking at the cos(x) function first).</p>
  
  <p>The code for the test program is </p>
  
  <pre><code>import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
  import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
  import org.apache.commons.math3.analysis.solvers.*;
  import org.apache.commons.math3.exception.DimensionMismatchException;
  
  public class Test3 {
  
      public static void main(String args[]) {
          NewtonRaphsonSolver test = new NewtonRaphsonSolver();
          UnivariateDifferentiableFunction f = new UnivariateDifferentiableFunction() {
  
              public double value(double x) {
                  return Math.cos(x);
              }
  
              @Override
              public DerivativeStructure value(DerivativeStructure t) throws DimensionMismatchException {
                  return t.cos();
              }
          };
  
          for (int i = 1; i &lt;= 500; i++) {
              System.out.println(test.solve(1000, f, i, i+0.1));
          }
      }
  }
  </code></pre>
  
  <p>Not certain if I needed to reference Math.cos(x) and t.cos() twice</p>
  
  <pre><code>public double value(double x) {
                  return Math.cos(x);
              }
  
              @Override
              public DerivativeStructure value(DerivativeStructure t) throws DimensionMismatchException {
                  return t.cos();
              }
  </code></pre>
  
  <p>Newton's method finds all of the zeroes and displays them to the user.</p>
  
  <pre><code>1.5707963267948966
  1.5707963267948966
  -7.853981633974483
  4.71238898038469
  4.71238898038469
  1.5707963267948966
  7.853981633974483
  7.853981633974483
  10.995574287564276
  10.995574287564276
  10.995574287564276
  10.995574287564276
  14.137166941154069
  14.137166941154069
  14.137166941154069
  127.23450247038663
  17.278759594743864
  17.278759594743864
  23.56194490192345
  20.420352248333657
  20.420352248333657
  39.269908169872416
  23.56194490192345
  23.56194490192345
  14.137166941154069
  26.703537555513243
  26.703537555513243
  23.56194490192345
  29.845130209103036
  29.845130209103036
  26.703537555513243
  32.98672286269283
  32.98672286269283
  32.98672286269283
  36.12831551628262
  36.12831551628262
  36.12831551628262
  23.56194490192345
  39.269908169872416
  39.269908169872416
  45.553093477052
  42.411500823462205
  42.411500823462205
  </code></pre>
  
  <p>Is there some way to prevent printing out zeroes that are duplicates? For example, the above output would read</p>
  
  <pre><code>1.5707963267948966
  4.71238898038469
  7.853981633974483
  10.995574287564276
  14.137166941154069
  17.278759594743864
  20.420352248333657
  23.56194490192345
  26.703537555513243
  29.845130209103036
  32.98672286269283
  36.12831551628262
  39.269908169872416
  42.411500823462205
  45.553093477052
  </code></pre>
  
  <p>Can this be done inside a for loop or through an array which only prints out values which are not duplicates?</p>