Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Curve Fitting Implimentation in C++ (SVD Least Sqares Fit or similar)

Tags:

c++

curve

I have been scouring the internet for quite some time now, trying to find a simple, intuitive, and fast way to approximate a 2nd degree polynomial using 5 data points.

I am using VC++ 2008.

I have come across many libraries, such as cminipack, cmpfit, lmfit, etc... but none of them seem very intuitive and I have had a hard time implementing the code.

Ultimately I have a set of discrete values put in a 1D array, and I am trying to find the 'virtual max point' by curve fitting the data and then finding the max point of that data at a non-integer value (where an integer value would be the highest accuracy just looking at the array).

Anyway, if someone has done something similar to this, and can point me to the package they used, and maybe a simple implementation of the package, that would be great!

I am happy to provide some test data and graphs to show you what kind of stuff I'm working with, but I feel my request is pretty straightforward. Thank you so much.

EDIT: Here is the code I wrote which works! http://pastebin.com/tUvKmGPn

change size to change how many inputs are used

0 0 1 1 2 4 4 16 7 49

a: 1 b: 0 c: 0 Press any key to continue . . .

Thanks for the help!

like image 893
Shawn Tabrizi Avatar asked Jun 27 '12 19:06

Shawn Tabrizi


People also ask

What is least square method of curve fitting?

The least square method is the process of finding the best-fitting curve or line of best fit for a set of data points by reducing the sum of the squares of the offsets (residual part) of the points from the curve.

Which method is best for curve fitting?

Curve Fitting using Polynomial Terms in Linear Regression Despite its name, you can fit curves using linear regression. The most common method is to include polynomial terms in the linear model. Polynomial terms are independent variables that you raise to a power, such as squared or cubed terms.

Is curve fitting the same as regression?

Regressing (or curve fitting) your data to this equation yields y=1–e−t0.0023. Explicitly, this says that in ≈0.0023 sec, 63.2% of the response has completed. I have the benefit of not knowing your specific noise or data variance, so I took the liberty to curve fit a 2-parameter model to see if it tightens the error.


2 Answers

Assuming that you want to fit a standard parabola of the form

    y = ax^2 + bx + c 

to your 5 data points, then all you will need is to solve a 3 x 3 matrix equation. Take a look at this example http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html - it works through the same problem you seem to be describing (only using more data points). If you have a basic grasp of calculus and are able to invert a 3x3 matrix (or something nicer numerically - which I am guessing you do given you refer specifically to SVD in your question title) then this example will clarify what you need to do.

like image 77
mathematician1975 Avatar answered Oct 17 '22 04:10

mathematician1975


Look at this Wikipedia page on Poynomial Regression

like image 24
Doug Currie Avatar answered Oct 17 '22 03:10

Doug Currie