I have some data in the form of {theta,y} and I am trying to fit a Legendre polynomial to it, however I don't know how I can get it to vary the m and l parameters in integer increments as I don't know what shape it will be in, my guess looks like this:

Fit[data, a*LegendreP[l, m, Cos[theta]]+b, m, l, a, b]

where a and b are just constants to adjust.

I have never fitted using Mathematica before so I don't know if this is easy or impossible, thanks for any advice.

  • $\begingroup$ correct cos(theta) ---> Cos[theta] ... and other syntax issues first $\endgroup$ Feb 14 '15 at 16:08
  • $\begingroup$ Are you sure you don't want to fit to a sum of such polynomials? That's what people usually do. $\endgroup$
    – Jens
    Feb 14 '15 at 18:23

The fitting functions in Mathematica can't solve ILP problems. However you could try to minimize the squared sum directly, which most certainly will require at least to add some common sense constraints on the parameter space. Like this, which gets the exact result:

f[a_, b_, l_, m_, t_] := a LegendreP[l, m, Cos[t]] + b;
data = Table[{t, f[a, b, l, m, t] /. {a -> 4, b -> 2, l -> 8, m -> 1}}, {t, 0, 5, .1}];
NMinimize[{Tr[Norm[#2 - f[a, b, l, m, #1]] & @@@ data],
           a > 0 && b > 0 && l > m > 0 && Element[{l, m}, Integers]}, {a, b, l, m}]

(* {3.19189*10^-15, {a -> 4., b -> 2., l -> 8, m -> 1}}*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.