How to fit the 2D scatter data with a line with C++

前端 未结 7 1611
春和景丽
春和景丽 2020-12-16 23:19

I used to work with MATLAB, and for the question I raised I can use p = polyfit(x,y,1) to estimate the best fit line for the scatter data in a plate. I was wondering which r

7条回答
  •  被撕碎了的回忆
    2020-12-16 23:53

    I would suggest coding it from scratch. It is a very simple implementation in C++. You can code up both the intercept and gradient for least-squares fit (the same method as polyfit) from your data directly from the formulas here

    http://en.wikipedia.org/wiki/Simple_linear_regression#Fitting_the_regression_line

    These are closed form formulas that you can easily evaluate yourself using loops. If you were using higher degree fits then I would suggest a matrix library or more sophisticated algorithms but for simple linear regression as you describe above this is all you need. Matrices and linear algebra routines would be overkill for such a problem (in my opinion).

提交回复
热议问题