forum.alglib.net
http://forum.alglib.net/

Unable to find the Coeff. using lsfitfit
http://forum.alglib.net/viewtopic.php?f=2&t=3762
Page 1 of 1

Author:  esfandiar [ Sun Jul 17, 2016 3:49 pm ]
Post subject:  Unable to find the Coeff. using lsfitfit

Hello,

I am new to alglib. I am trying to use the library for non linear curve fitting and I have used R in the past.
I have been trying to use alglib in my program and hope to have the same result as I get from R in non linear curve fitting.
My initial example produced good result between R and alglib but this one here does not seem to work with alglib.

Here is the example
I have the following data

Temperature(deg),Initial viscosity(Pa.s)
60,0.045
75,0.023
90,0.0147

That I would like to fit on using the following function : N = a EXP (b/T) where
N is Initial viscosity and T is Temperature.

I initially used R and performed a linear regression to find a good initial value for the coefficients for now.
I did this by transforming the function to log(N) = log(a) + (b/T) and I got the following values for "a" and "b"
a = 5.572977e-08
b = 4522.083

I also used the non linear fitting function "nls" in R afterword to and the result looked good with the final "a" and "b"
being
a = 2.65624e-08
b = 4776.137
Plotting the curve with the data looked good.

I have attempted to do the same with the alglib's lsfitfit using the following code

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "interpolation.h"
#include <iostream>

using namespace alglib;
void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr)
{
func = c[0] * exp(c[1]/x[0]);
}

int main(int argc, char **argv)
{
real_2d_array x = "[[60],[75],[90]]";
real_1d_array y = "[0.045,0.023,0.0147]";
real_1d_array c = "[5.572977e-08,4522.083]";

double epsf = 0;
double epsx = 0.000001;
ae_int_t maxits = 0;
ae_int_t info;
lsfitstate state;
lsfitreport rep;
double diffstep = 0.0001;

//
// Fitting without weights
//
lsfitcreatef(x, y, c, diffstep, state);
lsfitsetcond(state, epsf, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func);
lsfitresults(state, info, c, rep);

printf("%d\n", int(info));
printf("%s\n", c.tostring(12).c_str());
}

The result of the program above
2
[0.000000055730,4522.083000000000]

I am not familiar with alglib very much so I am not sure what I am doing wrong. But it seems that the coeff. did not change...
I would really appreciate the help.

Regards

Author:  Sergey.Bochkanov [ Tue Jul 19, 2016 1:26 pm ]
Post subject:  Re: Unable to find the Coeff. using lsfitfit

Termination code 2 means that "step length is smaller than espx".

One of your coefficients - c[0] - is very small in magnitude, smaller than epsx. Algorithm stops before making first step because first step is already smaller than stopping condition. I recommend you to set scale of your variables with lsfitsetscale() - tell optimizer that scale of c[0] is 1E-8, and scale of C[1] is 1E3. As result, stopping criteria will be checked with respect to variable scales.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/