f



FindFit and NormFunction (to find a best fit by medians)

I'm trying to use FindFit to find a best linear fit using Medians rather
than Mean

At one time, I believe the following code worked (I used it in several
places, and never noted it failing, but it hasn't been used in a while and I
don't actually remember):

FindFit[sampledata,a+b*x,{a,b},x,NormFunction->(Sqrt[Median[#^2]]&)]

Now, Mathematica seems to pass unevaluated expressions to the NormFunction,
causing Median o fail with a "rectn: Rectangular array of real numbers is
expected at position 1" error.

I would appreciate any guidance.

Michael


1
nycstern (85)
9/6/2008 6:05:25 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

2 Replies
279 Views

Similar Articles

[PageSpeed] 22

Hi,

thank you that you was so kind to post a complete working example,

sampledata =
  Table[{t, 0.2*Random[] + (2 + 0.1*Random[])*t}, {t, 0, 10, 0.25}];

myNorm[lst : {__Real}] := Sqrt[Median[lst^2]]

FindFit[sampledata, a + b*x, {a, b}, x, NormFunction -> myNorm]

will avoid the rectn-error message.

Regards
   Jens


Stern wrote:
> I'm trying to use FindFit to find a best linear fit using Medians rather
> than Mean
> 
> At one time, I believe the following code worked (I used it in several
> places, and never noted it failing, but it hasn't been used in a while and I
> don't actually remember):
> 
> FindFit[sampledata,a+b*x,{a,b},x,NormFunction->(Sqrt[Median[#^2]]&)]
> 
> Now, Mathematica seems to pass unevaluated expressions to the NormFunction,
> causing Median o fail with a "rectn: Rectangular array of real numbers is
> expected at position 1" error.
> 
> I would appreciate any guidance.
> 
> Michael
> 
> 

0
kuska (2791)
9/7/2008 9:35:42 AM
When I am trying one problem with FindFit, I found your note.

Here I have one similar question: Here I have TWO methods to constrain
the fitting process, one is the normal  least-squares method, another
is made by me. And now I want to use these two methods at the same
time.

You have given one method to use your own function, but how to write
the codes to add the normal least-squares into the program?


I have searched this  for a long time, but have not found this kind of
examples, please help me.


Many thanks

0
liqfer (3)
9/20/2008 8:55:24 AM
Reply: