# Why FindMaximum doesn't work in my example

Consider the following function

n = 200000200;
k = 2 n - 50200;
μ = 10200^-5;
ν = 10200^-5;
qhat = k/(2 n);

fun[s_] :=
Log[(E^(4 n qhat s) (1 - qhat)^(-1 + 4 n μ) qhat^(-1 + 4 n ν))/
NIntegrate[ E^(4 n qhat s) (1 - qhat)^(-1 + 4 n μ) qhat^(-1 + 4 n ν),
{qhat, 1/(4 n + 1), 1 - 1/(4 n + 1)},  MaxRecursion -> 12]]


I can Plot my function in order to estimate the maximum

Plot[fun[s], {s, 0, 0.2}, AxesLabel -> {"s", "P(qhat | s)"},
PlotRange -> All]


The maximum seems to be around 0.075. Let's calculate it:

FindMaximum[fun[s], {s, 0.075}, MaxIterations -> 100]


The integrand [...] has evaluated to non-numerical values for all sampling points in the region with boundaries

Why can't I estimate the maximum with FindMaximum?

• are defined all the constant in the fun[s] and try NMaximize[fun[s],s]
– k_v
Commented Jan 21, 2015 at 19:02
• @K_v Yes, all constant are defined. I edited my post to add exactly how I defined them. NMaximize[fun[s],s] doesn't work either. It returns the same error message than FindMaximum Commented Jan 21, 2015 at 19:08

This is a recurring issue. Replace fun[s_]:= by fun[s_?NumericQ]:= to eliminate your error messages. FindMaximum then will give the answer, {9.09757, {s -> 0.0799914}}, which appears to be correct, despite the fact that FindMaximum gives the warning message that
FindMaximum::lstol: The line search decreased the step size to within the tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient increase in the function. You may need more than MachinePrecision digits of working precision to meet these tolerances."

See this Answer for a discussion of ?NumericQ.
• Use arbitrary precision (e.g., WorkingPrecision -> 16) in FindMaximum rather than machine precision to avoid FindMaximum::lstol warning. Commented Jan 21, 2015 at 20:20
• @BobHanlon, Good point, although it does not change the answer much: {9.097567799873060, {s -> 0.07999124376931556}}. Commented Jan 21, 2015 at 20:35