Re: Generating best-fit line formula
- To: mathgroup at smc.vnet.net
- Subject: [mg103437] Re: Generating best-fit line formula
- From: ÐÐÐÐÑÐÐ ÐÑÑÐÐÐÐ <astanoff at gmail.com>
- Date: Mon, 21 Sep 2009 07:08:57 -0400 (EDT)
Good day, If what you're looking for is a symbolic fitting, this could help you : In[1]:= myLinearFit[data_List, x_] := Module[{xx, yy, mx, my, a, b}, xx = data[[All, 1]]; yy = data[[All, 2]]; mx = Mean[xx]; my = Mean[yy]; a = ((xx - mx).(yy - my))/((xx - mx).(xx - mx)); b = my - a * mx; Simplify[a*x + b] ]; In[2]:= myLinearFit[{{x1, y1}, {x2, y2}, {x3, y3}}, x] Out[2]= (1/(2*(x1^2 + x2^2 - x2*x3 + x3^2 - x1*(x2 + x3))))*(x2^2* y1 + x3^2*y1 - x2*x3*y2 + x3^2*y2 + x2^2*y3 - x2*x3*y3 + x1^2*(y2 + y3) - x1*(x2*(y1 + y2) + x3*(y1 + y3)) + x*((-x3)*(y1 + y2 - 2*y3) - x2*(y1 - 2*y2 + y3) - x1*(-2*y1 + y2 + y3))) In[3]:= myLinearFit[{{1, 2}, {2, 3}, {3, 5}}, x] Out[3]= (1/6)*(2 + 9*x) Check : In[4]:= Fit[{{1, 2}, {2, 3}, {3, 5}}, {1, x}, x] // Rationalize // Simplify Out[4]= (1/6)*(2 + 9*x) hth -- Valeri Astanoff