forum.alglib.net http://forum.alglib.net/ |
|
Function fitting problem http://forum.alglib.net/viewtopic.php?f=2&t=612 |
Page 1 of 1 |
Author: | twistfire [ Fri Sep 07, 2012 10:52 am ] |
Post subject: | Function fitting problem |
Hi there! trying to use ALGLIB to fit experimental data. I have an array of 101 elemens (e.g.) they are in range of -10..1000 Trying to use gaussian to fit experimantal data: y(x)=c0*exp[-(x-c1)^2/(2*c2^2)] c - regression coefficients.. we used this example: http://www.alglib.net/translator/man/ma ... sfit_d_nlf But every time we get in info code 4. According to (http://www.alglib.net/translator/man/ma ... fitresults) this means : gradient norm is no more than EpsG.. What is that mean? and how can we get estimates of the coefficients (they don't change from initial values)? Maybe we do something wrong? Here is the code in c# (running on windows 7 x64, .NET 4.0) Code: //main code
private void DrawApproxGraph() { try { List<double> zond = GetZond(Convert.ToInt32(Left_textbox.Value), Convert.ToInt32(Right_textbox.Value)); double[,] x = CreateXArr(Convert.ToInt32(Left_textbox.Value), Convert.ToInt32(Right_textbox.Value)); double[] y = zond.ToArray(); double[] c = new double[] { 1000, 20, 10}; double epsf = 0; double epsx = 0.0001; int maxits = 0; int info; alglib.lsfitstate state; alglib.lsfitreport rep; double diffstep = 0.000000001; // // Fitting without weights // alglib.lsfitcreatef(x, y, c, diffstep, out state); alglib.lsfitsetcond(state, epsf, epsx, maxits); alglib.lsfitfit(state, function_cx_1_func, null, null); alglib.lsfitresults(state, out info, out c, out rep); System.Console.WriteLine("{0}", info); // EXPECTED: 2 System.Console.WriteLine("{0}", alglib.ap.format(c, 1)); } catch (Exception ex) { } } //end of main public static void function_cx_1_func(double[] c, double[] x, ref double func, object obj) { func = c[0] * System.Math.Exp(Math.Pow(x[0] - c[1], 2) / (-2 * Math.Pow(c[2], 2))); } |
Author: | twistfire [ Fri Sep 07, 2012 11:39 am ] |
Post subject: | Re: Function fitting problem |
PS data for testing data --- x {double[101, 1]} double[,] [0, 0] 227.0 double [1, 0] 228.0 double [2, 0] 229.0 double [3, 0] 230.0 double [4, 0] 231.0 double [5, 0] 232.0 double [6, 0] 233.0 double [7, 0] 234.0 double [8, 0] 235.0 double [9, 0] 236.0 double [10, 0] 237.0 double [11, 0] 238.0 double [12, 0] 239.0 double [13, 0] 240.0 double [14, 0] 241.0 double [15, 0] 242.0 double [16, 0] 243.0 double [17, 0] 244.0 double [18, 0] 245.0 double [19, 0] 246.0 double [20, 0] 247.0 double [21, 0] 248.0 double [22, 0] 249.0 double [23, 0] 250.0 double [24, 0] 251.0 double [25, 0] 252.0 double [26, 0] 253.0 double [27, 0] 254.0 double [28, 0] 255.0 double [29, 0] 256.0 double [30, 0] 257.0 double [31, 0] 258.0 double [32, 0] 259.0 double [33, 0] 260.0 double [34, 0] 261.0 double [35, 0] 262.0 double [36, 0] 263.0 double [37, 0] 264.0 double [38, 0] 265.0 double [39, 0] 266.0 double [40, 0] 267.0 double [41, 0] 268.0 double [42, 0] 269.0 double [43, 0] 270.0 double [44, 0] 271.0 double [45, 0] 272.0 double [46, 0] 273.0 double [47, 0] 274.0 double [48, 0] 275.0 double [49, 0] 276.0 double [50, 0] 277.0 double [51, 0] 278.0 double [52, 0] 279.0 double [53, 0] 280.0 double [54, 0] 281.0 double [55, 0] 282.0 double [56, 0] 283.0 double [57, 0] 284.0 double [58, 0] 285.0 double [59, 0] 286.0 double [60, 0] 287.0 double [61, 0] 288.0 double [62, 0] 289.0 double [63, 0] 290.0 double [64, 0] 291.0 double [65, 0] 292.0 double [66, 0] 293.0 double [67, 0] 294.0 double [68, 0] 295.0 double [69, 0] 296.0 double [70, 0] 297.0 double [71, 0] 298.0 double [72, 0] 299.0 double [73, 0] 300.0 double [74, 0] 301.0 double [75, 0] 302.0 double [76, 0] 303.0 double [77, 0] 304.0 double [78, 0] 305.0 double [79, 0] 306.0 double [80, 0] 307.0 double [81, 0] 308.0 double [82, 0] 309.0 double [83, 0] 310.0 double [84, 0] 311.0 double [85, 0] 312.0 double [86, 0] 313.0 double [87, 0] 314.0 double [88, 0] 315.0 double [89, 0] 316.0 double [90, 0] 317.0 double [91, 0] 318.0 double [92, 0] 319.0 double [93, 0] 320.0 double [94, 0] 321.0 double [95, 0] 322.0 double [96, 0] 323.0 double [97, 0] 324.0 double [98, 0] 325.0 double [99, 0] 326.0 double [100, 0] 327.0 double --- y {double[101]} double[] [0] -0.17 double [1] 0.0 double [2] -0.17 double [3] -0.33 double [4] -0.17 double [5] -0.17 double [6] -0.17 double [7] -0.33 double [8] -0.17 double [9] -0.33 double [10] -0.33 double [11] -0.33 double [12] -0.17 double [13] -0.17 double [14] -0.17 double [15] 0.0 double [16] 0.0 double [17] -0.17 double [18] -0.17 double [19] -0.17 double [20] -0.17 double [21] -0.17 double [22] 0.16 double [23] 0.48 double [24] 1.29 double [25] 2.43 double [26] 3.4 double [27] 3.72 double [28] 3.72 double [29] 3.24 double [30] 2.26 double [31] 1.29 double [32] 3.72 double [33] 14.25 double [34] 44.06 double [35] 101.41 double [36] 190.5 double [37] 310.38 double [38] 451.8 double [39] 600.52 double [40] 737.89 double [41] 850.8 double [42] 932.29 double [43] 982.5 double [44] 1000.0 double [45] 983.15 double [46] 929.69 double [47] 847.4 double [48] 743.72 double [49] 627.08 double [50] 510.93 double [51] 409.04 double [52] 321.88 double [53] 248.66 double [54] 190.18 double [55] 147.25 double [56] 118.58 double [57] 100.92 double [58] 88.93 double [59] 78.4 double [60] 67.55 double [61] 57.02 double [62] 47.14 double [63] 39.04 double [64] 32.88 double [65] 27.37 double [66] 20.89 double [67] 14.09 double [68] 8.1 double [69] 3.72 double [70] 1.45 double [71] 0.64 double [72] 0.64 double [73] 0.81 double [74] 0.97 double [75] 0.97 double [76] 1.78 double [77] 3.4 double [78] 5.83 double [79] 8.26 double [80] 9.39 double [81] 9.23 double [82] 8.1 double [83] 6.15 double [84] 4.69 double [85] 4.53 double [86] 5.02 double [87] 5.83 double [88] 6.8 double [89] 6.96 double [90] 7.12 double [91] 7.29 double [92] 7.61 double [93] 8.1 double [94] 8.74 double [95] 8.42 double [96] 7.45 double [97] 5.99 double [98] 4.05 double [99] 2.59 double [100] 1.13 double --- |
Author: | Sergey.Bochkanov [ Fri Sep 07, 2012 12:43 pm ] |
Post subject: | Re: Function fitting problem |
I think the problem is that you've passed bad initial values. Your data clearly have peak somewhere at x=271, but you initial estimate is a Gaussian centered at x=20 with width=10. It is effectively zero at x=271, so gradient with respect to all of its parameters is zero. Try providing better estimates - choose location of maximum x as initial location of your Gaussian, y-value at this x as coefficient before Gaussian, and choose some reasonable width (it is better to choosing it from data, not to use some fixed value). Does it solve your problem? |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |