tag:blogger.com,1999:blog-5342835410371668293.post9021700702882289485..comments2023-06-25T05:26:41.760-05:00Comments on Smarter Every Day: Arduino Linear Regression FunctionAnonymoushttp://www.blogger.com/profile/15618829446403485898noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-5342835410371668293.post-62772568245144768682022-06-26T04:35:45.980-05:002022-06-26T04:35:45.980-05:00Hi,
Thank you for the code.
I am not familiar wit...Hi, <br />Thank you for the code.<br />I am not familiar with coding. I need help in getting the code for the following linear regression equation. y=(8*10^-5)x^2+0.1873x+46.131 for arduino program. x is an analog value to give an ouput y.<br />You assistance will be helpful<br />Thank youAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-7471901043167883342020-09-16T16:56:29.304-05:002020-09-16T16:56:29.304-05:00This comment has been removed by the author.Kevinhttps://www.blogger.com/profile/01019922671191212738noreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-51222754110410536432019-03-13T13:37:50.268-05:002019-03-13T13:37:50.268-05:00hello, i have some nonlinear calibration points fo...hello, i have some nonlinear calibration points for which i need output 0 to 5 vdc linear . can you please give me code.Anonymoushttps://www.blogger.com/profile/10176170336399933138noreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-59709818361676266122017-04-20T15:22:52.711-05:002017-04-20T15:22:52.711-05:00I have found errors in the code.
According to
htt...I have found errors in the code.<br />According to <br />http://en.wikipedia.org/wiki/Simple_linear_regression<br />and compared to results from another math soft, the regression algorithm must be a bit changed.<br /><br />Complete improved code:<br />// The Wikipedia example<br />// http://en.wikipedia.org/wiki/Simple_linear_regression<br />//<br />float x[15]={1.47, 1.5, 1.52, 1.55, 1.57, 1.6, 1.63, 1.65, 1.68, 1.7, 1.73, 1.75, 1.78, 1.8, 1.83};<br />float y[15]={52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46};<br /><br />float lrCoef[2]={0,0};<br /><br />// initialize variables<br />float sum_x=0;<br />float sum_y=0;<br />float sum_xy=0;<br />float sum_xx=0;<br /><br /><br /><br />void setup()<br />{<br /> Serial.begin(9600);<br /> delay(200);<br /> <br /> // call simple linear regression algorithm<br /> simpLinReg(x, y, lrCoef, 15);<br /> Serial.println("Wikipedia example should give: beta= 61.272, alpha= -39.062");<br /> Serial.println();<br /> Serial.println("Calculated coefficients:"); <br /> Serial.println(" BETA ALPHA");<br /> Serial.print("f(x)= ");<br />//Serial.print("BETA: ");<br /> Serial.print(lrCoef[0], 4);<br /><br />//Serial.print(", ALPHA");<br /> if (lrCoef[1]>0)<br /> {<br /> Serial.print("*x + ");<br /> }<br /> else<br /> {<br /> Serial.print("*x ");<br /> }<br /> Serial.println(lrCoef[1], 4);<br />}<br /><br /><br />void loop()<br />{<br /> <br />}<br /><br /><br />void simpLinReg(float* x, float* y, float* lrCoef, int n)<br />{<br /> // pass x and y arrays (pointers), lrCoef pointer, and n. The lrCoef array is comprised of the slope=lrCoef[0] and intercept=lrCoef[1]. n is length of the x and y arrays.<br /> // http://en.wikipedia.org/wiki/Simple_linear_regression<br /><br /> // calculations required for linear regression<br /> for (int i=0; i<n; i++)<br /> {<br /> sum_x = sum_x+x[i];<br /> sum_y = sum_y+y[i];<br /> sum_xy = sum_xy+x[i]*y[i];<br /> sum_xx = sum_xx+x[i]*x[i];<br /> }<br /> <br /> // simple linear regression algorithm<br /> lrCoef[0]=(n*sum_xy-sum_x*sum_y)/(n*sum_xx-sum_x*sum_x);<br /> lrCoef[1]=(sum_y/n)-((lrCoef[0]*sum_x)/n);<br />}<br /><br />kolier31noreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-79556813765940905522017-04-20T04:23:50.744-05:002017-04-20T04:23:50.744-05:00During compilation I have got an error:
lin-regre...During compilation I have got an error:<br /><br />lin-regression_01.ino: In function 'void simpLinReg(float*, float*, float*, int)':<br />lin-regression_01.ino:76:1: error: unable to find a register to spill in class 'POINTER_REGS'<br /><br /><br />After long investigation I have moved the block:<br /> // initialize variables<br /> float xbar=0;<br /> float ybar=0;<br /> float xybar=0;<br /> float xsqbar=0;<br /><br />before void setup() section.<br />And now the code is compiled without problems.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-7247080136367440862015-01-15T14:16:55.321-06:002015-01-15T14:16:55.321-06:00I'm not sure what "tendance" is.I'm not sure what "tendance" is. Anonymoushttps://www.blogger.com/profile/15618829446403485898noreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-83821674406369269442015-01-15T04:00:10.154-06:002015-01-15T04:00:10.154-06:00Hi John,
Great code
How can I use this code in re...Hi John,<br />Great code <br />How can I use this code in real time to get the tendance of a potentiometer ?<br />Thanks youffb.boy.30https://www.blogger.com/profile/10951795737045441001noreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-64865807324059245792014-05-13T10:16:25.289-05:002014-05-13T10:16:25.289-05:00I'm glad you were able to make use of it. Tha...I'm glad you were able to make use of it. Thanks for the programming tip! Anonymoushttps://www.blogger.com/profile/15618829446403485898noreply@blogger.comtag:blogger.com,1999:blog-5342835410371668293.post-85090279104515052712014-05-06T05:39:47.254-05:002014-05-06T05:39:47.254-05:00Just what I was looking for :)
(Doing some work w...Just what I was looking for :) <br />(Doing some work with a robotic arm and potentiometer, and turns out it'd be better to calibrate the potentiometer at pre-set positions every once in a while. It can be done with a simple Excel-like program, but it's way more convenient for it to be inside the ardu to calibrate itself, a.k.a, get the Linear Regression Coefficients).<br /><br />i'd like to suggest a minor change to your code, though.<br /><br /><br />if you need to do <br /><br /> a = a + somethingtoadd ;<br /><br />it can be done simply as:<br /><br /> a += somethingtoadd ;<br /><br />Shorter and a bit more elegant<br /><br />http://arduino.cc/en/Reference/IncrementCompound<br /><br />RobotDaballohttp://www.robotdaballo.comnoreply@blogger.com