# forum.alglib.net

ALGLIB forum
 It is currently Wed Jun 26, 2019 4:24 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.

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: lsfit problem with delphi(unexpected function coefficient)Posted: Tue Mar 27, 2018 8:30 am

Joined: Sun Mar 25, 2018 2:05 am
Posts: 2
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);

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 [ 53.39 KiB | Viewed 876 times ]
Top

 Post subject: Re: lsfit problem with delphi(unexpected function coefficienPosted: Wed Mar 28, 2018 11:10 am

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

Top

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

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 2 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: