Re: Automate datafitting to a series of parameterized function
- To: mathgroup at smc.vnet.net
- Subject: [mg70313] Re: Automate datafitting to a series of parameterized function
- From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
- Date: Thu, 12 Oct 2006 05:38:00 -0400 (EDT)
- Organization: Uni Leipzig
- References: <egi1h9$j6r$1@smc.vnet.net>
Hi,
a Phyton script is a woderfull idea. Unitil you
have finished it
you can try
doFit[var_, param_] :=
Module[{pow, fit, err, f},
pow = Table[var^i, {i, 0, Length[param] - 1}];
fit = FindFit[data, pow.param, param, var];
f[x_] = pow.param /. fit /. var -> x;
err = (f[#1] - #2)^2 & @@@ data;
{pow.param /. fit, Total[err], Max[err]}
]
that return a list with the approximated
expression,
the mean and the maximum error.
Regards
Jens
"Peng Yu" <pengyu.ut at gmail.com> schrieb im
Newsbeitrag news:egi1h9$j6r$1 at smc.vnet.net...
| 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
|