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]] )
}
]
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
'-', given two arrays, applies that operation to each element of that
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?
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?
)
(
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.
I missed finding all the info on the statistical packages not
knowing this. And who knows what else..
)