Re: Fit[] problem

*To*: mathgroup at christensen.cybernetics.net*Subject*: [mg1819] Re: Fit[] problem*From*: rknapp (Robert Knapp)*Date*: Thu, 3 Aug 1995 23:51:23 -0400*Organization*: The Santa Fe Institute

In article <DCCur0.oE at wri.com> glagorio at vol.it (Giovanni Lagorio) writes: > Newsgroups: comp.soft-sys.math.mathematica > Path: SantaFe!tesuque.cs.sandia.gov!ferrari.mst6.lanl.gov!newshost.lanl.gov!ncar!gatech!howland.reston.ans.net!vixen.cso.uiuc.edu!wri!news > From: glagorio at vol.it (Giovanni Lagorio) > Sender: news at wri.com ( ) > Nntp-Posting-Host: christensen.cybernetics.net > Organization: Steven M. Christensen and Associates, Inc. and MathSolutions, Inc. > Date: Thu, 27 Jul 1995 03:23:23 GMT > Approved: Steven M. Christensen <steve at christensen.cybernetics.net>, Moderator > Lines: 25 > > I have a problem with Fit[] (I'm using Mathematica for Student 2.2.1 Windows). > Here is the session: > > in : ( data = Table[ Sum[i,{i,1,n}] , {n,0,100} ] ) //Short > out: {0, 1, 3, 6, 10, 15, 21, 28, 36, <<88>>, 4753, 4851, 4950, 5050} > in : Chop[ Fit[ data , {1,x,x^2} , x ] ] > 2 > out: -0.5 x + 0.5 x > ^^^^^^^^^^^^^^^ and this is ok ... but ... > > in : Chop[ Fit[ data , {1,x,x^2,E^x} , x ] ] > out: 0 > ^^^ Why ?!?!? > > I don't understand why the result change adding an useless function... > someone knows ? > The reason is that you are running into problems with numerical error. As stated in the reference guide for Fit, exact numbers are converted to approximate numbers with machine precision. I expect you have included the Chop command because you wanted to eliminate any small coefficients from the fit. This is fine in most cases, buit is not always such a good idea. Take a look at the fit of your data without the Chop: In[1]:= data = Table[ Sum[i,{i,1,n}] , {n,0,100} ]; In[2]:= Fit[ data , {1,x,x^2,E^x} , x ] Out[2]= -41 x -79 2 0. + 9.34527 10 E + 0. x + 2.81784 10 x Indeed the coefficients are small, but consider that In[3]:= Exp[101.]*9.34527305982043*10^-41 Out[3]= 6828.65 so the coefficient of Exp[x] is not that small compared to the size of the function. What has happened here is that Exp[x] has a range which is enormous, and due to roundoff errors inherent in computations with machine numbers, the exponential function dominated the fit. So what can be done? All that is necessary is to use higher precision: In[4]:= data1 = Table[ {n+1,Sum[i,{i,1,n}]} , {n,0,100} ]; In[5]:= fit1 = Fit[ N[data1,100] , {1,x,x^2,E^x} , x ] Out[5]= -65 7.135457881555409033227538870223 10 - -106 x 3.52452900384557720836640870298644 10 E - 0.5000000000000000000000000000000000000000000000000\ 0000000000000000652299740606555602512805088522 x \ + 0.5000000000000000000000000000000000000000000000\ 0000000000000000000002398583700122984574020274592\ 2 677 x While 100 digits is proabably well in excess of what is necessary, for a small problem like this it doesn't hurt to be really sure! Notice the number of digits in error in the coefficients for x and x^2 exceeds the number of digits of machine precision (16 on my machine). In[6]:= Chop[N[fit1]] Out[6]= 2 -0.5 x + 0.5 x Now that the higher precision has been used to find the result, it is probably unneccessary for further computations, so it is reasonable to use N to convert to machine numbers and Chop to eliminate the small coefficients. Hope this clears up the waters. Rob Knapp WRI