# How can I get the exact maximum of a function with a symbolic iterator index?

I have a functional sequence whose general term is $$a_n=x^n-x^{2\,n}$$ where $$x\in [0,1]$$ and $$n$$ is a symbolic iterator index, which means $$n$$ is an arbitrary positive integer. When $$n$$ runs from 1 to an arbitrary large positive integer, it gives the sequence.

I want to get the maximum of the general term $$a_n$$ with respect to the variable $$x$$ where $$x\in [0,1]$$ but with $$n$$ being kept in place, which means the solution should be values containing numbers,and/or $$n$$ but no $$x$$, illustrating the maximum of $$a_n$$ for every possible $$n$$.

The algorithm is simple, for every possible $$n$$, $$n$$ can be regarded as a positive constant in the term, then we can solve for the null point of the derivatives of $$a_n=x^n-x^{2\,n}$$ with respect to $$x$$:

Solve[D[x^n - x^(2 n), x] == 0, x]


{{x -> 2^(-1/n)}}

And the maximum:

After examining the sign of the derivative, it can be determined {{x -> 2^(-1/n)}} is the point where the general term attains its maximum.

Simplify[(x^n - x^(2 n)) /. x -> 2^(-1/n), n >= 1]


1/4

Although I can calculate step by step like above, what I want now is a function which can automate this process. Preferably, the built-in function Maximize[] since I have noticed:

Maximize[{x^\[Pi] - x^(2 \[Pi]), 0 <= x <= 1}, x]


{1/4, {x -> 2^(-1/[Pi])}}

where $$\pi$$ is a built-in constant greater than 1 and similar to $$n$$ in my sequence. However, when I try:

Maximize[{x^n - x^(2 n), 0 <= x <= 1}, x]


Maximize::infeas:"There are no values of {x} for which the constraints 0<=x<=1 are
satisfied and the objective function x^n-x^(2\n) is real-valued."

I guess it is because MMA does not know $$n$$ can be regarded as a constant for a specific step in my sequence, so I made those definitions below, intending to make $$n$$ just look like $$\pi$$ for MMA:

SetAttributes[n, Constant];
NumericQ[n] = True;
$Assumptions = n \[Element] Integers && n >= 1;  However, when I run the same code, it still gives: Maximize::infeas:"There are no values of {x} for which the constraints 0<=x<=1 are satisfied and the objective function x^n-x^(2\n) is real-valued." My questions are: 1.Why Maximize[{x^\[Pi] - x^(2 \[Pi]), 0 <= x <= 1}, x] works but Maximize[{x^n - x^(2 n), 0 <= x <= 1}, x] fails even after I made those definitions? 2.How can I make MMA recognize in a specific scope that $$n$$ can be regarded as a positive integer constant and thus give me the correct answer {1/4, {x -> 2^(-1/n)}}when I run Maximize[{x^n - x^(2 n), 0 <= x <= 1}, x]? 3.Is there any other functions that can be somehow employed to automate the process and get the correct answer? Thanks in advance. • Assuming[{0 <= x <= 1, n > 0}, sol = Solve[{D[x^n - x^(2 n), x] == 0, D[x^n - x^(2 n), {x, 2}] < 0}, x][[1]]; {x^n - x^(2 n) /. sol // Simplify, sol}] Commented Jul 14, 2022 at 15:21 • Thanks@BobHanlon, your solution is elegant and straightforward, but not applicable when there are more variables than just x. That's why I would like to employ Maximize[] and make it recognize n as a constant. Commented Jul 15, 2022 at 12:31 ## 2 Answers Edit Solution with Maximize. Maximize works best with linear and polynomial expressions.( See help function) Therefore using variable transformations. an = x^n - x^(2 n) // Factor (* -x^n (-1 + x^n) *) max = Maximize[{an /. x^n -> xn, 0 <= xn <= 1}, xn] (* {1/4, {xn -> 1/2}} *) Solve[(xn /. max[[2]]) == x^n && 0 <= x <= 1 && n > 0, x] (* {{x -> ConditionalExpression[2^(-1/n), n > 0]}} *)  A solution with Reduce (red = List @@ Reduce[{an == x^n - x^(2 n), 0 <= x <= 1, n > 0, Element[n, Integers]}, x, Reals, Backsubstitution -> True]) // TableForm  Maximum for an is 1/4 at x == 2^(-1/n). Didn't get Maximize to extract max of an from red. Example from comment. Since x and y are form 0 to Pi/2, eliminating Cos and converting Sin[x] == sx, Sin[y] == sy. anequ = an==n(Sin[x] + Cos[y] + Cos[x - y])//TrigExpand (* (0 <= x <= \[Pi]/2, 0 <= y <= \[Pi]/2) *) eli = Eliminate[{anequ, Sin[x]^2 + Cos[x]^2 == 1, Sin[y]^2 + Cos[y]^2 == 1, Sin[x] == sx, Sin[y] == sy}, {Cos[x], Cos[y]}] sol1 = First@ Solve[{Sin[x] == sx, Sin[y] == sy, 0 <= x <= Pi/2, 0 <= y <= Pi/2}, {x, y}] // Simplify[#, {0 <= x <= Pi/2, 0 <= y <= Pi/2}] & eli2 = List @@ (eli /. sol1 // Simplify) (* {an^4 + 4 n^4 sx^2 (1 + sy)^2 (-1 + sx^2 + sy^2) + 4 an^2 n^2 (1 + sy) (-1 + sy + sx^2 (2 + sy)) == 4 an n sx (1 + sy) (an^2 + 2 n^2 (1 + sy) (-1 + sx^2 + sy)), 0 <= sy < 1 && 0 <= sx < 1} *) max = Maximize[{an, eli2 && n > 0}, {an, sx, sy}] // Simplify[#, Assumptions -> Element[n, Integers] && n > 0] & (* {(3 Sqrt[3] n)/2, {an -> (3 Sqrt[3] n)/2, sx -> Sqrt[3]/2, sy -> 1/2}} *) {x -> ArcSin[sx], y -> ArcSin[sy]} /. max[[2]] (* {x -> Pi/3, y -> Pi/6} *)  • Thanks Akku for your suggestion. The method you proposed is impressive, but it can not deal with the case where there are more variables than just x, like an==n(Sin[x] + Cos[y] + Cos[x - y]) (0 <= x <= \[Pi]/2, 0 <= y <= \[Pi]/2) as I have tested. Commented Jul 18, 2022 at 6:01 $Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global*"]

tab1 = Table[{n, MaxValue[{x^n - x^(2 n), 0 <= x <= 1}, x]},
{n, 1, 10}]

(* {{1, 1/4}, {2, 1/4}, {3, 1/4}, {4, 1/4}, {5, 1/4}, {6, 1/4},
{7, 1/4}, {8, 1/4}, {9, 1/4}, {10, 1/4}} *)

max[n_] = 1/4;

tab2 = Table[{n, ArgMax[{x^n - x^(2 n), 0 <= x <= 1}, x]},
{n, 1, 10}]

(* {{1, 1/2}, {2, 1/Sqrt[2]}, {3, 1/2^(1/3)}, {4, 1/2^(1/4)},
{5, 1/2^(1/5)}, {6, 1/2^(1/6)}, {7, 1/2^(1/7)}, {8, 1/2^(1/8)},
{9, 1/2^(1/9)}, {10, 1/2^(1/10)}} *)

argMax[n_] = 2^FindSequenceFunction[Log2[tab2[[All, 2]]], n]

(* 2^(-1/n) *)
`