MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Q:CurveFitting

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21945] Re: Q:CurveFitting
  • From: "Kevin J. McCann" <kevin.mccann at jhuapl.edu>
  • Date: Sat, 5 Feb 2000 00:49:19 -0500 (EST)
  • Organization: Johns Hopkins University Applied Physics Lab, Laurel, MD, USA
  • References: <87e1tl$s2g@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Another job for a least squares fit.  I have attached a NB at the bottom.

Hope this helps you.

Kevin

--

Kevin J. McCann
Johns Hopkins University APL

FRESSENGEAS Nicolas <nicolas.fressengeas at supelec.fr> wrote in message
news:87e1tl$s2g at smc.vnet.net...
> Hello out there
>
> I am looking for any ideas to fit data to :
> Exp[1-Exp[-t/tau]] : the fit variable is tau
>
> Thanks in advance
>

(***********************************************************************

                    Mathematica-Compatible Notebook

This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook
starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do one of
the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the
word CacheID, otherwise Mathematica-compatible applications may try to
use invalid cache data.

For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info at wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)

(*CacheID: 232*)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[      5254,        180]*)
(*NotebookOutlinePosition[      6013,        206]*)
(*  CellTagsIndexPosition[      5969,        202]*)
(*WindowFrame->Normal*)



Notebook[{

Cell[CellGroupData[{
Cell["LSQ Fit", "Title"],

Cell[CellGroupData[{

Cell["Original Message", "SectionFirst"],

Cell[BoxData[{
    \(Hello\ out\ there\), "\n",
    \(I\ am\ looking\ for\ any\ ideas\ to\ fit\ data\ \(to : \(\(\
\[ExponentialE]\^\(1 - \[ExponentialE]\^\(-\(t\/tau\)\)\)\) :
          the\ fit\ variable\ is\ tau\)\)\)}], "Text"]
}, Open  ]],

Cell[CellGroupData[{

Cell["LSQ", "SectionFirst"],

Cell["\<\
There are lots of routines out there for doing this, but it is so simple to
\
do that this is how I almost always do it. The same technique will work with
\
a multivariate fit. The only real problem is that the FindMinimum below \
requires an initial guess.

Your function. We wish to determine the \[Tau] that best fits the data\
\>", "Text"],

Cell[BoxData[
    \(f[t_, \[Tau]_] := \[ExponentialE]\^\(1 - \
\[ExponentialE]\^\(-\(t\/\[Tau]\)\)\)\)], "Input",
  CellLabel->"In[7]:="],

Cell[CellGroupData[{

Cell["Some \"data\"", "Subsection"],

Cell["\<\
Generate a random list of x-values -  maybe your data is not equispaced\
\>", "Text"],

Cell[BoxData[
    \(\(xlist = Table[Random[Real, {0, 5}], {100}];\)\)], "Input",
  CellLabel->"In[3]:="],

Cell["\<\
and the corresponding data list with \[Tau]=3.14 and some noise added\
\>", "Text"],

Cell[BoxData[
    \(\(dataList = \(({#,
                f[#, 3.14] + Random[Real, {\(- .5\),  .5}]} &)\)\  /@ \
          xlist;\)\)], "Input",
  CellLabel->"In[8]:="],

Cell["Plot the data", "Text"],

Cell[BoxData[
    \(\(pdata =
        ListPlot[dataList,
          PlotStyle \[Rule] {RGBColor[0, 0, 1],
              AbsolutePointSize[5]}];\)\)], "Input",
  CellLabel->"In[10]:="]
}, Open  ]],

Cell[CellGroupData[{

Cell["LSQ fit Module", "Subsection"],

Cell["\<\
Here is a Module to do a least-squares sum for a given \[Tau]\
\>", "Text"],

Cell[BoxData[
    \(lsFit[\[Tau]_] :=
      Module[{}, \[IndentingNewLine]Plus\  @@ \
          Apply[\((f[#1, \[Tau]]\  - \ #2)\)\^2 &,
            dataList, {1}]\[IndentingNewLine]]\)], "Input",
  CellLabel->"In[11]:="],

Cell["\<\
What the module does is to take the first argument in each {t,data} pair, \
compute f(t,\[Tau]), subtract the data value from it and square.  Then sum \
over all the data pairs. A least-squares fit looks for the \[Tau] that \
minimizes this.\
\>", "Text"],

Cell["\<\
Let's plot this for a range of \[Tau]:\
\>", "Text"],

Cell[BoxData[
    \(Plot[lsFit[\[Tau]], {\[Tau],  .001, 10},
      PlotStyle \[Rule] RGBColor[1, 0, 0], Frame \[Rule] True,
      FrameLabel \[Rule] {"\<\[Tau]\>", "\<LSQ(\[Tau])\>"},
      GridLines \[Rule] Automatic]; \)], "Input",
  CellLabel->"In[16]:="],

Cell["\<\
The is a clear minimum. Just use FindMinimum to get it. Initial guess is 3 \
based on the plot\
\>", "Text"],

Cell[BoxData[
    \(ans = FindMinimum[lsFit[\[Tau]], {\[Tau], 3}]\)], "Input",
  CellLabel->"In[18]:="],

Cell[BoxData[
    \(\[Tau]0 = \[Tau] /. ans[\([2]\)]\)], "Input",
  CellLabel->"In[19]:="],

Cell["\<\
Not too bad, since the real \[Tau] = 3.14.\
\>", "Text"],

Cell["\<\
Now, plot the function with this \[Tau] and compare with the data. Also note
\
that ans[[1]]=8.38 is the measure of how close the fit is. Here it is not so
\
close because of the large noise.\
\>", "Text"],

Cell[BoxData[
    \(\(Plot[f[t, \[Tau]0], {t, 0, 5},
        PlotStyle \[Rule] {AbsoluteThickness[3], RGBColor[1, 0, 0]},
        Epilog \[Rule] pdata[\([1]\)],
        PlotRange \[Rule] {0.5, 2.5}];\)\)], "Input",
  CellLabel->"In[23]:="]
}, Open  ]]
}, Open  ]]
}, Open  ]]
},
FrontEndVersion->"4.0 for Microsoft Windows",
ScreenRectangle->{{0, 1152}, {0, 791}},
WindowToolbars->{"RulerBar", "EditBar"},
WindowSize->{496, 695},
WindowMargins->{{18, Automatic}, {Automatic, 23}},
ShowCellLabel->False,
StyleDefinitions -> "ArticleClassic.nb"
]


(***********************************************************************
Cached data follows.  If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of
the file.  The cache data will then be recreated when you save this file
from within Mathematica.
***********************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{

Cell[CellGroupData[{
Cell[1739, 51, 24, 0, 72, "Title"],

Cell[CellGroupData[{
Cell[1788, 55, 40, 0, 62, "SectionFirst"],
Cell[1831, 57, 233, 4, 56, "Text"]
}, Open  ]],

Cell[CellGroupData[{
Cell[2101, 66, 27, 0, 62, "SectionFirst"],
Cell[2131, 68, 353, 7, 95, "Text"],
Cell[2487, 77, 137, 3, 43, "Input"],

Cell[CellGroupData[{
Cell[2649, 84, 35, 0, 43, "Subsection"],
Cell[2687, 86, 95, 2, 27, "Text"],
Cell[2785, 90, 104, 2, 30, "Input"],
Cell[2892, 94, 93, 2, 27, "Text"],
Cell[2988, 98, 169, 4, 30, "Input"],
Cell[3160, 104, 29, 0, 27, "Text"],
Cell[3192, 106, 185, 5, 46, "Input"]
}, Open  ]],

Cell[CellGroupData[{
Cell[3414, 116, 36, 0, 43, "Subsection"],
Cell[3453, 118, 85, 2, 27, "Text"],
Cell[3541, 122, 224, 5, 65, "Input"],
Cell[3768, 129, 265, 5, 61, "Text"],
Cell[4036, 136, 62, 2, 27, "Text"],
Cell[4101, 140, 261, 5, 62, "Input"],
Cell[4365, 147, 118, 3, 44, "Text"],
Cell[4486, 152, 103, 2, 30, "Input"],
Cell[4592, 156, 90, 2, 30, "Input"],
Cell[4685, 160, 66, 2, 27, "Text"],
Cell[4754, 164, 215, 4, 61, "Text"],
Cell[4972, 170, 242, 5, 62, "Input"]
}, Open  ]]
}, Open  ]]
}, Open  ]]
}
]
*)




(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)


  • Prev by Date: Re: Little(?) problem: What is the formula ???
  • Next by Date: Re: Mathematica on the Web
  • Previous by thread: Re: Q:CurveFitting
  • Next by thread: Little(?) problem: What is the formula ???