Consider the following function of one variable, k:
ClearAll[bar];
bar[k_] := NIntegrate[k t, {t, 0, 3}];
My actual integrand is more complicated than just k t, of course. This one could be integrated analytically, but my real bar cannot (to my knowledge). I'll show it below, but this integrand is a Minimal Viable Example (MVE).
All is well should I try to evaluate bar over a few values of k:
In[150]:= Table[bar[k], {k, 1, 4, 0.5}]
Out[150]= {4.5, 6.75, 9., 11.25, 13.5, 15.75, 18.}
But plotting it runs into trouble:
Plot[bar[k], {k, 1, 4}]
NIntegrate::inumr: The integrand k t has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,3}}.
That eventually produces a plot, but what I really need to do, NMinimize, does not work at all:
NMinimize[{bar[k], k >= 1, k <= 4, k \[Element] Reals}, k,
EvaluationMonitor -> Print[bar[k]]]
NIntegrate::inumr: The integrand k t has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,3}}.NIntegrate[k t,{t,0,3}]
I'd appreciate any advice.
PS: my real integrand is
(tt[r, t] - qs2[k t])^2
where
tt[r, t]
is
Piecewise[{{1, t > r}, {(r + t)/(2*r), -r <= t <= r}}, 0]
and
qs2[k t]
is
Piecewise[{{0, 0.5 + k*t <= 0}, {E^(-(0.5 + k*t)^(-2)),
0.5 + k*t > 0}}, 0]/
(Piecewise[{{0, 0.5 - k*t <= 0}, {E^(-(0.5 - k*t)^(-2)),
0.5 - k*t > 0}}, 0] + Piecewise[{{0, 0.5 + k*t <= 0},
{E^(-(0.5 + k*t)^(-2)), 0.5 + k*t > 0}}, 0])
My real problem is to find the values of k that minimize the integral of this integrand from t=0 to t=3 r as a function of r between r=0.07 and r=8. It's a least-squares fit of qs2[k t] (non-linear mollifier) to tt[r, t] (clipped linear function).
bar[k_?NumericQ]and things will work. – Leonid Shifrin Sep 11 '16 at 21:56