Question on NonlinearFit

• To: mathgroup at smc.vnet.net
• Subject: [mg17974] Question on NonlinearFit
• From: "David Keith" <dkeith at hevanet.com>
• Date: Mon, 7 Jun 1999 02:51:25 -0400
• Organization: Hevanet Communications
• Sender: owner-wri-mathgroup at wolfram.com

```I am trying to use NonlinearFit to find the best-fit parameters for a
piecewise model. The problem is somewhat complicated, so I first constructed
a simple example to help me understand it, and it does not behave as I
expected.

First I construct a piecewise model which describes a curve which is
continuous through the first derivative (C1). It behaves as y = a x^b below
some x=x0, and continues as a line segment for x > x0.

In[30]:=
y[a_, b_, x0_, x_] := a x^b /; x < x0
In[31]:=
y[a_, b_, x0_, x_] := (x - x0) a b x0^(b - 1) + a x0^b /; x >= x0

I then produce a dataset by assuming values for the parameters and adding
some noise:

data = Table[{x, y[.5, 2, 10, x] + Random[Real, {-5, 5}]}, {x, 0, 20}];

Then I try to fit the data with NonlinearFit:

NonlinearFit[data,
Evaluate[y[aa, bb, xx0,
xx]], {xx}, {{aa, {1, 10}}, {bb, {1, 10}}, {xx0, {1, 10}}},

and get the response:

FindMinimum::"fmgs": "Could not symbolically find the gradient of y[aa, bb,
xx0, #1]. Try using the default method, giving two starting values for each
variable."

What confuses me is that I gave 2 values for each of the parameters as
required. It seems to be complaining that since I didn't give 2 values for
the variable, it needs a symbolic gradient. But the variable is the
variable -- it should know now that it can use numerical methods.

Can anyone tell me what I've done wrong? (I have attached the notebook, but
it might not survive the news server.)

Thanks.

begin 666 nlfit.nb
M*"HJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ
M*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ#0H-"B @(" @(" @(" @(" @
M(" @(" @36%T:&5M871I8V\$M0V]M<&%T:6)L92!.;W1E8F]O:PT*#0I4:&ES
M(&YO=&5B;V]K(&-A;B!B92!U<V5D(&]N(&%N>2!C;VUP=71E<B!S>7-T96T@
M=VET:"!-871H96UA=&EC82 T+C L#0I-871H4F5A9&5R(#0N,"P@;W(@86YY
M(&-O;7!A=&EB;&4 at 87!P;&EC871I;VXN(%1H92!D871A(&9O<B!T:&4@;F]T
M96)O;VL at #0IS=&%R=',@=VET:"!T:&4@;&EN92!C;VYT86EN:6YG('-T87)S
M(&%B;W9E+ at T*#0I4;R!G970 at =&AE(&YO=&5B;V]K(&EN=&\ at 82!-871H96UA
M=&EC82UC;VUP871I8FQE(&%P<&QI8V%T:6]N+"!D;R!O;F4@;V8 at #0IT:&4@
M9F]L;&]W:6YG. at T*#0HJ(%-A=F4 at =&AE(&1A=&\$@<W1A<G1I;F<@=VET:"!T
M:&4@;&EN92!O9B!S=&%R<R!A8F]V92!I;G1O(&\$ at 9FEL90T*("!W:71H(&\$@
M;F%M92!E;F1I;F<@:6X at +FYB+"!T:&5N(&]P96X at =&AE(&9I;&4@:6YS:61E
M('1H92!A<'!L:6-A=&EO;CL-"@T**B!#;W!Y('1H92!D871A('-T87)T:6YG
M('=I=&@@=&AE(&QI;F4@;V8@<W1A<G,@86)O=F4 at =&\ at =&AE#0H@(&-L:7!B
M;V%R9"P at =&AE;B!U<V4 at =&AE(%!A<W1E(&UE;G4 at 8V]M;6%N9"!I;G-I9&4@
M=&AE(&%P<&QI8V%T:6]N+ at T*#0I\$871A(&9O<B!N;W1E8F]O:W,@8V]N=&%I
M;G,@;VYL>2!P<FEN=&%B;&4 at -RUB:70 at 05-#24D@86YD(&-A;B!B90T*<V5N
M="!D:7)E8W1L>2!I;B!E;6%I;"!O<B!T:')O=6=H(&9T<"!I;B!T97AT(&UO
M9&4N("!.97=L:6YE<R!C86X at 8F4-"D-2+"!,1B!O<B!#4DQ&("A5;FEX+"!-
M86-I;G1O<V@@;W(@35,M1\$]3('-T>6QE*2X-"@T*3D]413H at 268@>6]U(&UO
M9&EF>2!T:&4 at 9&%T82!F;W(@=&AI<R!N;W1E8F]O:R!N;W0@:6X at 82!-871H
M96UA=&EC82T-"F-O;7!A=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&]W1G)A;64M/DYO<FUA;"HI#0H-"@T*#0I.
M;W1E8F]O:UM[#0I#96QL6R(\/"!3=&%T:7-T:6-S8\$YO;FQI;F5A<D9I=& B
M+" B26YP=70B+ T*("!#96QL5&%G<RT^(E,V+C4N,2)=+ T*#0I#96QL6T)O
M>\$1A=&%;#0H@(" @7"AY6V%?+"!B7RP@>#!?+"!X7UT at .CT@85P@>%Q>8B O
M.R!X(#P@>#!<*5TL("));G!U=")=+ T*#0I#96QL6T)O>\$1A=&%;#0H@(" @
M7"AY6V%?+"!B7RP@>#!?+"!X7UT at .CT@7"@H>" M('@P*5PI(&%<(&)<('@P
M7%Y<*&(@+2 Q7"D at *R!A7"!X,%Q>8B O.R -"B @(" @(" @>"!<6T=R96%T
M97)%<75A;%T@>#!<*5TL("));G!U=")=+ T*#0I#96QL6T)O>\$1A=&%;#0H@
M(" @7"A0;&]T6WE;("XU+" R+" Q,"P@>%TL('MX+" P+" R,'U=7"E=+" B
M26YP=70B72P-"@T*0V5L;%M";WA\$871A6PT*(" @(%PH7"AV87(@/2 U.UPI
M(#T at #0H@(" @(" @(%1A8FQE6WMX+"!Y6R N-2P@,BP@,3 L('A=("L at 4F%N
M9&]M6U)E86PL('M<*"UV87)<*2P at =F%R?5U]+"![>"P@,"P at #0H@(" @(" @
M(" @(" R,'U=.UPI7"E=+" B26YP=70B72P-"@T*0V5L;%M";WA\$871A6PT*
M(" @(%PH3&ES=%!L;W1;9&%T85U<*5TL("));G!U=")=+ T*#0I#96QL6T-E
M;&Q'<F]U<\$1A=&%;>PT*#0I#96QL6T)O>\$1A=&%;#0H@(" @7"A.;VYL:6YE
M87)&:71;9&%T82P at #0H@(" @("!%=F%L=6%T95MY6V%A+"!B8BP@>'@P+" -
M"B @(" @(" @("!X>%U=+"![>'A]+"![>V%A+"![,2P@,3!]?2P@>V)B+"![
M,2P@,3!]?2P@>WAX,"P@>S\$L(#\$P?7U]+" -"B @(" @(\$UE=&AO9"!<6U)U
M;&5=(\$=R861I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;&Q;,C\$Q,RP at -C4L(#\$S.2P@,RP@,3 U+" B26YP=70B72P-"D-E;&Q;,C(U
M-2P at -S L(#0W+" Q+" S-BP@(DEN<'5T(ETL#0H-"D-E;&Q;0V5L;\$=R;W5P
M1&%T85M[#0I#96QL6S(S,C<L(#<U+" Q.#4L(#0L(# at R+" B26YP=70B72P-
M"D-E;&Q;,C4Q-2P at .#\$L(#(S-RP at -"P at .3,L(")-97-S86=E(ETL#0I#96QL
M6S(W-34L(# at W+" Q,3,L(#(L(#,P+" B365S<V%G92)=+ T*0V5L;%LR.#<Q
M+" Y,2P at .#0W+" Q-2P@,C8V+" B3W5T<'5T(ET-"GTL(\$]P96X@(%U=#0I]
M#0I=#0HJ*0T*#0H-"@T*#0HH*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ
M*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BH-
M"D5N9"!O9B!-871H96UA=&EC82!.;W1E8F]O:R!F:6QE+ at T**BHJ*BHJ*BHJ
M*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ*BHJ
6*BHJ*BHJ*BHJ*BHJ*BHJ*BHI#0H-"@``
`
end

```

• Prev by Date: need to plot an arc
• Next by Date: Re: Question on symbols in modules
• Previous by thread: need to plot an arc
• Next by thread: NDSolve - large ODE systems