3

Consider the following function

n = 200000`200;
k = 2 n - 50`200;
μ = 10`200^-5;
ν = 10`200^-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]

enter image description here

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?

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
Remi.b
  • 1,135
  • 1
  • 7
  • 18
  • are defined all the constant in the fun[s] and try NMaximize[fun[s],s] – k_v Jan 21 '15 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 – Remi.b Jan 21 '15 at 19:08

1 Answers1

5

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.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • 1
    Use arbitrary precision (e.g., WorkingPrecision -> 16) in FindMaximum rather than machine precision to avoid FindMaximum::lstol warning. – Bob Hanlon Jan 21 '15 at 20:20
  • @BobHanlon, Good point, although it does not change the answer much: {9.097567799873060, {s -> 0.07999124376931556}}. – bbgodfrey Jan 21 '15 at 20:35