# forum.alglib.net

ALGLIB forum
 It is currently Sun Jul 21, 2024 10:43 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 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Least squares fitting for non-linear curve in C++Posted: Fri Sep 15, 2017 4:24 pm

Joined: Fri Sep 15, 2017 3:36 pm
Posts: 2
I want to perform a least square fitting on my data set using this code :

Code:

void function(const alglib::real_1d_array &coeff, const alglib::real_1d_array &x, double &func, void *ptr)
{
func =  coeff[0] / pow( 1 + coeff[1] * coeff[2] * x[0] , 1 / coeff[3] );
}

void main()
{

int N = 10;
int C = 4;
double epsx = 0.000001;
double diffstep = 0.0001;
alglib::lsfitstate state;
alglib::lsfitreport report;
alglib::ae_int_t maxits = 0;
alglib::ae_int_t info;

alglib::real_2d_array xAxis;
xAxis.setlength(N, 1);
alglib::real_1d_array yAxis;
yAxis.setlength(N);
alglib::real_1d_array cArray;
cArray.setlength(C);

/* Filling arrays  .... */

std::cout << " x " << xAxis.tostring(2) << std::endl; //printing xarray
std::cout << " y " << yAxis.tostring(2) << std::endl; // printing yarray
std::cout << " c " << cArray.tostring(10) << std::endl; //printing yarray

alglib::lsfitcreatef(xAxis,yAxis, cArray, diffstep, state);
alglib::lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function);
lsfitresults(state, info, cArray, report);
std::cout << " Best fit coefficients " << cArray.tostring(12) << std::endl;
std::cout << " Informations  " << int(info) << std::endl;

}

Here is what i get in my console :

x [[0.00],[1.00],[2.00],[3.00],[4.00],[5.00],[6.00],[7.00],[8.00],[9.00]]
y [0.00,184.00,184.00,184.00,184.00,184.00,554.00,356.00,340.00,340.00]
c [554.0000000000,1.0000000000,0.0060000000,554.0000000000]
Best fit coefficients []
Informations about algorithm -8

I dont know what is the signification of the error code -8.
I dont get any coeffcients as a result. But i think my mistake comes from the data x, y i am using because when i use the dataset(only x and y array, i still keep my function that i want to fit my data and the coeff array) provided in alglib documentation, i get this results :

x [[-1.00],[-0.80],[-0.60],[-0.40],[-0.20],[0.00],[0.20],[0.40],[0.60],[0.80],[1.00]]
y [0.22,0.38,0.58,0.79,0.94,1.00,0.94,0.79,0.58,0.38,0.22]
c [554.0000000000,1.0000000000,0.0060000000,554.0000000000]
Best fit coefficients [0.621302545335,0.999951946642,0.000084880705,553.999951049188]
Informations 2

I obtain coefficients . So, my first guess is that my mistake comes from the x and y data i am using but i dont know how to explain it or maybe i am missing smth. I would appreciate some help. I apoligize for my poor english and if you need further informations about my code, just let me know.

Julien.

Top

 Post subject: Re: Least squares fitting for non-linear curve in C++Posted: Mon Sep 18, 2017 11:44 am

Joined: Fri May 07, 2010 7:06 am
Posts: 921
Hi!

1. -8 error code means that NAN/INF values were detected in the target function. Say, you tried to divide by zero - and returned INF to optimizer. Or tried to calculate sqrt(-1) - and returned NAN.

Your target function, coeff[0] / pow( 1 + coeff[1] * coeff[2] * x[0] , 1 / coeff[3] ), can fail in such way if 1 + coeff[1] * coeff[2] * x[0] become negative or zero. You have to put constraints on coefficients in order to prevent it. Constrain coeff[1] and coeff[2] to be non-negative, it will work. It can be done with lsfitsetbc() function call.

It may also help to constrain coeff[3] in a similar way.

2. The whole composition of your target function is very unstable. You can easily get very small or very large values by playing with coeff[3]. And there exists a redundancy between coeff[1] and coeff[2] - they enter expression only as product, which makes solution non-unique (you can multiply one of them by 2, divide another one by 2, and get same function value).

So, I recommend you to find good initial values for the algorithm. In such cases you can not rely on convergence from any initial point, you have to provide good one in order to converge to good solution.

Top

 Post subject: Re: Least squares fitting for non-linear curve in C++Posted: Wed Sep 20, 2017 1:27 pm

Joined: Fri Sep 15, 2017 3:36 pm
Posts: 2
Hi Sergey,

I want to thank you for your detailled response. I will try what you just told me and am pretty sure it will works !
Again, thank you very much, you made my day!

Julien

Top

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

 All times are UTC

#### Who is online

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

 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:
 Jump to:  Select a forum ------------------ ALGLIB forum    ALGLIB-discuss
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group