MathGroup Archive 2000

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

Search the Archive

Re: Fit with constraints?

  • To: mathgroup at
  • Subject: [mg23324] Re: [mg23249] Fit with constraints?
  • From: "Mark Harder" <harderm at>
  • Date: Tue, 2 May 2000 00:43:07 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

    Judging from your message, you want to fit a model that is linear in
some parameters (eg. straight line, polynomial) to some data.  Fit[] is
pretty rigid in this regard, & I couldn't figure out how to make it work, so
I used Find Minimum, with a sum-of-squares error as the objective function
as follows:

...make some noisy data :
data = Table[{x, 5. + .1 x}, {x, 0., 6., 1.}]
noise= Table[.1*Random[Real, {-1., 1.}], {i, 7}]
data[[All, 2]] = data[[All, 2]] + noise

...then put sum-of-squares error function ( the dot product  error.error)
into FindMinimum, with "a" as the slope parameter, and intercept=5.0:
bestLine =
  FindMinimum[(data[[All, 2]] - (5. + a*data[[All, 1]])).(data[[All, 2]] -
(5. + a*data[[All, 1]])), {a, .2}]

for the noise vector I had, I get
{0.0159021, {a -> 0.108843}}   for a result.

To display the data and the best-fit line:
lp = ListPlot[data, DisplayFunction -> Identity];
modelPlot =   Plot[(5. + a x) /. bestLine[[2, 1]], {x, 0., 6.} ,
DisplayFunction -> Identity]
Show[{lp, modelPlot}, DisplayFunction -> $DisplayFunction ]

For more complicated model functions, replace (5.+a*data[[All,1]]) with your
function, where data[[All,1]] is the list of x-values in the data.  Note
that non-linear models will work here as well.

-----Original Message-----
From: Martin Rommel <Martin_Rommel at>
To: mathgroup at
Subject: [mg23324] [mg23249] Fit with constraints?

>It seems the Fit function lacks the option to fix a point (like "set
>intercept" in Excel).
>How is that best done?

  • Prev by Date: Re: plot discrete spectrum
  • Next by Date: Re: FindRoot inside a loop
  • Previous by thread: Re: Re: Fit with constraints?
  • Next by thread: Re: Fit with constraints?