       Re: Linear and logarithmic fit

• To: mathgroup at smc.vnet.net
• Subject: [mg39368] Re: Linear and logarithmic fit
• From: "Kevin J. McCann" <KevinMcCann!kjm at uunet.uu.net>
• Date: Thu, 13 Feb 2003 04:51:53 -0500 (EST)
• References: <b2d1ig\$eak\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```What you want, probably, is a least squares fit, and then decide by
whichever one gives you the smallest residual. The least-squares fit picks a
function, e.g. a+b x. Then, given (a,b) computes the sum of the squares of
the difference between your data points and the straight line. Then you seek
those parameters (a,b) which minimize the resulting sum (residual). Here is
some code to do that. lsfit takes two arguments (a,b) and returns the
residual, FindMinimum then minimizes. The output of FindMinimum is the
residual and two rules with the best (a,b). Substitute your own function for
f[x_] in the body of lsfit.

Cheers,

Kevin

data = Table[{x, Sqrt[x]},

{x, 1., 3., 0.25}]

Plus @@ Apply[(f[#1] - #2)^2 & , data, {1}]

lsfit[(p_)?NumberQ, (q_)?NumberQ] :=

Module[{soln, f}, f[x_] := p + q*x;

Plus @@ Apply[(f[#1] - #2)^2 & , data,

{1}]]

lsfit[1, 1]

FindMinimum[lsfit[a, b], {a, -2, 2},

{b, -2, 2}]

lsfit[(p_)?NumberQ, (q_)?NumberQ] :=

Module[{soln, f}, f[x_] := p + q*Log[x];

Plus @@ Apply[(f[#1] - #2)^2 & , data,

{1}]]

FindMinimum[lsfit[a, b], {a, -2, 2},

{b, -2, 2}]

"jay Johnson" <joh_nson at yahoo.com> wrote in message
news:b2d1ig\$eak\$1 at smc.vnet.net...
> Hi everybody,
>
> If I have 9 points in a 2 dimensional space how do I decide if they
> fit better a linear function or a logarithmic function?
>