forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 6:16 pm

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.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Curve Fitting Problem.
PostPosted: Sun Nov 27, 2011 2:19 pm 
Offline

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
 Profile  
 
 Post subject: Re: Curve Fitting Problem.
PostPosted: Mon Nov 28, 2011 7:18 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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
 Profile  
 
 Post subject: Re: Curve Fitting Problem.
PostPosted: Mon Nov 28, 2011 10:16 am 
Offline

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]
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!


Top
 Profile  
 
 Post subject: Re: Curve Fitting Problem.
PostPosted: Mon Nov 28, 2011 11:13 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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
 Profile  
 
 Post subject: Re: Curve Fitting Problem.
PostPosted: Mon Nov 28, 2011 1:00 pm 
Offline

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

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 34 guests


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

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group