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

lsfit problem with delphi(unexpected function coefficient)
http://forum.alglib.net/viewtopic.php?f=2&t=3846
Page 1 of 1

Author:  littlebird [ Tue Mar 27, 2018 8:30 am ]
Post subject:  lsfit problem with delphi(unexpected function coefficient)

I used lsfit package to fit an non-linear function(Richards equation, the formula is:y=A/power((1+B*exp(-Kx)),1/N).
I modified the example lsfit_d_nlf. the code as below:
Code:
program XTestALGLIB;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
    SysUtils, Classes, Math, XALGLIB;

procedure function_cx_1_func(const c: TVector; const x: TVector; var func: Double; obj: Pointer);
begin
    func := c[0]/(power((1.0+c[1]*exp(-c[2]*x[0])),(1.0/c[3])))
end;

var
    x: TMatrix;
    y: TVector;
    c: TVector;
    epsx: Double;
    maxits: TALGLIBInteger;
    info: TALGLIBInteger;
    state: Tlsfitstate;
    rep: Tlsfitreport;
    diffstep: Double;
    w: TVector;

begin
    state:=nil;

    x:=Str2Matrix('[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],   [11], [12], [13], [14],   [15], [16], [17], [18],   [19], [20], [21], [22],   [23], [24], [25], [26], [27], [28], [29]]');
   
    setlength(y,29);
    y[0]:=276.6724171 ;
    y[1]:=294.7418196 ;
    y[2]:=312.999814 ;
    y[3]:=318.796855 ;
    y[4]:=327.9809221 ;
    y[5]:=348.5382184 ;
    y[6]:=373.6247255 ;
    y[7]:=390.9853543 ;
    y[8]:=   439.8460131;
    y[9]:=481.6166667 ;
    y[10]:=508.9197225 ;
    y[11]:=516.696677 ;
    y[12]:=502.5845254 ;
    y[13]:=504.3674456  ;
    y[14]:=539.8693852 ;
    y[15]:=568.5217391 ;
    y[16]:=612.2468205 ;
    y[17]:=   694.9141966 ;
    y[18]:=798.5451169 ;
    y[19]:=893.4315531 ;
    y[20]:=1004.368269 ;
    y[21]:=1152.037148 ;
    y[22]:=1274.113633 ;
    y[23]:=1401.486387 ;
    y[24]:=1578.892632 ;
    y[25]:=1776.018034 ;
    y[26]:=1972.037091 ;
    y[27]:=2205.325444 ;
    y[28]:=   2285.438293 ;
   
    c:=Str2Vector('[25080, 1841, 0.1523, 1.594]');
    epsx:=1E-6;
    maxits:=0;
    diffstep:=1E-6;

    // Fitting without weights

    lsfitcreatef(x, y, c, diffstep, state);
    lsfitsetcond(state, epsx, maxits);
    lsfitfit(state, function_cx_1_func, nil, nil);
    lsfitresults(state, info, c, rep);
    WriteLn(Format('%d', [info])); // EXPECTED: 2
    WriteLn(Format('%s', [FormatVector(c,1)])); // EXPECTED: [5884.4,19373,0.3088,3.3937]
   
    FreeAndNil(state);

    ReadLn;
    ExitCode:=0;
end.


but the result vector is [1023202.5 1419825.5 0.2 1.6].
i expected result is [5884.4,19373,0.3088,3.3937](this result is solved with lsqcurvefit function in matlab).

in the picture, the blue curve is the result solved with lsqcurvefit function in matlab,and the red curve is the result of my code.

and i think my result is not a good fit.
is my code mistake or any other question?
thanks!

Attachments:
result.jpg
result.jpg [ 53.39 KiB | Viewed 3383 times ]

Author:  Sergey.Bochkanov [ Wed Mar 28, 2018 11:10 am ]
Post subject:  Re: lsfit problem with delphi(unexpected function coefficien

Hi!

Your parameters vary wildly in magnitude. It helps to manually specify variable scales in such cases. Even rough estimates of the magnitudes will work!

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