Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to fit my data to a combined function model. Say I have two functions with some common parameter(s)

f[x_]:= a*x^2 + b*x + c
g[x_]:= d*x^2 + e*x + c

Also, I have separate data and errors for these two functions.

Performing the fit is easy: I create the chi-squared function manually and minimize via NMinimize. In my work the code is rather lengthy, but looks similar to

Chi2[data_, errors_, dataprime_, errorsprime] := 
Sum[((data[[i, 2]] - f[data[[i, 1]]])/errors[[i]])^2,{i, 1, Length[data]}] + 
Sum[((data[[i, 2]] - g[data[[i, 1]]])/errors[[i]])^2,{i, 1, Length[data]}];

where data and dataprime are the different datasets for the functions f[x] and g[x] respectively (data[[i,1]] represents, say, the i-th x value while data[[i,2]] represents what f[data[[i,1]]] should be, say, y).

However, I would like to perform, using the values obtained from NMinimize, a NonlinearModelFit.

This would give me error estimation for parameters (which are hard to get with my current technique) and also provides confidence band levels and other features which are quite useful to me.

However, I haven't been able to find the way to implement a NonlinearModelFit ... any suggestion?

(I already did a search and found some links related to calculating the errrors through the Hessian. However, this is not convenien for me due to errors in calculating the inverse of the Hessian.)

Thanks in advance!

share|improve this question
    
Maybe this will help. –  b.gatessucks Dec 7 '12 at 10:13
    
I will give it a try, I was wondering doing a similar thing using a piecewisefunction, however, probably this is more convenient. I will let you know @b.gatessucks as soon as I am done ;) –  pablo Dec 7 '12 at 10:26
    
@b.gatessucks I was trying with a simple function, then I defined myfunc[y_, x_] := (a*x)*KroneckerDelta[1 - y]; And then performed the NonlinearModelFit eo = NonlinearModelFit[{{1, 1, 2.6}, {1, 2, 6.1}, {1, 3, 9.3}, {1, 4,11.9}, {1, 5, 15.4}}, myfunc[y, x], {a}, {y, x}] Howevere I get some error: "Experimental`NumericalFunction::dimsl: "{x} given in {y,x} should be a list of dimensions for a particular argument" Though fit seems ok. I don't get the problem, do you wonder what may be? THanks in advance! –  pablo Dec 7 '12 at 11:14

1 Answer 1

up vote 5 down vote accepted

That's one way, without the errors; you can add them as an exercise.

(* Sample data *)
SeedRandom[1];
dataf = Table[{i + RandomReal[{-0.4, 0.4}], i^1.5}, {i, 10}];
datag = Table[{i + RandomReal[{-0.4, 0.4}], i^2 - RandomReal[{-5, 5}]}, {i, 10}];

(* Prepend 1 to data for f, 2 to data for g *)
allData =  Join[{1, Sequence @@ #} & /@ dataf, {2, Sequence @@ #} & /@ datag];

myF[index_, x_] :=  KroneckerDelta[index - 1] f[x] + KroneckerDelta[index - 2] g[x]

nlm = NonlinearModelFit[allData, myF[index, x], {a, b, c, d, e}, {index, x}];

Show[ListPlot[{dataf, datag}],  Plot[{nlm[1, x], nlm[2, x]}, {x, 1, 10}]]

plot

nlm["ParameterConfidenceIntervalTable"]

table

share|improve this answer
    
Yes, that's perfect, thank you a lot! Now it's working for me thanks to you –  pablo Dec 7 '12 at 12:04
    
THanks for the corrections! ;) @m_goldberg –  pablo Dec 7 '12 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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