Polynomial Fit Functions

screen-shot

Polynomial Fit Functions

RegressionObject.cls contains a class that provides an easy way to add polynomial regression functionality to any application. If you just want linear regression of a very high degree, no matter; this class has good performance and scales seamlessly with the complexity of your problem.

What Is a Regression, Anyway?

Given a number of two-dimensional data points—maybe as pairs (x,y) of coordinates, maybe as measured values of current and voltage, etc.—the problem arises of how to fit a theoretical curve that matches these data as good as possible. Very common is the use of linear approximation or polynomial fits of the form:

    y(x) = c0*x^0 + c1*x^1 + c2*x^2 + c3*x^3 + ...

Two special cases of these polynoms everyone is familiar with are the first and second order curves (straight line and parabel):

    y(x) = m*x + n          (linear regression)
    y(x) = a*x^2 + b*x + c  (parabolic regression)

The good news is: That's all you need to know about mathematics. These functions exist and can give a smooth fit for your data.

How to Use the Class

Include RegressionObject.cls to your project workspace and create an instance of the class:

    Dim Reg As New RegressionObject

Decide for the highest power of x (the degree of the polynom) you want to consider. Be careful with too-high values! Use 1 for linear regression and 2 for parabolic. The class limits the degree to 25. Higher values lead to datatype overflow and even 10 is much higher than I can think of. If you have a shortage of memory, change the limit in RegressionObject.cls, "Private Const MaxO& = 25", to a smaller value.

    Reg.Degree = 2    '2 is also the default value

Then, add your data points. No extra memory is consumed because these data are not actually stored in the class. Add as many as you like!

    For i= 1 To MyDataCount
      Reg.XYAdd MyData(i).Voltage, MyData(i).Current
    Next i

When done, maybe you want to draw the fit curve to a PictureBox?

    Pic1.CurrentX = xMin
    Pic1.CurrentY = Reg.RegVal(xMin)
    For X = xMin to xMax
      Pic1.Line -(X, Reg.RegVal(X))
    Next X

That's it!

I Know My Math. Give Me the Coefficients!

The work that the class object does for you is to calculate the coefficients ( c0, c1, c2, c3, ...) in the general equation of the polynom:

    y(x) = c0*x^0 + c1*x^1 + c2*x^2 + c3*x^3 + ...

If you want to use them outside of the class object, you can retrieve them with the Coeff() property:

    a = Reg.Coeff(2)
    b = Reg.Coeff(1)
    c = Reg.Coeff(0)    ' gives the coeffs for y = a*x^2 + b*x + c

or

    m = Reg.Coeff(1)
    n = Reg.Coeff(0)    ' for y = m*x + n

Additional Notes

It is not possible to retrieve the original data from the class object because they are NOT stored there. However, if you forgot it, you can check how many datapoints already have been added to the regression. The XYCount property provides this information.

I used VB 6.0 (SP4) to build and test the code. It is supposed to work with previous VB editions without any changes.

This class is built on my knowledge of mathematics and on the implementation suggestion for the gauss algorithm from R. Sedgewick's Algorithms ... book.

Thanks to U. Hannemann for bringing up the idea of implementing a second order regression in the first place, which I extendend to this more general task.

Frank Schindler, Dresden, Germany, 16.9.2000
e-mail: nospam@matemaster.de

1st Revision, Dresden, Germany, 23.9.2003

Downloads

Download zipped files - 5 Kb



Comments

  • Is it possible to force the function go through a fixed point like origin(0,0)

    Posted by Tao Wang on 05/15/2015 02:04pm

    Hello, Is it possible with this class, that we can let the function go through some fixed point like the origin (0,0)? Best Regards, Tao Wang

    Reply
  • engineer

    Posted by Dirk Bruere on 11/11/2014 05:26am

    Hi - this looks good. Before I go ahead and convert it, is there already a copy in C or C++?

    Reply
  • EO Tech Lead

    Posted by David Panka on 05/22/2014 02:33pm

    is there also a coefficient for the R^2 value (the goodness of fit for a polynomial)?

    Reply
  • engineer

    Posted by Peter on 07/11/2012 12:58pm

    Thank you. This works very good!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Employees must exchange sensitive emails with customers and partners. These emails might contain protected health information, protected financial information, or corporate information that should not be made public. Globalscape® Mail Express® allows you to encrypt the emails that it manages so that no one but the sender and recipient--not even the administrator--can view the contents of the email. "Secure the Transfer of Sensitive Emails" is the property of GlobalSCAPE

  • This white paper focuses on how to protect and restore Microsoft Exchange data with Veeam. It briefly covers Microsoft Exchange 2013 and Veeam Backup & Replication v8 architecture, after which it focuses on how to handle proper backup, replication and recovery of Exchange in vSphere and Hyper-V environments.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date