12,818,315 members (35,343 online)
Rate this:
See more:
Hello,

I have been trying to curve fit a smooth and slightly oscillating curve. I have already tried to model this curve in MATLAB using the built in function '`polyfit`' and to graph it using '`polyval`'. Modeling and graphing using MATLAB was successful :) .

The ultimate goal of mine is to write this program in C++ in which I can model and retrieve polynomial coefficients using least squares fit. I was partially successful writing this program, however, the results compared to MATLAB result were not correct. I assume that the results I get from MATLAB is correct and this is a fairly good assumption for my case.

As a reference, I have used this sample program someone already has written, http://www.nr.com/forum/showthread.php?t=1452[^]. This program is evaluated using the headers from 'Numerical Recipes'.

I was wondering if there is any better way to approach the least squares fit problem. :confused:

Thank you for your time and hope to get some advise soon!

BM
Posted 31-May-10 8:32am
Updated 31-May-10 9:26am
v2

Rate this:

## Solution 1

What does you mean, exactly with
Jungyeoul Maeng wrote:
I was partially successful writing this program, however, the results compared to MATLAB result were not correct

?

What is unsatisfactory in your fit?
:)
Jungyeoul Maeng 2-Jun-10 9:55am

The result I get from MATLAB and C++ programs are off by as much as 30%. I believe that the curve fit from C++ does not quite match the ideal curve fit from MATLAB. I was looking for a more precise curve fit result from C++. Is there any reference I can take a look about least squares fit?
CPallini 2-Jun-10 14:04pm

How do you measure it (sorry for being nitpick)?
Rate this:

## Solution 2

Top Experts
Last 24hrsThis month
 Jochen Arndt 180 Richard Deeming 145 Graeme_Grant 140 ppolymorphe 130 OriginalGriff 110
 OriginalGriff 4,507 Graeme_Grant 3,789 Karthik Bangalore 3,471 Jochen Arndt 2,571 ppolymorphe 2,417