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

Curve fitting: Reproducing Excel
http://forum.alglib.net/viewtopic.php?f=2&t=27
Page 2 of 2

Author:  Doug Jenkins [ Wed Jun 30, 2010 10:05 am ]
Post subject:  Re: Curve fitting: Reproducing Excel

I'm glad that fixed it! I'm certainly not a maths pro, but I do use maths applications in my work as an engineer.

But if you want to replicate the behaviour of the Excel curve fitting function you don't want the curvefit routines (which fit a best approximation smooth curve through scattered data) but rather the curvebuild routines, which fit a smoothcurve exactly through each specified point. The closest match to the Excel function is probably the CatmullRom curve, with a low "tension" value (0 or 0.1 I think). VBA code for that is

Code:
Function CRSpline1DA(XA As Variant, YA As Variant, XIA As Variant, Optional EndType As Long = 0, Optional Tension As Double = 0) As Variant
Dim CMResA() As Double, NumXRows As Long, NumXIrows As Long, i As Long, J As Long
Dim XAD() As Double, YAD() As Double, Rtn As Variant, Tbl() As Double
Dim C1 As Spline1DInterpolant

Rtn = GetSplineData(XA, YA, XAD, YAD, NumXRows, XIA, NumXIrows)

If Rtn <> 0 Then
CRSpline1DA = Rtn
Exit Function
End If

Spline1DBuildCatmullRom XAD, YAD, NumXRows, EndType, Tension, C1

ReDim CMResA(1 To NumXIrows, 1 To 1)
For i = 1 To NumXIrows
CMResA(i, 1) = Spline1DCalc(C1, XIA(i, 1))
Next i

CRSpline1DA = CMResA

End Function


If you want the parameters of the curve, rather than the Y values for specific X values use:

Spline1DUnpack C1, NumXRows, Tbl

instead of Spline1DCalc(C1, XIA(i, 1))


Note that the curves are made up of a series of cubic curves (a + bx +cx^2 + dx^3), rather than one higher order polynomial curve. Look in the AlgLib manual and code for more details.

You also might find the following posts from my blog helpful (and I hope not too mathematical):
http://newtonexcelbach.wordpress.com/20 ... c-splines/
http://newtonexcelbach.wordpress.com/20 ... l-splines/

Also follow up the comments from Lori Miller on my blog, which are talking about replicating the Excel smooth curves.

Author:  ALGLIBNewbie [ Wed Jun 30, 2010 2:10 pm ]
Post subject:  Re: Curve fitting: Reproducing Excel

Thanks for pointing me to the CatmullRom curve. This sounds good. (Though everything I grounded with initially sounded good.)

Here are my questions to your VBA code.

(1) What does XAD stand for? What is it?
(2) What does YAD stand for? What is it?
(3) What does XIA stand for? What is it?
(4) It seems I need some data mentioned as "input variant array" you get by VBA method GetSplineData. Do I need this information? How do I create this spline data from my x, y columns?

So far I took my x/y columns and used spline1dbuildcatmullrom:
Code:
   // Input objects
   ap::real_1d_array xValues;
   ap::real_1d_array yValues;
   int numberOfValues = 56;
   xValues.setcontent(0, numberOfValues, xDoubles);
   yValues.setcontent(0, numberOfValues, yDoubles);

   spline1dinterpolant interpolant; // Return object
   spline1dbuildcatmullrom(xValues, yValues, numberOfValues, 0, 0.1, interpolant);

Author:  Doug Jenkins [ Thu Jul 01, 2010 12:04 pm ]
Post subject:  Re: Curve fitting: Reproducing Excel

Quote:
(1) What does XAD stand for? What is it?
(2) What does YAD stand for? What is it?
(3) What does XIA stand for? What is it?


The best thing to do is find the AlgLib functions in my code, and refer to the AlgLib documentation to find out what the variables are, so in:

Spline1DBuildCatmullRom XAD, YAD, NumXRows, EndType, Tension, C1

XAD is the xvalues array, and YAD is the yvalues array.

XIA is the array of x values where you want interpolated values, which you use in:

CMResA(i, 1) = Spline1DCalc(C1, XIA(i, 1))

Spline1DCalc returns the y value from the spline specified by C1 at x = XIA(i,1)


Quote:
(4) It seems I need some data mentioned as "input variant array" you get by VBA method GetSplineData. Do I need this information? How do I create this spline data from my x, y columns?


If you are not wanting to get data from Excel and use it as input to an AlgLib routine you don't need to worry about this. When you bring in an Excel range as an array it is a 2D array of variants, and needs to be converted to the form required by AlgLib, usually a 1D array of doubles. That's what GetSplineData does.

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