+ Reply to Thread
Results 1 to 8 of 8

Execution Speed: Implementing Polynomial Curve Fit Without Linest

  1. #1
    Registered User
    Join Date
    10-17-2008
    Location
    UK
    Posts
    17

    Execution Speed: Implementing Polynomial Curve Fit Without Linest

    Hi,

    I have had a search on this forum and using google and wasn't able to find anything that is quite what i was looking for.

    I have a VBA function that repeatedly performs a second order polynomial curve fit on some x and y values (normally 6 pairs) and then uses the relationship to interpolate a final value. This is called many tens of thousands of times in a reasonably complicated model. I beleive that this functon is the slowest part of my code and hence was looking into options to speed it up.

    I was wondering whether anyone could tell me whether it would be worth imlementing this in pure VBA rather than using the LINEST and POWER functions, would this likely give a speed increase? I beleive I have the general algorithm for calculating the fit (but if anyone can point me at some pre-written code, that would be great) and I beleive it would need to calculate averages etc for the data. Should I also implement such functions in VBA? (bearing in mind the small dataset) or would the application calls be faster.

    I realise that for a real speed increase I should built this function in another language and compile it as an add in/dll, however due to lockdowns in effect on the machines we use this is not a realistic option.

    This might be a case of 'try it and see', but i thought I may be able tog et some sage advice and save myself some time.

    Regards
    Stephen

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    Stephen,
    It is possible that VBA can beat LINEST if all you want are the three coefficients of the quadratic polynominal to perform the interpolation. However, as a person with a staitstical bent, it hurts my head that you are only using 6 points to to these fits. How well are the data points matched to a quadratic? Is there some reason that you are using a quadratic, maybe something in the model that tells you that the points should follow a quadratic curve?

    If you look here you will find the basic equations to find the three coefficients in the least squares fit to a set of data points. Let me know if you want to help you implement them in VBA.
    Last edited by blane245; 10-13-2010 at 08:36 AM.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    10-17-2008
    Location
    UK
    Posts
    17

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    Hi thanks for your reply.

    Essentially we have a simplified model that can give y values at only 6 known x values (these differ depending on the inputs given to the simplified model), we know that a second order polynomial fits all six of these values reasonably well and so it is used to perform a simple interpolation of the six values. We have validated this by running the full (and extremely slow) model and the final values predicted are well within our tolerances.

    If we were ever especially sensitive to the result we would run a full model, however the fast model allows us to perform fast calculations to a degree of accuracy that is perfectly acceptable to us.

    The expected response is non linear and most represents a 2nd order polynomial curve (for the bounded region we are looking at).

    I hope this makes sense, please say if not.

    I will take a look at implementing this entirely in code and see how well it performs.

    Kind regards

    Stephen

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    Great! Hope the web site I pointed to you helps. This is the most reduced set of equations to get to the quadratic coefficients. Note that the demoninator is the same for all three, so you need to calculate it only once and make sure you check for zero! Also note that S00 = number of data points, so you don't need to calculate it.

  5. #5
    Registered User
    Join Date
    10-17-2008
    Location
    UK
    Posts
    17

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    Hi,

    The website was very helpful, thank you.

    I just wondered if you had a similar function/info to get the 3rd order coefficients? I figure while i'm looking at it it might be a useful tool to have.

    Regards
    Stephen

  6. #6
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    The equations get a bit messier as you get to higher order polynominals. You have to get the inverse of a N+1 matrix where N is the order of polynomial. If you look at the original web site, the equation:

    Please Login or Register  to view this content.
    changes to
    Please Login or Register  to view this content.
    so you need to solve four equations with 4 unknowns. There are a few techniques for doing matrix inversion so might try to find. here is one such method.

  7. #7
    Registered User
    Join Date
    10-17-2008
    Location
    UK
    Posts
    17

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    Thank you once again, I will get reading!

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Execution Speed: Implementing Polynomial Curve Fit Without Linest

    There are a few techniques for doing matrix inversion ...
    One of which is Excel's native MINVERSE.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1