Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to fit some data to a model of exponential decay using the FindFit function:

data = {{0, 78}, {24, 64.5}, {48, 70.5}, {96, 54}, {144, 64.5}, {216, 3}, {336, 0}, {696, 0}};
model = data[[1, 2]]*Exp[-k1*t];

fit1 = FindFit[data, model, k1, t]
fit2 = FindFit[data, {model, k1 > 0}, k1, t]

modelf1 = Function[{t}, Evaluate[model /. fit1]];
modelf2 = Function[{t}, Evaluate[model /. fit2]];

Plot[#[t], {t, 0, 696}, Epilog -> Map[Point, data], PlotRange -> All] & /@ {modelf1, modelf2}

Interestingly, the model with no specified constraint on k1 finds a much better solution than the constrained model, but the solution of the unconstrained problem falls within the range of the constrained one. Here is the output:


{k1 -> 0.00512571}
{k1 -> 1.01979}

Result of the Plot

Why isn't the solution to the constrained problem at least as good as the solution of the unconstrained one?

share|improve this question
2  
I think the reason is that different methods are used for constrained and unconstrained problems. – Ajasja Aug 23 '12 at 13:10
2  
Have you tried: fit2 = FindFit[data, {model, k1 > 0}, k1, t, Method -> NMinimize] – P. Fonseca Aug 23 '12 at 13:13

2 Answers

up vote 5 down vote accepted

It is because your constraint forces the path chosen for finding a local extrema. You could verify it by doing:

data ={{0, 78}, {24, 64.5}, {48, 70.5}, {96, 54}, {144, 64.5}, {216, 3}, {336, 0}, {696, 0}};
model = data[[1, 2]]*Exp[-k1*t];
l = {}; l1 = {};
fit1 = FindFit[data,  model, k1,          t, EvaluationMonitor :> AppendTo[l, k1]]
fit2 = FindFit[data, {model, k1 > 0}, k1, t, EvaluationMonitor :> AppendTo[l1, k1]]

Show[ListLinePlot[{l1,l}], 
         ListPlot[{l1,l},PlotStyle ->{{Blue,PointSize[Large]}, {Red,PointSize[Medium]}}]]

Mathematica graphics

Edit

As for why some methods fail, you can see your extrema is very sharp:

f[k1_] := Total[(data[[1, 2]]*Exp[-k1*#[[1]]] - #[[2]])^2 & /@ data];
Plot[{f[k1], Evaluate[D[f[k2], k2] /. k2 -> k1]}, {k1, -1, 1}, 
 PlotRange -> {{-.1, .4}, {0, 16500}}, Axes -> {True, False}, Frame -> True]

Mathematica graphics

share|improve this answer
But it does not explain why the Automatic option for the constrained problem uses another path then the constrained "NMinimize" option? Just add l2={} and fit3 = FindFit[data, {model,k1>0}, k1, t, Method->"NMinimize",EvaluationMonitor :> AppendTo[l2, k1]] to your code. – Frederik Ziebell Aug 23 '12 at 15:17
1  
@FrederikZiebell as far as I can see, you are not asking that in your question – belisarius Aug 23 '12 at 15:33

Different methods are used for constrained and unconstrained problems.
Compare for example

data = {{0, 78}, {24, 64.5}, {48, 70.5}, {96, 54}, {144, 64.5}, {216, 
    3}, {336, 0}, {696, 0}};
model = data[[1, 2]]*Exp[-k1*t];
methods = {Automatic, "ConjugateGradient", "Gradient", 
          "LevenbergMarquardt", "Newton", "QuasiNewton",
         {"NMinimize", Method -> "NelderMead"},
         {"NMinimize", Method -> "DifferentialEvolution"}, 
         {"NMinimize", Method -> "SimulatedAnnealing"},
         {"NMinimize", Method -> "RandomSearch"} };


fit1 = FindFit[data, model, k1, t, Method -> #] & /@ methods;
fit2 = (FindFit[data, {model, k1 > 0}, k1, t, Method -> #] & /@ methods) \
       /. FindFit[__] -> "NA";

TableForm[Transpose@{fit1, fit2}, TableAlignments -> Left, 
          TableHeadings -> {methods, {"Unconstrained", "Constrained"}}]

output

Note the warnings of the type Method -> XXX can only be used for unconstrained problems.

We see that NMinimize was the only method suitable for this constrained problem. For the unconstrained problem the "Gradient" method was probably used.

Oh, and if you want the table LeftAlignment to actually work, check out this great answer.

share|improve this answer
2  
Add the option "Automatic" to your methods variable and you see the thing that makes me wonder: Why does the Automatic method for the unconstrained problem find the value k1 -> 1.01979 when "NMinimize" is the only method available? – Frederik Ziebell Aug 23 '12 at 13:49
@FrederikZiebell I always thought "Automatic" just means that mma chooses the method for you. But in this case it seems something more is happening. No idea for now. – Ajasja Aug 23 '12 at 13:50

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.