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

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply nycstern (85) 9/6/2008 6:05:25 AM

See related articles to this posting

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
Reply 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
Reply liqfer (3) 9/20/2008 8:55:24 AM
comp.soft-sys.math.mathematica 28727 articles. 7 followers.

2 Replies
70 Views

Similar Articles

[PageSpeed] 37

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Re: FindFit and NormFunction (to find a best fit by medians)
On 9/6/08 at 2:05 AM, nycstern@gmail.com (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]]&)] It is unclear to me what you mean by fitting using means or medians. I am going to assume you mean by using means you mean using FindFit with the default norm...

Find best fit of number
Hello, can someone help me with this problem: I have a numeric-double-control as input. Then i have a numeric-double-array-constant with ~10 numbers. The output should be one of the constant-numbers which is the nearest to the input-number. For example: input: 2.7 constant-array: 0.1 / 0.7 /2.1 / 3 / 5.5 / 9 The the ouput should be: 3 Thanks for your help Hello Aitortxo, I already did it like you said but i thought there woul be an easier/faster way to do it than searching an array. findbestfit.vi: http://forums.ni.com/attachments/ni/170/153509/1/findbestfit.vi What about this one? ...

Finding best fit for surf
Hi, I am trying to formulate an equation based on some measured data. There are two variables, lets call them x and y and a resulting value z. I have test data for an array of points from which I have compiled a 2-d array using interp2. I can plot this using surf but the "basic fitting" is not available. I would like to have an equation z=f(x,y) for the data. Does anyone know how I can produce this equation in Matlab? Much appreciated Tim ...

Finding Best fit coefficients
Lets say Y = F(x) where F = (A/(B-Cx)^2 ) -D and A,B,C,D are all constants. I have a column of data for Y, and a column of data for x and all I know is that A<D. What do I do to find the coefficients A,B,C,D such that the function F has the best fit with the data? Thanks, Arvindh "Arvindh " <arvindh.rao@gmail.com> wrote in message <hrqise$ht7$1@fred.mathworks.com>... > Lets say Y = F(x) where F = (A/(B-Cx)^2 ) -D and A,B,C,D are all constants. > > I have a column of data for Y, and a column of data for x and all I know is that A<D. &g...

Find the function that best fits
Dear collages, I have implemented a code to obtain information. Now I want to obtain a function that fits to this information. Is there any option to say to Mathematica to look for the best function that fit to dates? Thank you very much. i think you need to be allot more specific about what you have and what your looking for. stated well, and if a limited question, there are many very capable math group participators i bet will help answer there are many new date functions. fit date? ...

Re: Find the function that best fits
On 4/20/13 at 5:45 AM, JFONFRIA@UJAEN.ES (JFONFRIA) wrote: >I have implemented a code to obtain information. Now I want to >obtain a function that fits to this information. Is there any option >to say to Mathematica to look for the best function that fit to >dates? No. The problem you pose is ill defined and open ended. Key is defining what is meant by "best". Mathematica provides a variety of tools suitable for fitting functions to data. It is up to you to use them appropriately. And only you can define "appropriate", "best" etc for your ...

best fit sphere and finding its center
hi, i have (X,Y,Z) coordinates of a GPS receiver mounted on a telescope.which apparently forms half of a sphere.as GPS data are not 100% precise thus i cant get accurate surface of the half sphere. therefore i need to fit the points on the sphere. how do i get the best fit sphere and center of it. regards taaj "syed " <taaj_1079@hotmail.com> wrote in message <i148fd$lqv$1@fred.mathworks.com>... > hi, > i have (X,Y,Z) coordinates of a GPS receiver mounted on a telescope.which apparently forms half of a sphere.as GPS data are not 100% precise thus i cant get ...

How to find the best fit for a data with multivariables
Hi I have a matrix of size 800 x 5 containing the data of the five independent variables a,b,c,d,e and vector 800 x 1 containing the data for the dependent variable z. I want to fit a model for the data giving the functional relation between dependent variable z and five independent variables a,b,c,d,e. My questions are: 1. Which functions/tools in MATLAB should I used to get the best fit? 2. Which MATLAB tools/functions should I used to get an expression of the fitted model and also validate the fitted model? Best Regards, Rabi "Rabi " <rabikhattak@gmail.c...

Find the best fit with an exponential curve (or spherical)
Hello, Given vectors of data xdata and ydata, I want to find coefficients to find the best fit to some kind of exponential decay equation. Please have a look at my data. I posted it as two vectors at the end of this message, which you can copy and paste into MATLAB. The ydata vs. xdata plot clearly shows a exponential decay. I used the command lsqcurvefit with the function F(p,xdata) = p(1)*exp(p(2)*(xdata+p(3))) However it fails and I don't know what is wrong woth it. It might be the function or any settings. Its the first time that I use this kind of approach. I only u...