Author 
Comment/Response 
Stan

06/29/12 08:05am
I've come up with a possible workaround but for some reason it gives results that are clearly wrong.
What I did is calculate points for the fuction L and fitted an interpolation function to those points and then maximized the interpolation function. This doesn't produce an error but gives clearly wrong results. With the example in the post above:
l[(k_)?NumericQ] := NDSolve[{y''[x] + (k + Sin[x]^2) y[x] == 0, y'[0] == 0, y[0] == 0.5}, y, {x, 0, 30}]
L[k_, t_] := (y[t] /. l[k])^2
data = Table[{k, First[L[k, 30]]}, {k, 0, 10, 0.001}];
f = Interpolation[data];
Maximize[{f[x], 0 < x < 10}, x]
However this gives
{0.229964, {x > 6.35663}}
which is obviously wrong. I can see quite clearly by plotting the fuction that the maximum is around x=0.4 and is about 120. In fact if I give this as an argument to the interpolation function it gives
In[06]:=f[0.4]
Out[06]=123.278
which is clearly much larger that what it found to be the maximum. If I give it a range that is around what I know is the maximum it does find it correctly:
In[07]:= Maximize[{f[x], 0 < x < 0.5}, x]
Out[07]= {123.308, {x > 0.398059}}
Why is it getting the wrong result for a wider range. Or is there a better way of doing this? Any help appreciated.
URL: , 
