forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 1:34 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  [ 2 posts ] 
Author Message
 Post subject: Unable to find the Coeff. using lsfitfit
PostPosted: Sun Jul 17, 2016 3:49 pm 
Offline

Joined: Sun Jul 17, 2016 2:50 pm
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: Unable to find the Coeff. using lsfitfit
PostPosted: Tue Jul 19, 2016 1:26 pm 
Offline
Site Admin

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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 21 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group