Re: Description of the function

• To: mathgroup at smc.vnet.net
• Subject: [mg13754] Re: Description of the function
• From: Paul Abbott <paul at physics.uwa.edu.au>
• Date: Mon, 24 Aug 1998 05:07:06 -0400
• Organization: University of Western Australia
• References: <6rdrdq\$tl@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Toshiyuki Meshii wrote:

> I want to make a least square fit of some data set A{x1, x2, f}. The
> function to be fitted is
>
> f[x1_, x2_, a_, b_] = a*x1 + b*x2^2
>
> When I define a error function
> erf[a_,b_,c_,d_]= Sum[ ( f[ A[[j]][[1]], A[[j]][[2]],a,b]-A[[j]] )^2,
> {j,6} ]

This is not a good idea, in general, since the number of terms will not
be fixed.

> How can I obtain function erf in a direct form?

The simplest way is to load

In[1]:= <<Statistics`NonlinearFit`

Then the fit and error can be obtained immediately:

In[2]:= NonlinearFit[data,f[x1,x2,a,b],{x1,x2},{a,b},
ShowProgress->True]

Appended below is a Notebook with some more specific comments.

Cheers,
Paul

____________________________________________________________________
Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia            Nedlands WA  6907
mailto:paul at physics.uwa.edu.au  AUSTRALIA
http://www.physics.uwa.edu.au/~paul

God IS a weakly left-handed dice player
____________________________________________________________________

Notebook[{

Cell[CellGroupData[{
Cell["Least-squares Fit", "Section"],

Cell[TextData[{
"I want to make a least square fit of some data set, ",
StyleBox["e.g.",
FontSlant->"Italic"],
","
}], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\`data = Flatten[
Table[{x, y, 15.3  x + 3.2  y\^2 + Random[Real, {\(-0.1\),
0.1}]}, {
x, 0, 4}, {y, 0, 5}], 1]\)], "Input"],

Cell[BoxData[
FormBox[
RowBox[{"(", GridBox[{
{"0", "0", "0.0880062597881452468`"},
{"0", "1", "3.18294258055859957`"},
{"0", "2", "12.7747345223256325`"},
{"0", "3", "28.7048295747130701`"},
{"0", "4", "51.275974732293541`"},
{"0", "5", "79.9306644406819533`"},
{"1", "0", "15.3506154624638053`"},
{"1", "1", "18.4079100655106451`"},
{"1", "2", "28.0880050807756873`"},
{"1", "3", "44.1772500913196175`"},
{"1", "4", "66.4320848578615841`"},
{"1", "5", "95.2302736133987437`"},
{"2", "0", "30.5201033295162904`"},
{"2", "1", "33.7100417945716079`"},
{"2", "2", "43.3005355479892806`"},
{"2", "3", "59.4915452159901114`"},
{"2", "4", "81.717016825425599`"},
{"2", "5", "110.636494402211771`"},
{"3", "0", "45.8368889767999299`"},
{"3", "1", "49.1608124168589277`"},
{"3", "2", "58.6593144675969746`"},
{"3", "3", "74.6566376651778096`"},
{"3", "4", "97.0760053501765263`"},
{"3", "5", "125.808605428462594`"},
{"4", "0", "61.1713082078088365`"},
{"4", "1", "64.4736950846192069`"},
{"4", "2", "73.9012708278508956`"},
{"4", "3", "89.9037758537495257`"},
{"4", "4", "112.395333475515313`"},
{"4", "5", "141.243030643937235`"}
},
"Output"]
}, Closed]],

Cell["The function to be fitted is", "Text"],

Cell[BoxData[
\(TraditionalForm\`\(f(x1_, x2_, a_:  a, b_:  b) = b\ x2\^2 + a\ x1;

\)\)], "Input"],

Cell[BoxData[

Cell[TextData[{
"the fit and ",
Cell[BoxData[
" can be obtained immediately:"
}], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\`NonlinearFit[data, f[x1, x2, a, b], {x1, x2}, {a, b},
ShowProgress \[Rule] True]\)], "Input"],

Cell[BoxData[
FormBox[
InterpretationBox[
\\\"1.\\\"}\\)\\)\"\>",
StringForm[ "Iteration:`1`  ChiSquared:`2`  Parameters:`3`", 1,
NumberForm[ 95028.33338408306, 6],
NumberForm[ {1.0, 1.0}, 6]],

Cell[BoxData[
FormBox[
InterpretationBox[
\\\"3.19839\\\"}\\)\\)\"\>",
StringForm[ "Iteration:`1`  ChiSquared:`2`  Parameters:`3`", 2,
NumberForm[ 0.16646547033493583, 6],
NumberForm[ {15.281664747550501, 3.1983921972331486}, 6]],

Cell[BoxData[
FormBox[
InterpretationBox[
\\\"3.1993\\\"}\\)\\)\
\"\>",
StringForm[ "Iteration:`1`  ChiSquared:`2`  Parameters:`3`", 3,
NumberForm[ 0.12712406008027677, 6],
NumberForm[ {15.293182518049207, 3.1992963674966468}, 6]],

Cell[BoxData[
FormBox[
InterpretationBox[
\\\"3.1993\\\"}\\)\\)\
\"\>",
StringForm[ "Iteration:`1`  ChiSquared:`2`  Parameters:`3`", 4,
NumberForm[ 0.12712405983015276, 6],
NumberForm[ {15.293183851253794, 3.1992963081159984}, 6]],

Cell[BoxData[
FormBox[
InterpretationBox[
\\\"3.1993\\\"}\\)\\)\
\"\>",
StringForm[ "Iteration:`1`  ChiSquared:`2`  Parameters:`3`", 5,
NumberForm[ 0.12712405983015204, 6],
NumberForm[ {15.293183851265326, 3.1992963081144059}, 6]],

Cell[BoxData[
\`3.1992963081144059`\ x2\^2 + 15.2931838512653257`\ x1\)],
"Output"]
}, Open  ]],

Cell["Alternatively, define an error function:", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\`error[x1_, \ x2_, \ x3_, a_:  a, \ b_:  b]\  = \
\((f[x1, x2, a, b] - x3)\)\^2\)], "Input"],

Cell[BoxData[
\(TraditionalForm\`\((b\ x2\^2 + a\ x1 - x3)\)\^2\)], "Output"] },
Open  ]],

Cell[TextData[{
"The total error (or ",
Cell[BoxData[
") is then"
}], "Text"],

Cell[BoxData[
\(TraditionalForm\`\(\[Chi] /: \[Chi]\^2 = Plus@@Apply[error, data,
1];
\)\)], "Input"],

Cell[TextData[{
"For example, with the parameters ",
Cell[BoxData[
" and ",
Cell[BoxData[
","
}], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\(TraditionalForm\`\[Chi]\^2 /. {a \[Rule] 1, b \[Rule] 1}\)],
"Input"],

Cell[BoxData[

Cell[TextData[{
"in agreement with ",
Cell[BoxData[
FormBox[
StyleBox["NonlinearFit",
"."
}], "Text"],

Cell["The parameters can be determined numerically:", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\`NSolve[{\[PartialD]\_a \[Chi]\^2 == 0, \[PartialD]\_b \[Chi]\^2 ==
0}, {
a, b}]\)], "Input"],

Cell[BoxData[
\`{{a \[Rule] 15.2931838512768543`, b \[Rule]
3.1992963081128134`}}\)],
"Output"]
}, Open  ]],

Cell[TextData[{
"More generally, one can use ",
Cell[BoxData[
FormBox[
StyleBox["FindRoot",
":"
}], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\`FindRoot[
Evaluate[{\[PartialD]\_a \[Chi]\^2 == 0,
\[PartialD]\_b \[Chi]\^2 == 0}], {a, 1}, {b, 1}]\)], "Input"],

Cell[BoxData[
\`{a \[Rule] 15.2931838512768614`, b \[Rule]
3.19929630811281384`}\)],
"Output"]
}, Open  ]]
}, Open  ]]
}
]

```

• Prev by Date: Re: Question for using complex variables