MathGroup Archive 2011

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

Search the Archive

Re: Generating Arbitrary Linear Combinations of Functions

  • To: mathgroup at
  • Subject: [mg120366] Re: Generating Arbitrary Linear Combinations of Functions
  • From: DrMajorBob <btreat1 at>
  • Date: Wed, 20 Jul 2011 06:32:34 -0400 (EDT)

You say "this works well", but it actually can't, since f -- as you used  
it in defining "Data" -- is undefined.

(You didn't give us all the code.)

So I'll build the model, only.

Clear[f, a, z, c]
f[a_?NumberQ, x_?NumberQ, xi_?NumberQ, c_?NumberQ] :=
   c Exp[-a (x - xi)^2/2]/Sqrt@a;
f[a_, z_, c_][x_] := f[a, x, z, c]
n = 10;
aVec = Array[a, n];
zVec = Array[z, n];
cVec = Array[c, n];
model = Total@Through[(f @@@ Transpose@{aVec, zVec, cVec})@x]

f[a[1], x, z[1], c[1]] + f[a[2], x, z[2], c[2]] +
  f[a[3], x, z[3], c[3]] + f[a[4], x, z[4], c[4]] +
  f[a[5], x, z[5], c[5]] + f[a[6], x, z[6], c[6]] +
  f[a[7], x, z[7], c[7]] + f[a[8], x, z[8], c[8]] +
  f[a[9], x, z[9], c[9]] + f[a[10], x, z[10], c[10]]

Then, after f[oneArgument_] and "data" have ALSO been defined:

nlm = NonlinearModelFit[data, model, Flatten@{aVec, zVec, cVec}, x]


On Tue, 19 Jul 2011 05:55:32 -0500, Thomas Markovich  
<thomasmarkovich at> wrote:

> Hi All,
> I'm trying to curve fit an arbitrary linear combination of distributed
> gaussians to a function -- each with their own set of parameters.  
> Currently,
> if I want to use twenty functions, I do the following
> f[a_?NumberQ, x_?NumberQ, xi_?NumberQ, c_?NumberQ] := (
>   c E^(- a (x - xi)^2/2))/Sqrt[a/=F0];
> Data := Table[{n/50, N[f[n/50]]}, {n, -300, 300}];
> model = f[a1, x, x1, c1] + f[a2, x, x2, c2] + f[a3, x, x3, c3] +
>    f[a4, x, x4, c4] + f[a5, x, x5, c5] + f[a6, x, x6, c6] +
>    f[a7, x, x7, c7] + f[a8, x, x8, c8] + f[a9, x, x9, c9] +
>    f[a10, x, x10, c10] + f[a11, x, x11, c11] + f[a12, x, x12, c12] +
>    f[a13, x, x13, c13] + f[a14, x, x14, c14] + f[a15, x, x15, c15] +
>    f[a16, x, x16, c16] + f[a17, x, x17, c17] + f[a18, x, x18, c18] +
>    f[a19, x, x19, c19] + f[a20, x, x20, c20];
> nlm = NonlinearModelFit[Data,
>    model, {a1, x1, c1, a2, x2, c2, a3, x3, c3, a4, x4, c4, a5, x5, c5,  
> a6,
> x6,
>      c6, a7, x7, c7, a8, x8, c8, a9, x9, c9, a10, x10, c10, a11, x11,  
> c11,
>     a12, x12, c12, a13, x13, c13, a14, x14, c14, a15, x15, c15, a16, x16,
> c16,
>      a17, x17, c17, a18, x18, c18, a19, x19, c19, a20, x20, c20}, x];
> This works well but it becomes tedious to create these linear  
> combinations
> by hand. It would be wonderful to create a linear combination of  
> functions
> with a vector of coefficients for a, xi, and c. I am just unsure of how  
> to
> approach this and I was hoping that you guys could offer some insight  
> into
> this.
> Best,
> Thomas

DrMajorBob at

  • Prev by Date: Re: MultinormalDistribution Question
  • Next by Date: Re: sorting a nested list of 4-tuples
  • Previous by thread: Re: Generating Arbitrary Linear Combinations of Functions
  • Next by thread: Question about adding events to ndsolve