Re: Re: show residuals of circle fit
- To: mathgroup at smc.vnet.net
- Subject: [mg79987] Re: [mg79951] Re: [mg79900] show residuals of circle fit
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Fri, 10 Aug 2007 01:47:04 -0400 (EDT)
- References: <4787716.1186570751288.JavaMail.root@m35> <200708090926.FAA21585@smc.vnet.net> <26151697.1186675842720.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
I did say "nearly" identical... but Daniel is correct. With the corrected error function, Minimize finds the same circle as hypersphereFit. > This last isn't quite right for purposes of least squares fit (though > clearly it gives a fit that is "good" in some sense). For LS the > comparable thing would be > > error[xc_, yc_, r_] = (#.#) &[ > points /. {x_, y_} :> ({x,y}-{xc,yc}).({x,y}-{xc,yc}) - r^2]; Bobby On Thu, 09 Aug 2007 10:30:55 -0500, Daniel Lichtblau <danl at wolfram.com> wrote: > DrMajorBob wrote: >> Data points: >> angleRange = {-\[Pi]/4, \[Pi]}; >> points = Table[theta = RandomReal@angleRange; >> {2*Cos[theta] + 1 + .05*Random[], >> 2*Sin[theta] - 3 + .05*Random[]}, {30}]; >> Nonlinear fit and plot: >> Clear[func] >> func[x_] = Fit[points, {1, x, x^2, x^3}, x] >> -1.26986 + 0.743417 x - 0.366836 x^2 - 0.0257307 x^3 >> Show[Graphics[ >> Plot[func[x], Evaluate@Flatten@{x, Sort[points][[{1, -1}, 1]]}]], >> ListPlot[points, PlotStyle -> {Red, PointSize[Small]}], Axes -> True, >> AspectRatio -> Automatic] >> (no need to copy/paste the limits!) >> Residuals: >> ListPlot[points /. {x_, y_} :> {x, y - func[x]}] >> Circle fit and plot: >> hypersphereFit[data_List] := Module[{rhs, qq, rr, x, y, r}, >> {qq, rr} = QRDecomposition[Append[#, 1] & /@ data]; >> rhs = #.# & /@ data; >> {x, y, r} = {1/2, 1/2, 1} LinearSolve[rr, qq.rhs]; >> {{x, y}, Sqrt[r + x^2 + y^2]}] >> {center, r} = hypersphereFit@points >> Show[Graphics[Circle[center, r, angleRange]], ListPlot@points, >> AspectRatio -> Automatic] >> {{1.02827, -2.97834}, 2.00092} >> Circular residuals: >> ListPlot[points /. {x_, y_} :> {ArcTan[x, y], >> Norm[{x, y} - center] - r}] >> Directly minimizing the sum of squared residuals gives a nearly >> identical = >> circle: >> Clear[error, r] >> error[xc_, yc_, r_] = #.# &[ >> points /. {x_, y_} :> Norm[{x, y} - {xc, yc}] - r]; >> Minimize[{error[x, y, r], r > 0}, {x, y, r}] >> {0.00679177, {r -> 2.00117, x -> 1.02822, y -> -2.97891}} >> Bobby > > This last isn't quite right for purposes of least squares fit (though > clearly it gives a fit that is "good" in some sense). For LS the > comparable thing would be > > error[xc_, yc_, r_] = (#.#) &[ > points /. {x_, y_} :> ({x,y}-{xc,yc}).({x,y}-{xc,yc}) - r^2]; > > The linear algebra methods are faster when #points and/or dimension get > large. > > Daniel > -- DrMajorBob at bigfoot.com
- References:
- Re: show residuals of circle fit
- From: DrMajorBob <drmajorbob@bigfoot.com>
- Re: show residuals of circle fit