Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Automate datafitting to a series of parameterized function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70277] Automate datafitting to a series of parameterized function
  • From: "Peng Yu" <pengyu.ut at gmail.com>
  • Date: Wed, 11 Oct 2006 01:53:36 -0400 (EDT)

Suppose I have some data x_i, y_i. Let us generated them by

coef = Table[Random[Real, {-1, 1}], {i, 0, 4}]
poly = Table[x^n, {n, 0, 4}]
data = Table[{x, coef.poly}, {x, 0, 1, .01}]

Let us forget how we generated the data. Now, we want to find an
function to fit the data. The simplest way to do is using Tayler
series. But the problem is I don't know how many terms I should keep.
One way I can do is to try for different number of terms. I start by
only 1 term (0th order).

f[x_, a0_] := a0
f[x_] := Evaluate@NonlinearFit[data, f[x, a0], {x}, {a0}]
error = Apply[(f[#1] - #2)^2 &, data, {1}];
avererror = Apply[Plus, error]/Length[data]
maxerror = Apply[Max, error]

If the above one doesn't work, I'll try 2 terms (0th and 1st order).
f[x_, a0_, a1_] := a0 + a1 x
f[x_] := Evaluate@NonlinearFit[data, f[x, a0, a1], {x}, {a0, a1}];
error = Apply[(f[#1] - #2)^2 &, data, {1}];
avererror = Apply[Plus, error]/Length[data]
maxerror = Apply[Max, error]

I can try even higher orders. But the problem is that ever time I have
to define the function f[x_,a0_...] and supply the right function and
parameter list to NonlinearFit. I feel it is especially difficult to
parameterize the number of parameters to a function.

Can some give some idea on how to do it if it is possible in
Mathematica? Of cause I could write a python program to generate the
mathematica code, and call math.exe in the command line mode. But that
is just to much, I just want to stick with mathematica.

Thanks,
Peng


  • Prev by Date: Re: Faster ways to unionize intersecting sets?
  • Next by Date: Re: On order of options to Graphics
  • Previous by thread: Re: FourierCosTransform
  • Next by thread: Re: Automate datafitting to a series of parameterized function