Linear Curve Fit Function

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Linear Curve Fit Function

Post by Narcís » Tue Nov 03, 2009 10:26 am

Hi Hans,

According to what Marjan described here you could calculate y in PlotSmoothFit like this:

Code: Select all

      y := coeffs[1] * x - coeffs[1] * MinXValue + coeffs[0] + MinYValue;
which would be the equivalent to Poly funciton call you make:

Code: Select all

      y := Poly( x-MinXValue, coeffs ) + MinYValue;
However, to get same y values as source series you should not add coeffs[0]:

Code: Select all

      y := coeffs[1] * x - coeffs[1] * MinXValue + MinYValue;
Which would be the equivalent to populating coeffs array like this:

Code: Select all

    for i := 2 to imax do
      coeffs[ i-1 ] := AnswerVector[ i ];
So the full method would be like this:

Code: Select all

procedure PlotSmoothFit( var serFit, serSmoothFit : tFastLineSeries;
                         FitFctn : TCurveFittingFunction;
                         smoothness : integer );
var interval, x, y : double;
    i, imax : integer;
    coeffs : array of double;
begin
  with FitFctn do
  begin
    imax := PolyDegree;
    SetLength( coeffs, imax );
    for i := 2 to imax do
      coeffs[ i-1 ] := AnswerVector[ i ];
  end;

  serSmoothFit.DataSource:=nil;

  with serFit do
  begin
    interval := ( MaxXValue - MinXValue )/ smoothness;

    for i := 1 to smoothness do
    begin
      x := MinXValue + (i-1) * interval;
      y := Poly( x-MinXValue, coeffs ) + MinYValue;
      //y := coeffs[1] * x - coeffs[1] * MinXValue + coeffs[0] + MinYValue;
      serSmoothFit.AddXY( x, y );
    end;
  end
end;
Hope this helps!
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Hans Wolfgang
Newbie
Newbie
Posts: 58
Joined: Fri Nov 15, 2002 12:00 am
Location: Naples, FL
Contact:

Re: Linear Curve Fit Function

Post by Hans Wolfgang » Wed Nov 04, 2009 4:12 pm

Yes thanks Narcis this was a great help. The linear fit works fine.

However (see attached) with the cubic fit (polydegree = 4), there is again a bias in fitted results without AnswerVector[1]. If I include it, the match is better. So, if you agree, I will alter the code to test for polydegree and act accordingly.

"Better" in that, for the cubic example in the attachment, the fitted polynomial appears to be evaluated only at the input data points, and it looks like straight lines are drawn between them. That was the original motivation for the "smooth fit", which, as you will see in the attached code, gives the true fitted curve through the entire range of the input data.
Attachments
ENW-Test01.zip
(9.32 KiB) Downloaded 692 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Linear Curve Fit Function

Post by Narcís » Thu Nov 05, 2009 9:16 am

Hi Hans,
Yes thanks Narcis this was a great help. The linear fit works fine.
You're welcome, I'm glad to hear that!
However (see attached) with the cubic fit (polydegree = 4), there is again a bias in fitted results without AnswerVector[1]. If I include it, the match is better. So, if you agree, I will alter the code to test for polydegree and act accordingly.
Yes, sure.
"Better" in that, for the cubic example in the attachment, the fitted polynomial appears to be evaluated only at the input data points, and it looks like straight lines are drawn between them. That was the original motivation for the "smooth fit", which, as you will see in the attached code, gives the true fitted curve through the entire range of the input data.
Ok, or in that case you can just "play" with function's Factor property, for example:

Code: Select all

  TTeeFunction1.Factor:=4;
Doing so you wouldn't have the need to perform the "smooth fit":

Code: Select all

procedure TResTime.FormCreate(Sender: TObject);
begin
  with RtLabData do begin
    AddXY( 5.005E+02, 5.86E+03 );
    AddXY( 6.665E+02, 4.00E+03 );
    AddXY( 8.366E+02, 2.98E+03 );
    AddXY( 9.530E+02, 2.62E+03 );
    AddXY( 1.325E+03, 1.79E+03 );
  end;
  tTeeFunction1.PolyDegree := 4;
  TTeeFunction1.Factor:=4;
  tTeeFunction1.AddPoints( RtLabData );
  //PlotSmoothFit( serFit, serSmoothFit, tTeeFunction1, 500 );
end;
Also, with the code above you could calculate smoothing like in the code below to obtain identical series.

Code: Select all

procedure PlotSmoothFit( var serFit, serSmoothFit : tFastLineSeries;
                         FitFctn : TCurveFittingFunction;
                         smoothness : integer );

var x, y : double; i, imax : integer; coeffs : array of double;

begin
  with FitFctn do begin
    imax := polydegree;
    SetLength( coeffs, imax );
    for i := 1 to imax do
      coeffs[ i-1 ] := AnswerVector[ i ];
    { end }
  end;

  with serFit do begin
    for i := 0 to Count-1 do begin
      x := XValue[i];
      y := poly( x-minXvalue, coeffs ) + minYvalue;
      serSmoothFit.AddXY( x, y )
    end;
  end
end;
Hope this helps!
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Hans Wolfgang
Newbie
Newbie
Posts: 58
Joined: Fri Nov 15, 2002 12:00 am
Location: Naples, FL
Contact:

Re: Linear Curve Fit Function

Post by Hans Wolfgang » Thu Nov 05, 2009 12:42 pm

What is the Factor property? How does it work? My D6 TC5 environment does not know anything about a "Factor". I can go on with smooth fit anyway.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Linear Curve Fit Function

Post by Narcís » Thu Nov 05, 2009 12:51 pm

Hi Hans,

Below there's Factor description at TeeChart Pro v8 documentation.

Default : 4

Gets or sets the number of times the resulting smooth points are in quantity compared to source points.

For example, a value of 4 means the smooth points will be 4 times the number of source points.

The bigger the factor property is, the smoothest resulting curves will be.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Hans Wolfgang
Newbie
Newbie
Posts: 58
Joined: Fri Nov 15, 2002 12:00 am
Location: Naples, FL
Contact:

Re: Linear Curve Fit Function

Post by Hans Wolfgang » Fri Nov 06, 2009 1:06 am

Thanks. I'm upgrading right away.

In the attached project I cannot access values of two series.The x and y values show up as xxxE-38. To replicate, please put a breakpoint In uTest2.pas line 120 (for first series) and 122 (for second series), and look at rmax and rmin. Yet the interface shows that the series were plotted correctly.

I then want to reuse one or the other series but cannot access their values.

Would you please show me what is going wrong here.
Attachments
ENW-Test04.zip
(15.53 KiB) Downloaded 637 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Linear Curve Fit Function

Post by Narcís » Fri Nov 06, 2009 9:16 am

Hi Hans,
Thanks. I'm upgrading right away.
Excellent :!:
In the attached project I cannot access values of two series.The x and y values show up as xxxE-38. To replicate, please put a breakpoint In uTest2.pas line 120 (for first series) and 122 (for second series), and look at rmax and rmin. Yet the interface shows that the series were plotted correctly.

I then want to reuse one or the other series but cannot access their values.
I'm not sure about what's going on but I guess it must be a Delphi debugger issue. If you use rmin and rmax as in code below and add a breakpoint at Chart.Title.Text.Add lines you'll see variables value being correct. It's as if debugger doesn't update variables value until they are used.

Code: Select all

  rmax := serFit.maxYvalue;
  rmin := serFit.minYvalue;
  Chart1.Title.Text.Add(FloatToStr(rmax) + ' - ' + FloatToStr(rmin));

  rmax := serSmoothFit.maxYvalue;
  rmin := serSmoothFit.minYvalue;
  Chart1.Title.Text.Add(FloatToStr(rmax) + ' - ' + FloatToStr(rmin));
Does this work fine for you? Can you use series' values successfully at a later time?

Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply