package com.nus.sincos;

/**
 * Created by duy on 3/2/15.
 */

import com.nus.LmSumSquaresError;
import com.nus.LmSolver;

/**
 * Task: Suppose the true model is f(x) = 0.5 * cos(2 * x) + 2 * sin(0.5 * x)
 *
 * Given the set of points generated by
 *                f(x) = 0.5 * cos(2 * x) + 2 * sin(0.5 * x)
 * with some noise, use Levenberg-Marquardt algorithm to find the model to fit
 * all the points.
 */
public class Main {
  public static void main(String[] args) {
    double[][] xy = {
      {1.0, 0.76}, {1.5, 0.86}, {-1.0, -1.18}, {2.0, 1.356}, {1.8, 1.118},
      {2.5, 2.039}, {-0.5, -0.224}, {-0.8, -0.7934}, {-1.1, -1.339},
      {2.2, 1.63}, {2.6, 2.1613}, {2.8, 2.35}, {-2.0, -2.009}, {-2.2, -1.936},
      {-1.7, -1.985}, {-1.4, -1.759}, {0.05, 0.55}, {0.0, 0.5},
      {1.5707963, 0.914}, {-1.5707963, -1.9142}, {0.6, 0.77}, {-0.6, -0.4},
      {1.67, 1.0}, {2.4, 1.9}, {0.1, 0.59}
    };

    int numData = xy.length;
    double x[] = new double[numData];
    double y[] = new double[numData];
    for (int i = 0; i < numData; ++i) {
      x[i] = xy[i][0];
      y[i] = xy[i][1];
    }

    ModelSinCosFunc model = new ModelSinCosFunc(x, y);
    LmSumSquaresError errorFunc = new LmSumSquaresError(model);

    LmSolver lmSolver = new LmSolver(0.00001, 300, 0.00000001, errorFunc);

    double originalAB[] = {1.7, 3};
    double ab[] = {1.7, 3};
    lmSolver.solve(ab);

    System.out.printf("Result with initial guess (a, b) = (%f, %f):\n",
      originalAB[0], originalAB[1]);
    System.out.println(ab[0]);
    System.out.println(ab[1]);

    originalAB[0] = 0.25;
    originalAB[1] = 1.5;
    ab[0] = 0.25;
    ab[1] = 1.5;
    lmSolver.solve(ab);
    System.out.printf("Result with initial guess (a, b) = (%f, %f):\n",
      originalAB[0], originalAB[1]);
    System.out.println(ab[0]);
    System.out.println(ab[1]);
  }
}

@@ -1312,31 +1312,31 @@
 ble 
-originalAB
+all_abs%5B%5D
 %5B%5D = 
+%7B
 %7B1.7, 3%7D
 ;%0A  
@@ -1335,345 +1335,130 @@
 , 3%7D
-;%0A    double ab%5B%5D = %7B1.7, 3%7D;%0A    lmSolver.solve(ab);%0A%0A    System.out.printf(%22Result with initial guess (a, b) = (%25f, %25f):%5Cn%22,%0A      originalAB%5B0%5D, originalAB%5B1%5D);%0A    System.out.println(ab%5B0%5D);%0A    System.out.println(ab%5B1%5D);%0A%0A    originalAB%5B0%5D = 0.25;%0A    originalAB%5B1%5D = 1.5;%0A    ab%5B0%5D = 0.25;%0A    ab%5B1%5D = 1.5;%0A    lmSolver.solve(ab);%0A
+, %7B0.25, 1.5%7D, %7B0.0, 3.0%7D, %7B10.0, 5.0%7D%7D;%0A    for (int i = 0; i %3C all_abs.length; ++i) %7B%0A      double%5B%5D ab = all_abs%5Bi%5D;%0A  
     
@@ -1535,39 +1535,53 @@
     
-originalAB%5B0%5D, originalAB%5B1%5D);%0A
+  ab%5B0%5D, ab%5B1%5D);%0A      lmSolver.solve(ab);%0A  
     
@@ -1603,24 +1603,26 @@
 tln(ab%5B0%5D);%0A
+  
     System.o
@@ -1640,14 +1640,20 @@
 ab%5B1%5D);%0A
+    %7D%0A
   %7D%0A%7D%0A

Update sincos example. Allow testing with more guesses