# forum.alglib.net

ALGLIB forum
 It is currently Thu Jul 18, 2024 2:44 am

 All times are UTC

### Forum rules

1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Curve Fitting Problem.Posted: Sun Nov 27, 2011 2:19 pm

Joined: Sun Nov 27, 2011 2:09 pm
Posts: 3
[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!

Top

 Post subject: Re: Curve Fitting Problem.Posted: Mon Nov 28, 2011 7:18 am

Joined: Fri May 07, 2010 7:06 am
Posts: 921
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.

Top

 Post subject: Re: Curve Fitting Problem.Posted: Mon Nov 28, 2011 10:16 am

Joined: Sun Nov 27, 2011 2:09 pm
Posts: 3
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]
}

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!

Top

 Post subject: Re: Curve Fitting Problem.Posted: Mon Nov 28, 2011 11:13 am

Joined: Fri May 07, 2010 7:06 am
Posts: 921
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).

Top

 Post subject: Re: Curve Fitting Problem.Posted: Mon Nov 28, 2011 1:00 pm

Joined: Sun Nov 27, 2011 2:09 pm
Posts: 3
o, Year!

Thanks!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for: