Consider the following toy example:

model = a  Cos[w t];
data = Table[{t, Cos[2.1 t]}, {t, 0, 10, .25}] + RandomReal[.1, 41];

as both an unconstrained problem:

fit1 = FindFit[data, model, {a, w}, t]
(* {a -> -0.0768602, w -> 0.986448} *)

and a constrained problem:

fit2 = FindFit[data, {model, a > 0, 1 < w < 2}, {a, w}, t]
(* {a -> 0.875427, w -> 2.} *)

In @ilian's answer to this question I found out about the usage of Optimization`FindFit`ObjectiveFunction as a neat way to get hold of the objective function that is used internally.

Compare both objective functions:

obj1 = Optimization`FindFit`ObjectiveFunction[data, model, {a, w}, t]; 
obj2 = Optimization`FindFit`ObjectiveFunction[data, {model, a>0, 1<w<2}, {a, w}, t];

Table[{a, w, obj1[{a, w}]}, {a, 0.7, 0.8, 0.01}, {w, 0.5, 2.5, 0.1}] ==
Table[{a, w, obj2[{a, w}]}, {a, 0.7, 0.8, 0.01}, {w, 0.5, 2.5, 0.1}]
(* True  *)

and have a look at a plot of the objective function of the constrained problem (and notice no penalty-/barrier- behavior)



Is there an representation of the used penalty-/barrier- function as well? General resources about the functions used to convert from a constrained problem into the appropriate unconstrained version with penalty-/barrier- functions are welcome too.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.