# NonlinearModelFit with a matrix as weights

I have to fit a non linear model to my data and I am trying to use NonlinearModelFit. The problem is that I have systematic errors, so the weights options is not a simple vector but a matrix.

To be clear: my chi squared is written as:

(diff)T*(weight)^(-1)*(diff)


Where diff is the difference between my model and the data (T means trasposed).

Does anyone have any idea?

I use Mathematica 8.

You could do the NonlinearModelFit yourself? Following the example in the documentation:

Starting with the data

 data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}, {6, 4}, {7, 5}};


Extracting the ys, and xs

 ys = Last /@ data;xs = First /@ data;


wghts = Table[Exp[-(i - j)^2/8.], {i, Length[ys]}, {j, Length[ys]}]


Let us define the corresponding χ2

  χ2 = (ys-Log[a + b xs^2]).wghts.(ys-Log[a + b xs^2]);


And carry out the optimization

NMinimize[{ χ2, a > 0, b > 0}, {a, b}]


(* {0.782788,{a->0.825214,b->1.59388}} *)

to be compared to the NonlinearModelFit solution

 NonlinearModelFit[data, Log[a + b x^2], {a, b}, x] // Normal


(* log(1.42633 x^2+1.50632) *)

• Thank you for you answer, but I don't get your definition of the chi square. I am sorry to ask but: are you sure? Mar 23, 2014 at 19:49
• @mattiav27 yes but if you wish I could write it as: (ys-Log[a + b xs^2]).wghts.(ys-Log[a + b xs^2]) Mar 23, 2014 at 19:55