forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 2:58 am

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  [ 13 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Curve fitting: Reproducing Excel
PostPosted: Wed Jun 30, 2010 10:05 am 
Offline

Joined: Sun May 16, 2010 11:42 pm
Posts: 63
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.

_________________
Doug Jenkins
http://newtonexcelbach.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Curve fitting: Reproducing Excel
PostPosted: Wed Jun 30, 2010 2:10 pm 
Offline

Joined: Wed Jun 16, 2010 2:05 pm
Posts: 7
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);


Top
 Profile  
 
 Post subject: Re: Curve fitting: Reproducing Excel
PostPosted: Thu Jul 01, 2010 12:04 pm 
Offline

Joined: Sun May 16, 2010 11:42 pm
Posts: 63
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.

_________________
Doug Jenkins
http://newtonexcelbach.wordpress.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

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