forum.alglib.net http://forum.alglib.net/ |
|
Curve Fitting Problem. http://forum.alglib.net/viewtopic.php?f=2&t=494 |
Page 1 of 1 |
Author: | hzxfy [ Sun Nov 27, 2011 2:19 pm ] |
Post subject: | Curve Fitting Problem. |
[data] x: 0 0.475 2.01 5.96 15.0 32.9 y:0.75 3.2 13.8 53.7 170.4 387.8 [curve] f(x) = (a*x + b*x^(2/3) + c*x^(1/3) + d)^3 How to fit the curve? Parameter a=?, b=?, c=? and d=? Thanks! |
Author: | Sergey.Bochkanov [ Mon Nov 28, 2011 7:18 am ] |
Post subject: | Re: Curve Fitting Problem. |
Look at http://www.alglib.net/translator/man/ma ... sfit_d_nlf - it demonstrates fitting using function value only (no analytic derivatives). You can add differentiation later, if you want. |
Author: | hzxfy [ Mon Nov 28, 2011 10:16 am ] |
Post subject: | Re: Curve Fitting Problem. |
My source code below: public static void function_cx_1_func(double[] c, double[] x, ref double func, object obj) { // this callback calculates f(c,x)= (c0*x + c1*x^(2/3) + c2*x^(1/3) + c3)^3 // where x is a position on X-axis and c is adjustable parameter func = System.Math.Pow((c[0] * x[0] + c[1] * Math.Pow(x[0], 2 / 3) + c[2] * Math.Pow(x[0], 1 / 3) + c[3]), 3); } static void Main(string[] args) { // // In this example we demonstrate exponential fitting // by f(x) = (c0*x + c1*x^(2/3) + c2*x^(1/3) + c3)^3 // using function value only. // // Gradient is estimated using combination of numerical differences // and secant updates. diffstep variable stores differentiation step // (we have to tell algorithm what step to use). // double[,] x = new double[,] { { 0 }, { 0.47 }, { 2.01 }, { 5.96 }, { 15 }, { 32.9 }}; double[] y = new double[] { 0.75, 3.2, 13.8, 53.7, 170.4, 387.8 }; double[] c = new double[] { 0.1, 0.1, 0.1, 0.1 }; double epsf = 0; double epsx = 0.0000001; int maxits = 0; int info; alglib.lsfitstate state; alglib.lsfitreport rep; double diffstep = 0.00001; // // 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, 5)); // EXPECTED: [-0.22, 1.4, -0.23, 0.909] // // Fitting with weights // (you can change weights and see how it changes result) // double[] w = new double[] { 1, 1, 1, 1, 1, 1}; alglib.lsfitcreatewf(x, y, w, 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, 5)); // EXPECTED: [-0.22, 1.4, -0.23, 0.909] System.Console.ReadLine(); } I hope the result is: EXPECTED: [-0.22, 1.4, -0.23, 0.909], But the actual output is as follows: 2 {0.13321,0.98960,0.98985,0.99026} 2 {0.13321,0.98960,0.98985,0.99026} Check into the formula found to be wrong. How can i do? Thanks! |
Author: | Sergey.Bochkanov [ Mon Nov 28, 2011 11:13 am ] |
Post subject: | Re: Curve Fitting Problem. |
That is easy :) you have to write 2.0/3.0 and 1.0/3.0 instead of 2/3 and 1/3. Former is 0.666 and 0.333, while latter is 0 and 0 again (integer division instead of real one). |
Author: | hzxfy [ Mon Nov 28, 2011 1:00 pm ] |
Post subject: | Re: Curve Fitting Problem. |
o, Year! Thanks! |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |