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
|