# FindMaximum “is not a real number at” nrnum problem

I'm experiencing a problem using FindMaximum that I don't understand (due to my newbie knowledge of Mathematica's syntax). That is, FindMaximum sometimes returns a (correct) answer, but sometimes returns

The function value some number i is not a real number at {t} = {some number}.

I have two defined functions:

Envelope[t_, A_] := Norm[MatrixExp[(A) t]];

MaxEnvelope[A_, tmax_] := FindMaximum[{First@Envelope[t, A], 0 <= t <= tmax}, t];


## Working

For an example matrix that "works":

A1={{-1, 15}, {0, -10}}
MaxEnvelope[A1, 5]


returns

{1.40801, {t -> 0.212153}}


which makes sense given the plot

Plot[Envelope[t, A1], {t, 0, 5}]


## Problem

For an example matrix that throws the error:

A2={{-0.293578, -0.880734}, {0.0103211, -0.344037}}
MaxEnvelope[A2,5]


returns

FindMaximum::nrnum: "The function value -1.+4.54871*10^-9\ I is not a real number at {t} = {6.06351*10^-9}. "

There is clearly a maximum here though:

Plot[Envelope[t, A2], {t, 0, 5}]


The problem appears to be the { } that wrap t and what is clearly a real number. But why does the first matrix work and the second one not?

• I don't think the problem is the {}. It's the non-zero imaginary part of the function value (hence it is not real). – Michael E2 Dec 12 '15 at 20:30
• Try Clear[Envelope]; Envelope[t_?NumericQ, A_] := Norm[MatrixExp[(A) t]]; – Michael E2 Dec 12 '15 at 20:43
• That worked! Thank you very much. – user12734 Dec 13 '15 at 4:39

Using ?NumericQ to control evaluation fixes the problem. The reason is somewhat subtler than usual and is discussed below. I also removed the First from MaxEnvelope; it seemed to me to be a left-over from the OP trying to deal with the problem.

Clear[Envelope];
Envelope[t_?NumericQ, A_?(MatrixQ[#, NumericQ] &)] := Norm[MatrixExp[(A) t]];

MaxEnvelope[A_, tmax_] := FindMaximum[{Envelope[t, A], 0 <= t <= tmax}, t];

A2 = {{-0.293578, -0.880734}, {0.0103211, -0.344037}};
MaxEnvelope[A2, 5]

(*  {1.15797, {t -> 2.06801}}  *)


The use of ?NumericQ is discussed in What are the most common pitfalls awaiting new users? Here the reason it works is somewhat different than the usual reasons that one needs to control evaluation.

The problem comes from round-off error in evaluating Norm on a symbolic matrix with approximate coefficients. First, a matrix with a symbol t:

Norm[MatrixExp[(A2) t]] /. t -> 1.


Max::nord: Invalid comparison with 0.476429 -6.71157*10^-18 I attempted. >>
Max::nord: Invalid comparison with 1.1094 +1.19767*10^-17 I attempted. >>

(*  Max[0.476429 - 6.71157*10^-18 I, 1.1094 + 1.19767*10^-17 I]  *)


Next, with a completely numeric matrix:

Norm[MatrixExp[(A2) t] /. t -> 1.]
(*  1.1094  *)


If we inspect the result of Norm in the first example, we can see the problem. There are some small imaginary parts and some unevaluated Conjugate expressions (Short form shown):

 Norm[MatrixExp[(A2) t]]


Evidently, Norm does some processing on the symbolic matrix and there is round-off error in computing the coefficients. When this complex-valued expression is passed to FindMaximum there are problems. (The First@Envelope[..] in the OP can be seen to pick out the first argument to the expression Max[..] returned by Norm.)

The fix, t_?NumericQ and A_?(MatrixQ[#, NumericQ] &), ensures that MatrixExp[(A) t] is a completely numeric matrix before passing it to Norm.

• The problem is also demonstrated by SingularValueList[MatrixExp[A2 t], Tolerance -> 0] /. t -> 1.; since Norm[] is equivalent to taking the maximum of that, the OP's problem is seen. – J. M. will be back soon Dec 13 '15 at 11:21