Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NonlinearModelFit and assumptions on fit parameters

4,478 views
Skip to first unread message

Niles

unread,
Sep 23, 2012, 3:00:50 AM9/23/12
to
Hi

I have a set of data (x, y) that I can succesfully fit a nonlinear function to using NonlinearModelFit:


data = {{1, 1}, {2, 2}, {3, 3.2}};
fitFuncExactNoLosses[a_, b_, x_] := a*x^2 + b + x;
nlm = NonlinearModelFit[data, fitFuncExactNoLosses[a, b, x],
{
{a, 1},
{b, 1}},
x]


However, the paramter "b" comes out negative and it *must* be positive. Is there a way to utilize assumptions such that b is constrained to be grater than zero?

Best,
Niels.

Bob Hanlon

unread,
Sep 24, 2012, 12:41:17 AM9/24/12
to
As shown in the documentation

data = {{1, 1}, {2, 2}, {3, 3.2}};

fitFuncExactNoLosses[a_, b_, x_] =
a*x^2 + b + x;

(nlm1 = NonlinearModelFit[data,
{fitFuncExactNoLosses[a, b, x], b > 0},
{{a, 1}, {b, 1}}, x]) // Normal

7.890764227861177*^-7 + x +
0.018364206486339258*x^2

nlm1["FitResiduals"]

{-0.018365, -0.0734576, 0.0347214}

(nlm2 = NonlinearModelFit[data,
{fitFuncExactNoLosses[a, b, x], b > 0},
{a, b}, x]) // Normal

0.011226160044776182 + x +
0.016760582112590634*x^2

nlm2["FitResiduals"]

{-0.0279867, -0.0782685, 0.0379286}

Plot[{nlm1[x], nlm2[x]}, {x, 0, 4},
Epilog -> {Red,
AbsolutePointSize[4],
Point[data]}]


Bob Hanlon

DC

unread,
Sep 24, 2012, 12:47:47 AM9/24/12
to
You can include contraints on the parameters as in :

nlm = NonlinearModelFit[data,
{fitFuncExactNoLosses[a, b, x],b>0}, {{a, 1}, {b, 1}}, x]

Frank K

unread,
Sep 24, 2012, 12:48:32 AM9/24/12
to
You could change b to b^2 in the fitting function and take the square root afterwards.

Bill Rowe

unread,
Sep 24, 2012, 12:48:42 AM9/24/12
to
On 9/23/12 at 3:01 AM, niels.m...@gmail.com (Niles) wrote:

>I have a set of data (x, y) that I can succesfully fit a nonlinear
>function to using NonlinearModelFit:

>data = {{1, 1}, {2, 2}, {3, 3.2}}; fitFuncExactNoLosses[a_, b_,
>x_]:= a*x^2 + b + x; nlm =
>NonlinearModelFit[data,fitFuncExactNoLosses[a, b, x],
>{ {a, 1}, {b, 1}}, x]

>However, the paramter "b" comes out negative and it *must* be
>positive. Is there a way to utilize assumptions such that b is
>constrained to be grater than zero?

Look up NonlinearModelFit in the Documentation Center and you
will find the second argument can be given in the form {model,
constrainte}. So, all you need do is

nlm = NonlinearModelFit[
data, {fitFuncExactNoLosses[a, b, x], b > 0}, {{a, 1}, {b,
1}}, x]


Alexei Boulbitch

unread,
Sep 25, 2012, 4:49:54 AM9/25/12
to
Hi

I have a set of data (x, y) that I can succesfully fit a nonlinear function to using NonlinearModelFit:


data = {{1, 1}, {2, 2}, {3, 3.2}};
fitFuncExactNoLosses[a_, b_, x_] := a*x^2 + b + x;
nlm = NonlinearModelFit[data, fitFuncExactNoLosses[a, b, x],
{
{a, 1},
{b, 1}},
x]


However, the paramter "b" comes out negative and it *must* be positive. Is there a way to utilize assumptions such that b is constrained to be grater than zero?

Best,
Niels.

Hi, Niels,
Try this:

ff = FindFit[data, {a*x^2 + b + x, b > 0.05}, {{a, 1}, {b, 1}}, x]

Show[{ListPlot[data, PlotStyle -> Red],
Plot[(a*x^2 + b + x) /. ff, {x, 0, 3}]}]

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu





0 new messages