MathGroup Archive 2004

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

Search the Archive

Re: Fitting multiple data

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50992] Re: [mg50966] Fitting multiple data
  • From: "Maxim A. Dubinnyi" <maxim at nmr.ru>
  • Date: Fri, 1 Oct 2004 04:48:07 -0400 (EDT)
  • References: <200409300852.EAA26475@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Andriy Krasowsky wrote:

>Hallo!
>
>I have a problem with fitting of one function on several different
>data sets. The same step function describe two curves from experiments 
>of different arts and has to be fitted simultaneous. Is there is any
>option in Mathematica for that?
>
>Thanks a lot!
>
>Andriy Krasowsky
>
>  
>
Hi Andriy,

There is no build-in solution for this problem,
but it is easy to implement it yourself.

I am using the following strategy:
1. Introduce one additional independent variable MDL
for switching between models and data sets;
2. Construct model function of the form:
Which[MDL==1, Model1, MDL==2, Model2, ...];
3. Construct unified data set:
{{x1i, 1, y1i}, ..., {x2i, 2, y2i},...}
where the second element in each entry
is an index of original data set and appropriate
model function;
4. Peform ordinary fitting for new model function
and new unified data set.


The following is one simplest example:


<< Graphics`MultipleListPlot`
<< Graphics`Colors`
<< Statistics`NonlinearFit`

(* FIRST MODEL FUNCTION  *)
Model1 = a Cos[b1 x];

(* SECOND MODEL FUNCTION, ONE COMMON PARAMETER 'a'  *)
Model2 = a Cos[b2 x]; 

(* TRUE PARAMETER VALUES *)
TrueParams = {a->10, b1->1, b2->0.5};

(* NOISY DATA SETS FOR BOTH MODELS *)
SeedRandom[111];
Data1 = Table[{x, (Model1/.TrueParams)+
          Random[Real,{-1, 1}]}, {x, 0, 10}];
Data2 = Table[{x, (Model2/.TrueParams)+
          Random[Real,{-1, 1}]}, {x, 0, 15}];

(* DATA PLOT *)
gr1 = MultipleListPlot[Data1, Data2,
      PlotJoined -> True,
      PlotStyle -> {{Red}, {Blue}},
      SymbolStyle -> {Red, Blue}];

(* COMBINED MODEL FUNCTION *)
CombinedModel :=
  Which[MDL == 1, Evaluate@Model1,
        MDL == 2, Evaluate@Model2,
        True,     0];

(* COMBINED DATA SET *)
CombiledData = Join[Insert[#,1,2]&/@Data1, Insert[#,2,2]&/@Data2];
CombiledData//TableForm

(OUT[]=)
0   1  10.8067
1   1  4.67018
2   1  -4.99169
3   1  -9.27291
4   1  -5.76031
5   1  3.74943
6   1  9.01693
7   1  7.48695
8   1  -0.858472
9   1  -9.63991
10  1  -8.85283
0   2  9.51028
1   2  8.75934
2   2  4.57503
3   2  -0.0651154
4   2  -4.92631
5   2  -7.942
6   2  -9.61091
7   2  -8.65899
8   2  -7.04512
9   2  -2.66896
10  2  3.437
11  2  7.18905
12  2  10.131
13  2  9.39819
14  2  7.87224
15  2  3.39892

(* REGRESSION OF MULTIPLE DATA SETS *)
regreport = NonlinearRegress[
    CombiledData,
    CombinedModel,
    {x, MDL},
    {{a, 12}, {b1, 1}, {b2, 0.4}},
    RegressionReport -> {BestFit, BestFitParameters}]

(OUT[]=)
{BestFit ->
    Which[MDL==1, 9.96995 Cos[1.00147 x],
          MDL==2, 9.96995 Cos[0.501091 x],
          True, 0],
 BestFitParameters -> {a->9.96995, b1->1.00147, b2->0.501091}}

(* PLOT OF THE DATA AND BEST FIT FUNCTIONS *)
gr2 = Plot[
  Evaluate[(BestFit/.regreport)/.{{MDL->1},{MDL->2}}],
  {x, 0, 15},
  PlotStyle->{Red,Blue},
  DisplayFunction->Identity];
Show[gr1, gr2];


Regards,
  Maxim A. Dubinnyi.


  • Prev by Date: Re: Re: unevaluated, hold, holdform
  • Next by Date: Re: Re: Re: Hyperbolic function identity
  • Previous by thread: Re: GuiKit, Script[] and external function
  • Next by thread: Re: Simulating random fields in Mathematica