Using NDSolve in NonlinearFit
- To: mathgroup at smc.vnet.net
- Subject: [mg48771] Using NDSolve in NonlinearFit
- From: "Dr. Ernst H.K. Stelzer" <stelzer at embl-heidelberg.de>
- Date: Wed, 16 Jun 2004 04:54:17 -0400 (EDT)
- Reply-to: <stelzer at embl-heidelberg.de>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
I have been trying to use NDSolve inside various Fitting routines
(NonlinearFit, FindFit, ...). Although the NDSolve works fine both inside
and outside a Module wrapper once inside a "fit"-function the variables are
never resolved. At least the Print inside the function returns names rather
than numbers??
What is my mistake?
Has anybody solved this problem differently?
I do not wish to write a new Fit-function and search myslef if it can be
avoided. The text below is copied as CellFormat and can be inserted and
interpreted in any Mathematica Notebook.
Sincerely
Ernst
--------------------------------------------------------------------
Dr. Ernst H.K. Stelzer Tel: +49 (6221) 387354
Scientific Groupleader Fax: +49 (6221) 387306
Cell Biology and Biophysics Programme
EMBL
European Molecular Biology Laboratory
Meyerhofstrasse 1 Postfach 10 22 09
69117 Heidelberg 69012 Heidelberg
e-mail: mailto:stelzer at embl-heidelberg.de
http://www.embl.de/ExternalInfo/stelzer/index.html
http://www.embl.de/ExternalInfo/stelzer/frames.html?publications
--------------------------------------------------------------------
{
Cell[BoxData[
RowBox[{
RowBox[{"models", " ", "=", " ",
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"n0", "'"}], "[", "c", "]"}], "\[Equal]",
RowBox[{
RowBox[{"-", "a00a"}],
RowBox[{"(", " ",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], "-", "b0"}],
")"}]}]}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"n0a", "'"}], "[", "c", "]"}], "==",
RowBox[{
RowBox[{"a00a",
RowBox[{"(", " ",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], "-", "b0"}],
")"}]}], "-",
RowBox[{"a01", " ",
RowBox[{"n0a", "[", "c", "]"}]}]}]}], ",", " ",
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"n1", "'"}], "[", "c", "]"}], "\[Equal]",
RowBox[{
RowBox[{"a01", " ",
RowBox[{"n0a", "[", "c", "]"}]}], "-",
RowBox[{"a11", " ",
RowBox[{"n1", "[", "c", "]"}]}]}]}], ",",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"n0", "[", "0", "]"}], "\[Equal]", "n00"}], ",",
RowBox[{
RowBox[{"n0a", "[", "0", "]"}], "\[Equal]", "n0a0"}],
",",
RowBox[{
RowBox[{"n1", "[", "0", "]"}], "==", "n10"}]}],
"\[IndentingNewLine]", "}"}]}], ";"}]], "Input",
CellLabel->"In[9]:="],
Cell[BoxData[
RowBox[{
RowBox[{"parms", " ", "=", " ",
RowBox[{"{",
RowBox[{
RowBox[{"a00a", " ", "\[RuleDelayed]", " ", "1"}], ",",
" ",
RowBox[{"b0", " ", "\[RuleDelayed]", " ", "1"}], ",", " ",
RowBox[{"a01", " ", "\[RuleDelayed]", " ", "1"}], ",",
" ",
RowBox[{"a11", " ", "\[RuleDelayed]", " ", "1"}], ",",
" ",
RowBox[{"n00", " ", "\[RuleDelayed]", " ", "10"}], ",",
" ",
RowBox[{"n0a0", " ", "\[RuleDelayed]", " ", "5"}], ",",
" ",
RowBox[{"n10", " ", "\[RuleDelayed]", " ", "3"}]}],
"}"}]}], ";"}]], "Input",
CellLabel->"In[10]:="],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"sol1", "=",
RowBox[{"NDSolve", "[",
RowBox[{
RowBox[{"Evaluate", "[",
RowBox[{"models", "/.", "parms"}], "]"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], ",",
RowBox[{"n0a", "[", "c", "]"}], ",",
RowBox[{"n1", "[", "c", "]"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"c", ",", " ", "0", ",", " ", "10"}], "}"}]}],
"]"}]}]], "Input",
CellLabel->"In[11]:="],
Cell[BoxData[
RowBox[{"{",
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{"n0", "[", "c", "]"}], "\[Rule]",
RowBox[{
TagBox[
RowBox[{"InterpolatingFunction", "[",
RowBox[{
RowBox[{"{",
RowBox[{"{",
RowBox[{"0.`", ",", "10.`"}], "}"}], "}"}],
",", "\<\"<>\"\>"}], "]"}],
False,
Editable->False], "[", "c", "]"}]}], ",",
RowBox[{
RowBox[{"n0a", "[", "c", "]"}], "\[Rule]",
RowBox[{
TagBox[
RowBox[{"InterpolatingFunction", "[",
RowBox[{
RowBox[{"{",
RowBox[{"{",
RowBox[{"0.`", ",", "10.`"}], "}"}], "}"}],
",", "\<\"<>\"\>"}], "]"}],
False,
Editable->False], "[", "c", "]"}]}], ",",
RowBox[{
RowBox[{"n1", "[", "c", "]"}], "\[Rule]",
RowBox[{
TagBox[
RowBox[{"InterpolatingFunction", "[",
RowBox[{
RowBox[{"{",
RowBox[{"{",
RowBox[{"0.`", ",", "10.`"}], "}"}], "}"}],
",", "\<\"<>\"\>"}], "]"}],
False,
Editable->False], "[", "c", "]"}]}]}], "}"}],
"}"}]], "Output",
CellLabel->"Out[11]="]
}, Open ]],
Cell[BoxData[
RowBox[{
RowBox[{"Plot", "[",
RowBox[{
RowBox[{"Evaluate", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], ",", " ",
RowBox[{"n0a", "[", "c", "]"}], ",", " ",
RowBox[{"n1", "[", "c", "]"}]}], "}"}], "/.",
"sol1"}], "]"}], ",", " ",
RowBox[{"{",
RowBox[{"c", ",", " ", "0", ",", " ", "10"}], "}"}], ",",
" ",
RowBox[{"AxesLabel", "\[Rule]",
RowBox[{"{",
RowBox[{"\"\<[c]\>\"", ",", " ", "\"\<[#]\>\""}],
"}"}]}]}], "]"}], ";"}]], "Input",
CellLabel->"In[12]:="],
Cell[BoxData[
RowBox[{
RowBox[{"data", " ", "=", " ",
RowBox[{"Table", "[",
RowBox[{
RowBox[{"{",
RowBox[{"c", ",", " ",
RowBox[{"First", "[",
RowBox[{
RowBox[{"Evaluate", "[",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], "/.", "sol1"}],
"]"}], " ", "+", " ",
RowBox[{"Random", "[",
RowBox[{"Real", ",",
RowBox[{"{",
RowBox[{
RowBox[{"-", "1"}], ",", " ", "1"}],
"}"}]}], "]"}]}], "]"}]}], "}"}], ",", " ",
RowBox[{"{",
RowBox[{
"c", ",", " ", "0", ",", " ", "10", ",", " ", "0.2"}],
"}"}]}], "]"}]}], ";"}]], "Input",
CellLabel->"In[13]:="],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"ListPlot", "[", "data", "]"}]], "Input",
CellLabel->"In[14]:="],
Cell[BoxData[
TagBox[
RowBox[{
"\[SkeletonIndicator]", "Graphics", "\[SkeletonIndicator]"}],
False,
Editable->False]], "Output",
CellLabel->"Out[14]="]
}, Open ]],
Cell[BoxData[
RowBox[{"<<", "Statistics`NonlinearFit`"}]], "Input",
CellLabel->"In[3]:="],
Cell[BoxData[
RowBox[{
RowBox[{"vars", " ", "=", " ",
RowBox[{"{",
RowBox[{
RowBox[{"a00a", " ", "\[Rule]", " ", "x1"}], ",", " ",
RowBox[{"b0", " ", "\[Rule]", " ", "x2"}], ",", " ",
RowBox[{"a01", " ", "\[Rule]", " ", "x3"}], ",", " ",
RowBox[{"n00", " ", "\[Rule]", " ", "x5"}]}], "}"}]}],
";"}]], "Input",
CellLabel->"In[15]:="],
Cell[BoxData[
RowBox[{
RowBox[{"values", " ", "=", " ",
RowBox[{"{",
RowBox[{
RowBox[{"a00a", " ", "\[RuleDelayed]", " ", "1"}], ",",
" ",
RowBox[{"b0", " ", "\[RuleDelayed]", " ", "1"}], ",", " ",
RowBox[{"a01", " ", "\[RuleDelayed]", " ", "1"}], ",",
" ",
RowBox[{"a11", " ", "\[RuleDelayed]", " ", "1"}], ",",
" ",
RowBox[{"n00", " ", "\[RuleDelayed]", " ", "10"}], ",",
" ",
RowBox[{"n0a0", " ", "\[RuleDelayed]", " ", "5"}], ",",
" ",
RowBox[{"n10", " ", "\[RuleDelayed]", " ", "3"}]}],
"}"}]}], ";"}]], "Input",
CellLabel->"In[16]:="],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"mdls", " ", "=", " ",
RowBox[{
RowBox[{"models", "/.", "vars"}], "/.",
"values"}]}]], "Input",
CellLabel->"In[17]:="],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{
SuperscriptBox["n0", "\[Prime]",
MultilineFunction->None], "[", "c", "]"}], "\[Equal]",
RowBox[{
RowBox[{"-", "x1"}], " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "x2"}], "+",
RowBox[{"n0", "[", "c", "]"}]}], ")"}]}]}], ",",
RowBox[{
RowBox[{
SuperscriptBox["n0a", "\[Prime]",
MultilineFunction->None], "[", "c", "]"}], "\[Equal]",
RowBox[{
RowBox[{"x1", " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "x2"}], "+",
RowBox[{"n0", "[", "c", "]"}]}], ")"}]}], "-",
RowBox[{"x3", " ",
RowBox[{"n0a", "[", "c", "]"}]}]}]}], ",",
RowBox[{
RowBox[{
SuperscriptBox["n1", "\[Prime]",
MultilineFunction->None], "[", "c", "]"}], "\[Equal]",
RowBox[{
RowBox[{"x3", " ",
RowBox[{"n0a", "[", "c", "]"}]}], "-",
RowBox[{"n1", "[", "c", "]"}]}]}], ",",
RowBox[{
RowBox[{"n0", "[", "0", "]"}], "\[Equal]", "x5"}], ",",
RowBox[{
RowBox[{"n0a", "[", "0", "]"}], "\[Equal]", "5"}], ",",
RowBox[{
RowBox[{"n1", "[", "0", "]"}], "\[Equal]", "3"}]}],
"}"}]], "Output",
CellLabel->"Out[17]="]
}, Open ]],
Cell[BoxData[
RowBox[{
RowBox[{"f", "[",
RowBox[{
"cc_", ",", " ", "x1_", ",", " ", "x2_", ",", " ", "x3_", ",",
" ", "x5_"}], "]"}], " ", ":=", " ",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{", "sol1", "}"}], ",", " ",
RowBox[{
RowBox[{"Print", "[",
RowBox[{
"cc", ",", " ", "\"\<, \>\"", ",", " ", "x2", ",", " ",
"\"\<, \>\"", ",", " ", "x5"}], "]"}], ";",
RowBox[{"sol1", "=",
RowBox[{"NDSolve", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
SuperscriptBox["n0", "\[Prime]",
MultilineFunction->None], "[", "c", "]"}],
"\[Equal]",
RowBox[{
RowBox[{"-", "x1"}], " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "x2"}], "+",
RowBox[{"n0", "[", "c", "]"}]}],
")"}]}]}], ",",
RowBox[{
RowBox[{
SuperscriptBox["n0a", "\[Prime]",
MultilineFunction->None], "[", "c", "]"}],
"\[Equal]",
RowBox[{
RowBox[{"x1", " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "x2"}], "+",
RowBox[{"n0", "[", "c", "]"}]}],
")"}]}], "-",
RowBox[{"x3", " ",
RowBox[{"n0a", "[", "c", "]"}]}]}]}], ",",
RowBox[{
RowBox[{
SuperscriptBox["n1", "\[Prime]",
MultilineFunction->None], "[", "c", "]"}],
"\[Equal]",
RowBox[{
RowBox[{"x3", " ",
RowBox[{"n0a", "[", "c", "]"}]}], "-",
RowBox[{"n1", "[", "c", "]"}]}]}], ",",
RowBox[{
RowBox[{"n0", "[", "0", "]"}], "\[Equal]",
"x5"}], ",",
RowBox[{
RowBox[{"n0a", "[", "0", "]"}], "\[Equal]",
"5"}], ",",
RowBox[{
RowBox[{"n1", "[", "0", "]"}], "\[Equal]",
"3"}]}], "\[IndentingNewLine]", "}"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], ",",
RowBox[{"n0a", "[", "c", "]"}], ",",
RowBox[{"n1", "[", "c", "]"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"c", ",", " ", "0", ",", " ", "10"}],
"}"}]}], "]"}]}], ";", " ",
RowBox[{
RowBox[{"First", "[",
RowBox[{"Evaluate", "[",
RowBox[{
RowBox[{"n0", "[", "c", "]"}], "/.", "sol1"}],
"]"}], "]"}], "/.",
RowBox[{"c", "\[Rule]", " ", "cc"}]}]}]}],
"\[IndentingNewLine]", "]"}]}]], "Input",
CellLabel->"In[18]:="],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"f", "[",
RowBox[{
"1", ",", " ", "1", ",", " ", "1", ",", " ", "1", ",", " ",
"10"}], "]"}]], "Input",
CellLabel->"In[19]:="],
Cell[BoxData[
InterpretationBox[
RowBox[{
"1", "\[InvisibleSpace]", "\<\", \"\>", "\[InvisibleSpace]",
"1", "\[InvisibleSpace]", "\<\", \"\>", "\[InvisibleSpace]",
"10"}],
SequenceForm[ 1, ", ", 1, ", ", 10],
Editable->False]], "Print",
CellLabel->"From In[19]:="],
Cell[BoxData["4.3109149874187755`"], "Output",
CellLabel->"Out[19]="]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"NonlinearFit", "[",
RowBox[{"data", ",",
RowBox[{"f", "[",
RowBox[{
"cc", ",", " ", "x1", ",", " ", "x2", ",", " ", "x3", ",",
" ", "x5"}], "]"}], ",", " ", "cc", ",",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"x1", ",", "1.1"}], "}"}], ",", " ",
RowBox[{"{",
RowBox[{"x2", ",", " ", "1.2"}], "}"}], ",",
RowBox[{"{",
RowBox[{"x3", ",", " ", "0.9"}], "}"}], ",",
RowBox[{"{",
RowBox[{"x5", ",", "9"}], "}"}]}], "}"}]}],
"]"}]], "Input",
CellLabel->"In[21]:="],
Cell[BoxData[
InterpretationBox[
RowBox[{
"cc", "\[InvisibleSpace]", "\<\", \"\>", "\[InvisibleSpace]",
"x2", "\[InvisibleSpace]", "\<\", \"\>", "\[InvisibleSpace]",
"x5"}],
SequenceForm[ cc, ", ", x2, ", ", x5],
Editable->False]], "Print",
CellLabel->"From In[21]:="],
Cell[BoxData[
RowBox[{
RowBox[{"NDSolve", "::", "\<\"ndinnt\"\>"}],
RowBox[{
":", " "}], "\<\"Initial condition \\!\\(x5\\) is not a number \
or a rectangular array of numbers. \\!\\(\\*ButtonBox[\\\"More\
\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", \
ButtonFrame->None, ButtonData:>\\\"NDSolve::ndinnt\\\"]\\)\"\>"}]], \
"Message",
CellLabel->"From In[21]:="],
Cell[BoxData[
RowBox[{
RowBox[{"ReplaceAll", "::", "\<\"reps\"\>"}],
RowBox[{
":", " "}], "\<\"\\!\\({\\(NDSolve[\\(\\(\[LeftSkeleton] 1 \
\[RightSkeleton]\\)\\)]\\)}\\) is neither a list of replacement rules \
nor a valid dispatch table, and so cannot be used for replacing. \
\\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", \
ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \
ButtonData:>\\\"ReplaceAll::reps\\\"]\\)\"\>"}]], "Message",
CellLabel->"From In[21]:="],
Cell[BoxData[
RowBox[{
RowBox[{"FindFit", "::", "\<\"nlnum\"\>"}],
RowBox[{
":", " "}], "\<\"The function value \
\\!\\({\\(\\(\\(\\(-9.931660812424306`\\)\\) + \\(\\(n0[0.`]\\)\\)\\)\
\\), \\(\\(\\(\\(-9.015163674620991`\\)\\) + \
\\(\\(n0[0.2`]\\)\\)\\)\\), \\(\\(\[LeftSkeleton] 7 \
\[RightSkeleton]\\)\\), \\(\\(\\(\\(-2.2641341584217156`\\)\\) + \
\\(\\(n0[1.8`]\\)\\)\\)\\), \\(\\(\[LeftSkeleton] 41 \[RightSkeleton]\
\\)\\)}\\) is not a list of numbers with dimensions \\!\\({51}\\) at \
\\!\\({x1, x2, x3, x5}\\) = \\!\\({1.1`, 1.2`, 0.9`, 9.`}\\). \
\\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", \
ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \
ButtonData:>\\\"General::nlnum\\\"]\\)\"\>"}]], "Message",
CellLabel->"From In[21]:="],
Cell[BoxData[
RowBox[{
RowBox[{"NonlinearFit", "::", "\<\"fitfail\"\>"}],
RowBox[{
":", " "}], "\<\"The fitting algorithm failed.\"\>"}]], \
"Message",
CellLabel->"From In[21]:="]
}, Open ]]
}
- Follow-Ups:
- Re: Using NDSolve in NonlinearFit
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Using NDSolve in NonlinearFit