0

I am trying to fit some points to an inverse parabola, in the form of F(x)=1/(ax^2+bx+c).

My objective is to program a function in c++ that would take a set of 10-30 points and fit them to the inverse parabola.

I started trying to get an analytical expression using Least squares, but I can't reach to get a result. I tried by hand (little crazy) and then I tried to solve analytically the expressions for a,b and c, but mupad doesn't give me a result (I am pretty new to Matlab's mupad so maybe i am not doing it correctly). i don't know anymore how to approach the problem.

Can I get a analytical expression for this specific problem? I have also seen algorithms for general least squares fitting but I don't need a so complicated algorithm, I just need it for this equation. If not, how would StackOverflow people approach the problem?

If needed I can post the equations, and the small Mupad code I've tried, but I think is unnecessary.

EDIT: some example

Im sorry the image is a little bit messy but it is the thing i need. The data is in blue (this data is particularly noisy). I need to use only the data that is between the vertical lines (a bunch of data in the left and another one in the right).

The result of the fit is in de red line.

all this has been made with matlab, but I need to make it in c++.

I'll try to post some data...

enter image description here

Edit 2: I actually did the fitting in Matlab as follows (not the actual code):

 create linear system Ax = b, with 
 A = [x²  x  1]
 x = [a; b; c]
 b = 1/y;

It should work, shouldn't it? I can solve then using Moore-Penrose pseudoinv calculated with SVD. Isn't it?

Delimitry
  • 2,987
  • 4
  • 30
  • 39
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • So, given 3 pairs of `(x,y)` you need calculate `a`, `b` and `c`? – Lol4t0 Dec 17 '12 at 16:15
  • given n pairs of (x,y) actually! @Lol4t0 – Ander Biguri Dec 17 '12 at 16:16
  • This isn't a programming problem, this is a math/statistics problem. That being said, only *linear* least squares have analytical solutions. Nonlinear least squares require numerical approaches. – In silico Dec 17 '12 at 16:20
  • @Insilico, where should I post this? it is not a methemathic problem but a programming problem! What I need is to PROGRAM, I know methods, but it is not the same knowing methods than knowing how to program methods. I can solve the problem in matlab and mupad in 5 minutes but can't solve it in c++. So nice, I can't ask in mathemathics forum because it is a programming question adn I can't ask in stackoverflow because it is a meth question. LIMBO! – Ander Biguri Dec 17 '12 at 17:32

2 Answers2

2

There is no analytic solution of least squares; it is an minimisation problem, and requires clever iterative methods to solve. (nonlinear LS - thanks @insilico)

You could try a Newton style iterative method (by rearranging your equation) but I don't think it will converge easily for your function -- which is going to be highly nonlinear at two points!

I'd recommend using a library for this. such as nelder-mead search

http://www.codecogs.com/code/maths/optimization/nelder.php

you simply provide your error function - which is

sum( pow(F(x) - dataY(x), 2) ) 

and provide a set of initial values (a stab in the dark at the solution); I've had good success with nelder-mead.

I don't think you will find a good plain-coded solution.

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
  • Ok, ill check it, but you can actually get an anallytical solution for some least squares method easily, It is a analyticall solution that has summatories (so iterations) but still it is an anallitical solution. I have already programmed some least squares that way. – Ander Biguri Dec 17 '12 at 16:21
  • @AnderBiguri you are quite right, I forgot to write 'nonlinear'. You can indeed do it for a normal parabola as it is linear combinations of [1, x, x^2]. – Sanjay Manohar Dec 17 '12 at 16:37
  • Oh, but the nelder function you post is commercial... i need to use free code. I'll try to check more – Ander Biguri Dec 17 '12 at 16:42
  • Oh - sorry. This guy has a clearly documented short implementation of the algorithm - http://www.colincaprani.com/files/programs/Nelder%20Mead.zip – Sanjay Manohar Dec 17 '12 at 16:56
  • Hey! what do you think of the proposition I made? would it work? – Ander Biguri Dec 17 '12 at 17:27
  • They closed my question... Well thanks a lot anyway! – Ander Biguri Dec 17 '12 at 17:33
1

If I understand you correctly, you just need to know the formula for a fit for a particular data set, right?

If so, then you just need to get a curve fitting program and fit the curve using your desired method. Then, implement the formula shown by the curve fit.

There are a few curve fit programs out there:

Curve Expert

http://www.curveexpert.net/

* Eurequa *

http://creativemachines.cornell.edu/eureqa

Additionally, some spreadsheet packages may have the curve fitting facilities you need.

I would be happy to try to do a fit for you if you provide the data. No guarantees on getting the fit you want.

RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23
  • thanks i will check them. I can give you data to fit, but it will be more or less a bell shaped curve (that can be represented by the inverse parabola equation) with some random noise. nothing weird. – Ander Biguri Dec 17 '12 at 16:23
  • @AnderBiguri: That's fine. If the above don't fit your needs, just let me know. Keep in mind that my fit would be an attempt to plug it into CurveExpert anyway, with at best an intermediate "massaging" if it grouses about the data. – RonaldBarzell Dec 17 '12 at 16:25
  • sorry we misanderstood. I dont need the data for a particular set, I need a fitting algorithm. The data will be not very different from test to test but still i need a generalization. – Ander Biguri Dec 17 '12 at 16:37
  • Ok, sorry about that. Would you mind clarifying the question just to emphasize this? – RonaldBarzell Dec 17 '12 at 16:41
  • Thanks for noting it, sometimes when you ask you have so clear what you want that you forgot to explain it in detail :) – Ander Biguri Dec 17 '12 at 16:44
  • I understand; been there :). Also, if you could add a small snippet of sample data with a desired result (if available), this might give people an idea of what's needed, in case they know it by a different name. As it stands, it sounds like you're trying to do a Quadratic fit, but I could be off. Seeing some data would let me know for sure, as I can at least try it and go from there as time permits. – RonaldBarzell Dec 17 '12 at 16:46
  • Hey! what do you think of the proposition I made? would it work? – Ander Biguri Dec 17 '12 at 17:28
  • They closed my question... Well thanks a lot anyway! – Ander Biguri Dec 17 '12 at 17:35
  • No problem... I guess you can try math overflow, but you'd have to phrase it as an algorithmic rather than programming question. – RonaldBarzell Dec 17 '12 at 17:37