# Maximize a definite integral with constraints on the limits

I would like to maximize the following definite double integral with respect to $$0:

$$\int _0^{\frac{d}{1-r}}\int _{\frac{d+r x-x}{r}}^s [(1-r) x+r y -d]dydx$$

under the constraints: $$0\leq d \leq 1$$, $$2d < s \leq 4$$, $$0 < r < \frac{1}{2}$$, $$0 \leq x \leq s$$, $$0\leq y \leq s$$.

Note that the variables of integration, i.e. $$x$$ and $$y$$, are both bounded by $$0$$ and $$s$$; for this reason, I have to be careful in my code regarding the limits of integration regions.

Here is my code:

maxR = Flatten[Table[{d, s, r /. Last@Maximize[{Integrate[(1 - r)*x + r*y - d, {x, 0, Min[s, d/(1 - r)]}, {y, Max[0, Min[s, (d + r*x - x)/r]], s}], 0 <= d <= 1, 2 d < s, 0 < r < 1/2}, {r}]}, {d, 0, 1, .1}, {s, 1, 4, .1}], 1];
ListPlot3D[maxR, PlotRange -> {0, 1}, AxesLabel -> {"d", "s", "r"}]


And this is running forever. Can anyone help please?

By the way, this is an extension of Ulich's answer to this question. While the latter shows how to get the solution of the integral, my question here regards a maximization of the integral.

• Possible duplicate of Solving a definite integral with constraints on the limits – AccidentalFourierTransform Jul 27 '19 at 2:17
• In 202468 we elaborated the integral numerically. Why didn't you use this result? – Ulrich Neumann Jul 27 '19 at 12:14
• @AccidentalFourier Transform, I have added an explanation that this post is not a duplicate of that post. – ppp Jul 27 '19 at 20:58
• @Ulrich, since my code in this post is part of a larger context, I utilized your suggestion about modifying the integration limits to my current code. Thanks so much! – ppp Jul 27 '19 at 21:01

With Integrate[...] substituded by the result from question 202534
int[s_?NumericQ, d_?NumericQ, r_?NumericQ] :=NIntegrate[(1 - r)*x + r*y - d, {x, 0, Min[s, d/(1 - r)]}, {y,Max[0, Min[s, (d + r*x - x)/r]], s}]

Table[NMaximize[{int[s, d, r], 0 < r < 1/2}, { r}], {d, 0,1, .1}, {s, 1, 4, .1 }]