Re: changing algorithm, finding residuals w/FindFit

*To*: mathgroup at smc.vnet.net*Subject*: [mg56904] Re: changing algorithm, finding residuals w/FindFit*From*: dh <dh at metrohm.ch>*Date*: Tue, 10 May 2005 03:42:07 -0400 (EDT)*References*: <d59l9q$6h6$1@smc.vnet.net> <42787687.5090305@metrohm.ch> <d5ctld$mcc$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi Ed, see below Sincerely, Daniel Edward Peschko wrote: >On Wed, May 04, 2005 at 09:15:19AM +0200, dh wrote: > >>Hi Ed, >>if you want linear least square, you can use the one liner: >>points[[All, 2]] - ( Fit[points, {1, x, x^2}, x] /. x :> points[[All, 1]]) > > >ok, now suppose that I wanted to make a list of points containing both the >residual *and* the x-coordinate associated with that residual. > >The best I've come up with is: > >residuals = Thread > [ > { > points[[All,1]], > points[[All,2]] - (func /. x:> points[[All,1]] ) > } > ] > Your ideas are basically o.k., but the above will not work. Thread takes a FUNCTION, not an EXPRESSION. Take another point of view: Considering the list of points, you want to replace the y values by the residuals. Towards this aim we define a pure function: (...)& and use Map /@: {#[[1]],#[[2]]-fun/.x->#[[1]] }& /@ points This leaves the x value unchanged and replaces the y value by the residual. Further, if you later want to encapsulate everything into a function, it is not convenient to work with expressions like your "func" because you have the arbitrary variable "x" . It is better to use a "pure" function. E.g. fun1=Evaluate[fun/.x -> # ]& this can now be applied like a "real" function. e.g. fun1[2.] With this we can define the function residuals: residuals[points_,fun_]:={#[[1]],#[[2]]-fun[#[[1]]]}& /@ points and you would call it like: residuals[points,fun1] Note that we need a "pure" function now. >where func == 1 + x + 0. x^2. I'm not sure, but this is how I read it: > > 'All' gives a cross-array slice in an array of arrays > All simply means every value of the index. It does not matter if we have a 1,2,.. dimensional array. > '-', given two arrays, applies that operation to each element of that > array > provided the two arrays have the same shape or we have a scalar and an array. > > x:> points[[All, 1]] applies a production rule that substitutes x for > each x value in the cross-array slice. > > the parens group in the second expression so that instead of doing each > combination of evals and getting back > > ( > (y1 - func(x1), y1 - func(x2), y1 - func(x3)), > (y2 - func(x1), y2 - func(x2), y2 - func(x3)), > etc. > ) > > you get back: > > (y1 - func(x1), y2-func(x2), y3 - func(x3)) > > Finally, Thread applied on the whole thing interleaves values to get > > ((x1, y1-func(x1)), (x2, y2 - func(x2)), (x3, y3 - func(x3))) > >Is that basically right? Is there a way to do this in an easier way? > > > >Now... how would you make this into a generic function that took an array of >2D points and a function with one variable? I'd like to be able to call > > Residual[ points, function, ResidualType -> xsquared ] > >and return back a list of points, with their x values and their associated >vertical residuals. And I'd love to be able to expand this later on to have different >types of residuals. And more to the point it would be a great programming exercise. >I would think to be truly production worthy, it would need to be able to: > > 1) query the function, figure out if it is of the form: > > f[x_] = ... > > or is simply a variable that could use /. to substitute for values. > > 2) if it found none or more than one variable, return an exception > > 3) query the points, and see that they are in fact a two dimensional > array of points (ie: validate their data structure) > > 4) return back a list of point pairs. > >Any ideas on how to do on the above? and is there perhaps a good programming >text on mathematica that people would recommend? > I myself learned it by reading the "BOOK". It is long, but for programming you can jump over the type setting stuff. Further,for beginners there is a book from Nancy Blachman (hope I remember the name correctly) or there is the book from Roman Maeder and many others I do no more remember. > > >Ed > > > >(ps - > >>The option "Method" can have following values: >> Possible settings for Method include "ConjugateGradient", "Gradient", >>"LevenbergMarquardt", "Newton" and "QuasiNewton", with the default being >>Automatic. > > >Hmm.. I didn't notice any of this when going through the 'help' icon for >findFit. It just says: > > 'method automatic' > >with no links to point to the various methods. Perhaps the documentation >could be updated? >) > Hi, in the Help for FindFit (version 5.1) you find: "Possible settings for Method are as for FindMinimum. " > >( > pps - another thing with the online help menu - I noticed that the > items that are found by 'go' depend on which tab that is open. > Which I guess makes sense, once you know what you are doing. > > But perhaps a 'search all' button would be helpful (next to the > 'Go' button would help) - if nothing but to remind you that you > are only searching under one tab, not everything. > Sort of an "Search All" is the "Master Index" tab. > > I missed finding all the info on the statistical packages not > knowing this. And who knows what else.. >) > -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail:<mailto:dh at metrohm.ch> Internet:<http://www.metrohm.ch>

**Representation and Simulation of Dynamic Systems**

**Re: Mathematica Notebook Organiztion**

**Re: changing algorithm, finding residuals w/FindFit**

**letrec/named let**