12,297,913 members (56,700 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.

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

BM
Posted 31-May-10 7:32am
Edited 31-May-10 8: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
 Peter Leow 400 OriginalGriff 365 Richard MacCutchan 354 CPallini 320 ppolymorphe 164
 OriginalGriff 9,425 ppolymorphe 5,516 CHill60 5,276 Karthik Bangalore 4,946 Richard MacCutchan 4,874