COMPGROUPS.NET | Post | Groups | Users | Stream | Browse | About | |

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

• 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

Many thanks

```
 0
Reply liqfer (3) 9/20/2008 8:55:24 AM
 comp.soft-sys.math.mathematica 28833 articles. 9 followers.

2 Replies
134 Views

Similar Articles

[PageSpeed] 51

• Email
• Follow

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...

Finding Best fit % in system identification Toollbox
Hi, I have huge data(Input and Output), By using the System identification tool box i need to find transfer function constants (K,Tp1, Tz) and it Best fit % with given output. With m file command (Iddata, Pem) am able to find the K,Tp1,Tz but am not able to find the command to find the best fit %. Please help me how can i get Best fit using m file commands. Its urgently required. Please tell the m file commands Regards Kamalakar ...

how do i find where 2 best fit lines cross?
Hi I was wondering if anyone knows a way to find where 2 best fit lines cross, I don't have the equation for the lines and i'm not sure if I can get it. I currently have the program tracing the lines and comparing y values however it will only increment in steps of 0.1 on the x-axis and i need an accuraccy of 0.0001 &nbsp; hi there i suppose you have three 1D arrayswith the same number of elements: the x-values, y-values curve 1 and y-values curve 2. you can now use the Interpolate 1D Array - function to interpolate the y-values of the curves. see attachment (not tested, but it sh...

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...

Help on finding the best lin/curve fit
Hello The output of our frequency estimation methods gives a frequency function f(n), where 'n' is the discrete-time index. We know that this frequency function f(n) can be nonlinear. That is f(n) = ax^3 + bx^2 + cx + d. I am looking for line/curve fitting or any other method/algorithm to estimate the coefficients a,b,c & d given f(n). The method is required to be used with a Digital Signal Processor(DSP). Please comment. Also suggest some references/newsgroups/links/ideas etc. Awaiting, eagerly for your reply. With regards Parthasarathy On Thu, 08 Jul 2004 21:07:40 -07...

Corelating data in Matlab. Finding the best fit
Hi, I want to write a code in Matlab, that will find a best fit. I have 4 columns of values. The Red, Green and blue values and in the fourth column the temperature values. I want matlab to intelligently find me the best corelation between the RGB values and Temperature. Any pointers as to how i can go about doing this. Ofcourse the main goal is to get the best fit with minimum error. Thanks in advance. Just pasting some raw data ... Red Blue Green Temperature 17 13 28 380 15 10 28 386 25 14 40 392 17 13 38 398 9 8 23 404 3 2 7 411 7 9 19 418 10 12 21 426 3 4 13 434 0 0 6 442 4 6 19 450 6...

How to find the best fit for a list {x,y} of data
Hello everybody, I have the list and the y=f(x) equation data = {{1, 1}, {28, 0.719188377}, {54, 0.35746493}, {81, 0.182114228}, {117, 0.166082164}, {260, 0.132765531}}; express = (1 - k*x)*(1 - k*x/q)*(1 - p*k*x/q) (1 - k x) (1 - (k x)/q) (1 - (k p x)/q) "k,q and p are parameters; then I do the fitting": f1 = FindFit[data, (1 - k*x)*(1 - k*x/q)*(1 - p*k*x/q), {{k, 0.01}, {p, 1.5}, {q,1}}, x, MaxIterations -> 200] "and I obtain": {k -> 0.00586032, p -> 2.86841, q -> 2.86841} "My question is: how I can find the best fit to my d...

Finding best parameters by looking at R? obtained from fitting
Hi, I have two sets of data (five points each), which shall be denoted L and Tc(L). I need to find the limit of Tc(L) as L approaches infinity. Assuming that |Tc(Inf) - Tc(L)| is proportional to L^(-x), log |Tc(Inf) - Tc(L)| should be linearly proportional to -x log(L). I can fit a linear polynomial and look at the R? to see how good my guess of Tc(Inf) is, but is there a way to program Matlab to search for the optimal selection of Tc(Inf)? Thank you for your help. ...

finding best model: proc genmod goodness of fit
I am doing a binary analysis on contingency tables using proc genmod (SAS v.9) and have a question on fitting the model. Currently, I am using the scaled deviance (in the output) and df as a measure of goodness of fit (see below.) I choose a model which has value/df closest to 1 and the largest p-value. I add/remove variables and interaction terms until I find a model that fits the data best. Then I examine my results and conduct the interpretation. Is this method correct? Criteria For Assessing Goodness Of Fit Criterion DF ...

Cfttool can't find best fit, but I can?
I'm trying to fit a simple function f(x)=f(A,B,x) to some data using cftool, where A,B are my unknown constants, and x the independent variable. I wrote a small m-file to generate a contour plot of the goodness of fit, and found with A= 6.3096e-5, B = 1.2589e-12 I get R^2 = 0.96665 ---> the highest R^2. If I give cftool the A,B I found above, it returns them to me. However, if I give something close, it doesn't converge to the closer value. For example, if I give cftool A=6e-5, B=1e-12, it returns them to me with R^2 = 0.9588 ------> How do I get cftool to converge to...

Hacker challenge II
Here is a problem that comes now and then in real life. Suppose we have an array that has gaps, i.e., there are entries for some indices and not for others. Let A be the array. The array can be empty, sparsely filled, half filled, almost completely full, or completely full. What we need to be able to do is, given an index i, find the smallest index j >= i, such A[j] has an entry, if there be such. Obviously we can do a linear search starting with i, but this could be an O(n) search if the array is sparse. What is needed is some kind of data structure and algorithm that is space and ti...

Best fitness vs mean fitness, initial range
1) Based on the example of Rastrigin's function. At the plot function, if I chose 'best fitness', on the same graph 'mean fitness' will also be plotted. I understand well about 'best fitness' whereby it plots the best function value in each generation versus iteration number. It will reach value zero after some times. I don't understand about 'mean fitness'in the graph plotted. What do those 'mean fitness' values mean? How does the 'mean fitness' graph help to understand Rastrigin's function? 2) What are the meaning of the ...

Best of the best,of the best, with honors.
http://www.displaymate.com/iPad_ShootOut_1.htm And now watch the WinTrolls blow a gasket in 3, 2, 1..... Hehe On Tuesday, March 20, 2012 12:57:54 PM UTC-4, jdear wrote: > http://www.displaymate.com/iPad_ShootOut_1.htm > > And now watch the WinTrolls blow a gasket in 3, 2, 1..... > > > Hehe 3.5" screen and 9" screen. Hehehe is right. Goes to watch a 3D movie on 70" Pioneer Kuro. ...

best fitting
Hi, I have some experimental data in the form of quadruplets {P, W, t, D}. How can I find the best fit parameters: alfa, A and E1 of the function: P = { (W - K^2/3) / t + A [(W - K^2/3) / t]^3} E1 (t^4) / (25^2) where K = P / (alfa D^0.5). Thank you -u Hi, look at: P = { (W - K^2/3) / t + A [(W - K^2/3) / t]^3} E1 (t^4) / (25^2) K = P / (alfa D^0.5) this is a recursive definition. Is this what you want???? hope this helps, Daniel umby wrote: > Hi, > > > > I have some experimental data in the fo...

Best Find?
I took my monthly trek around the local flea markets this weekend and scored a mint condition 64C and 1541C, along with about 20 original disks (5 in box). All for \$10. As the guy said, "It's all outdated stuff and I can't do anything with it". So I'm wondering what your best find is? I know you can get everything on e-bay, but that takes the fun out of it. On Wed, 18 Jan 2006, Lige Hensley wrote: > So I'm wondering what your best find is? Gathering up the collection of a former C= user and finding a SuperCPU 64 in it. Truly, Robert Bernardo ...

FindFit and NormFunction
I am having difficulty using the NormFunction option to FindFit. Let me give a concrete example. Abramowitz and Stegun Section 17.3.35 gives a nonlinear approximant to the complete elliptic integral. See www.convertit.com/Go/ConvertIt/Reference/AMS55.ASP?Res=150&Page=592 This approximant to EllipticE[1-m] can be implemented as f[a_, b_, c_, d_][m_] = a m + b m^2 - (c m + d m^2) Log[m] + 1; After sampling EllipticE[1-m], data = N[Table[{m, EllipticE[1-m]}, {m, 10^-8, 1 - 10^-8, 10^-3}]]; using FindFit gives quite a decent approximant: FindFit[data, f[a, b, c, d...

Curve-Fitting Techniques, New Technique to Find Goodness of fit
Dear Mathematicians, Statisticians and Scientists Everywhere, After 6 months of intensive work, I put between your hands a new project of research about the Relative Goodness-of-fit. This project addresses an important problem of how to choose the best fit from amongst a lot of available models in hand. The outcome is =CE=B7-Index which is a score ranging between 100 for the exact fit and 0 for the trivial fit. The =CE=B7-algorithm uses pairwise comparisons of the shifted logarithms of absolute residuals. It does not depend on dealing with the squared residuals, thereby it will not be biase...