I implemented a Kalman filter, which perfectly works. This filter has four parameters denoted here by Q1,Q2,R1,R2. I'd like to find the optimal parameters, in the sense of an error measured by a function error. To make it simple, I fix Q2,R1,R2 and the optimization problem is only in one variable, Q1.

My problem in short The cost function to optimize is quite unexpensive to compute and is (at least locally) convex:

ListLinePlot[Table[{Q1,error[Q1,0.01,0.1,0.1]},{Q1,0,1,0.01}]] // AbsoluteTiming

enter image description here

However, when trying to optimize:

FindMinimum[{error[Q1, 0.01, 0.1, 0.1], 0 <= Q1 <= 1.}, {Q1, 0.1}]

never seems to end.

My goal would be to be able to optimize for all four parameters, but I fail even for one. It's not clear to me since the cost function seem to be nice. Any ideas on why FindMinimum is slow?

Full code:

(* generate data (vList) from reference (vFun) *)

(* definitions *)

(* process iteration i *)

(* run n steps *)

(* cost function *)
error[Q1_, Q2_, R1_, R2_] := Block[{xKal},
  xKal = run[Q1, Q2, R1, R2];
  Total@Table[(vFun[i] - xKal[[i]])^2, {i, 1, Length[xKal]}]]

Just for information, the aim of the Kalman filter is to denoise a signal. Example:

sol = run[0.0001, 0.01, 0.3, 0.1];
Show[Plot[vFun[t], {t, 1, n}, PlotStyle -> Red], 
 ListLinePlot[{vList, sol}, PlotRange -> Full, DataRange -> {0, n}]]

The red curve is the objective, the blue curve is the input data, the orange curve is the filtered data. The parameters are not optimal here, i.e. with some better parameters the orange curve would better match the red one. enter image description here

  • $\begingroup$ You have 0 <= Q1 < 0 inside the FindMinimum call... Is that intentional? $\endgroup$
    – user484
    Aug 18, 2016 at 19:56
  • $\begingroup$ @Rahul Thank you for pointing out, that's a typo. The problem still holds. $\endgroup$
    – anderstood
    Aug 18, 2016 at 20:11
  • $\begingroup$ Maybe you should consider the command KalmanFilter? $\endgroup$
    – bill s
    Aug 18, 2016 at 20:31
  • $\begingroup$ @bills My goal is to really understand what happens in a Kalman filter, so I prefer to write it with my own hands. It proves to be also a good way of learning MMA :) $\endgroup$
    – anderstood
    Aug 18, 2016 at 20:49

1 Answer 1


The problem seems to be that time is spent evaluating error symbolically. If I define

fn[q_?NumericQ] := error[q, 0.01, 0.1, 0.1]


FindMinimum[{fn[Q1], 0 <= Q1 <= 1.}, {Q1, 0.1}]

evaluates quickly, giving

{0.566953, {Q1 -> 0.0883772}}
  • $\begingroup$ Seems to solve the problem. How did you find this? I thought MMA would treat everything as numeric, since all the variables have numeric values. $\endgroup$
    – anderstood
    Aug 18, 2016 at 20:48
  • $\begingroup$ I think that MMA tries to understand the symbolic form of the expression it is being asked to minimise, before attempting minimisation. In your case, evaluating error[Q1, 0.01, 0.1, 0.1] takes a long time (presumably your code is not intended to work with symbolic inputs). The technique of adding ?NumericQ to any input that only makes sense when defined as an explicit number is a very general one, and is a good "defensive" coding technique. $\endgroup$
    – mikado
    Aug 18, 2016 at 20:54

Your Answer

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

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